assert 'text' in knn_result['hits']['hits'][0]['fields'].keys()
# Test `hybrid_search` method
query = "Hello"
hybrid_result = knn_search.knn_hybrid_search(query = query, model_id= model_id, k=2, fields=['text'])
assert 'text' in hybrid_result['hits']['hits'][0]['fields'].keys()
Test with es client connection rather than cloud_id#
# Create Elasticsearch connection
es_connection = Elasticsearch(
    hosts=['https://es_cluster_url:port'], 
    basic_auth=('user', 'password')
)
# Instantiate ElasticsearchEmbeddings using es_connection
embeddings = ElasticsearchEmbeddings.from_es_connection(
    model_id,
    es_connection,
)
# Initialize ElasticKnnSearch
knn_search = ElasticKnnSearch(
	es_connection = es_connection,
	index_name= test_index, 
	embedding= embeddings
)
# Test `knn_search` method with model_id and query_text
query = "Hello"
knn_result = knn_search.knn_search(query = query, model_id= model_id, k=2)
print(f"kNN search results for query '{query}': {knn_result}")
print(f"The 'text' field value from the top hit is: '{knn_result['hits']['hits'][0]['_source']['text']}'")
previous
DocArrayInMemorySearch
next
FAISS
 Contents
  
ElasticSearch
ElasticVectorSearch class
Installation
Example
ElasticKnnSearch Class
Test adding vectors
Test knn search using query vector builder
Test knn search using pre generated vector
Test source option
Test fields option
Test with es client connection rather than cloud_id
By Harrison Chase
    
      © Copyright 2023, Harrison Chase.
rtdocs_stable/api.python.langchain.com/en/stable/modules/indexes/vectorstores/examples/elasticsearch.html