I've been looked up the knn-search page but there's no python version template, hence can somebody tell me how can I perform exact knn with python api?
Thanks ! I've checked that out, yet I found the example is to search in a numeral field(price range as to the example). I wonder is it possible to search on a text field? Just like my code? If so, how should I edit the body func accordingly?
def exact_knn(self,query_text):
if query_text is not None and len(query_text) == 0:
print("query is empty")
return None
embed_model,_ = self.embedding_model_local()
query_vector = embed_model.encode(query_text)
return {
"query":{
"script_score": {
"query": {
'match': {
"text":query_text
}
},
"script": {
"source": "cosineSimilarity(params.queryVector, 'product-vector') + 1.0",
"params": {
"queryVector": query_vector
}
}
}
}
}
when testing I received this:
elasticsearch.BadRequestError: BadRequestError(400, 'search_phase_execution_exception', "class_cast_exception: class org.elasticsearch.script.field.EmptyField cannot be cast to class org.elasticsearch.script.field.vectors.DenseVectorDocValuesField (org.elasticsearch.script.field.EmptyField and org.elasticsearch.script.field.vectors.DenseVectorDocValuesField are in module org.elasticsearch.server@8.13.4 of loader 'app')")
My mapping is quite simple :
self.es_client.indices.create(
index=self.index_name,
mappings={
"properties": {
#name the fields as given
text_field: {"type": "text"},
title_field: {"type": "text"},
dense_vector_field: {"type": "dense_vector"},
num_characters_field: {"type": "integer"},
}
},
Thanks for all the follow up here. What version of Elastic are you using? Depending on your version you will want to make ensure that query_vectorcontains floats and not doubles.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.