admin管理员组文章数量:1289637
I was following this documentation page from Elastic Search .html
I have stored the vectors already and tried to query the documents against another vector embedding but every hit returns the same score.
Here's my mapping inside a create_index function
def create_index(es_client, index_name, dim):
# Define index settings and mappings
mapping = {
"mappings": {
"properties": {
"page_id": {"type": "keyword"},
"chunk_id": {"type": "keyword"},
"embedding": {
"index": True, # set to true to enable the use of the knn query
"type": "dense_vector",
"dims": dim,
"similarity": "cosine",
},
}
}
}
if es_client.indices.exists(index=index_name):
print(f"Index {index_name} already exists.")
else:
es_client.indices.create(index=index_name, body=mapping)
print(f"Index {index_name} created successfully.")
And here's the query wrapped in a function
def search_similar_documents(user_embedding, top_k=3):
"""
Perform a k-NN semantic search in Elasticsearch.
:param user_embedding: List of floats representing the user's query embedding.
:param top_k: Number of similar documents to retrieve.
:return: List of document IDs.
"""
query = {
"knn": {
"field": "embedding",
"query_vector": user_embedding,
"k": top_k,
"num_candidates": 100,
},
"_source": ["page_id", "chunk_id"],
}
return es_client.search(index=ELASTICSEARCH_INDEX_NAME, body=query)
response = search_similar_documents(human_input_embedded, top_k=20)
All 20 hits have the same score!
I don't have the text field on my mapping. Could that be the issue? I have the text field stored in a Neo4J for another purpose.
本文标签:
版权声明:本文标题:elasticsearch - Elastic Search KNN Semantic Search with pre stored embeddings returing the same score for every hit - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741480280a2381127.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论