def _knn_search_query(
        self,
        embedding_field: str,
        query_embedding: List[float],
        k: int,
        filters: Optional[MetadataFilters] = None,
        search_method="approximate",
        excluded_source_fields: Optional[List[str]] = None,
    ) -> Dict:
        """
        Perform a k-Nearest Neighbors (kNN) search.

        If the search method is "approximate" and the engine is "lucene" or "faiss", use efficient kNN filtering.
        Otherwise, perform an exhaustive exact kNN search using "painless scripting" if the version of
        OpenSearch supports it. If the OpenSearch version does not support it, use scoring script search.

        Note:
            - AWS OpenSearch Serverless does not support the painless scripting functionality at this time according to AWS.
            - Approximate kNN search does not support pre-filtering.

        Args:
            query_embedding (List[float]): Vector embedding to query.
            k (int): Maximum number of results.
            filters (Optional[MetadataFilters]): Optional filters to apply for the search.
                Supports filter-context queries documented at
                https://opensearch.org/docs/latest/query-dsl/query-filter-context/
            excluded_source_fields: Optional list of document "source" fields to exclude from the response.

        Returns:
            Dict: Up to k documents closest to query_embedding.

        """
        filters = self._parse_filters(filters)

        if not filters:
            search_query = self._default_approximate_search_query(
                query_embedding,
                k,
                vector_field=embedding_field,
                excluded_source_fields=excluded_source_fields,
            )
        elif (
            search_method == "approximate"
            and self._method["engine"]
            in [
                "lucene",
                "faiss",
            ]
            and self._efficient_filtering_enabled
        ):
            # if engine is lucene or faiss, opensearch recommends efficient-kNN filtering.
            search_query = self._default_approximate_search_query(
                query_embedding,
                k,
                filters={"bool": {"filter": filters}},
                vector_field=embedding_field,
                excluded_source_fields=excluded_source_fields,
            )
        else:
            if self.is_aoss:
                # if is_aoss is set we are using Opensearch Serverless AWS offering which cannot use
                # painless scripting so default scoring script returned will be just normal knn_score script
                search_query = self._default_scoring_script_query(
                    query_embedding,
                    k,
                    space_type=self.space_type,
                    pre_filter={"bool": {"filter": filters}},
                    vector_field=embedding_field,
                    excluded_source_fields=excluded_source_fields,
                )
            else:
                # https://opensearch.org/docs/latest/search-plugins/knn/painless-functions/
                search_query = self._default_scoring_script_query(
                    query_embedding,
                    k,
                    space_type="l2Squared",
                    pre_filter={"bool": {"filter": filters}},
                    vector_field=embedding_field,
                    excluded_source_fields=excluded_source_fields,
                )
        return search_query

        Perform a k-Nearest Neighbors (kNN) search.

        If the search method is "approximate" and the engine is "lucene" or "faiss", use efficient kNN filtering.
        Otherwise, perform an exhaustive exact kNN search using "painless scripting" if the version of
        OpenSearch supports it. If the OpenSearch version does not support it, use scoring script search.

        Note:
            - AWS OpenSearch Serverless does not support the painless scripting functionality at this time according to AWS.
            - Approximate kNN search does not support pre-filtering.

        Args:
            query_embedding (List[float]): Vector embedding to query.
            k (int): Maximum number of results.
            filters (Optional[MetadataFilters]): Optional filters to apply for the search.
                Supports filter-context queries documented at
                https://opensearch.org/docs/latest/query-dsl/query-filter-context/
            excluded_source_fields: Optional list of document "source" fields to exclude from the response.

        Returns:
            Dict: Up to k documents closest to query_embedding.

        
llama-index-integrations/vector_stores/llama-index-vector-stores-opensearch/llama_index/vector_stores/opensearch/base.py