Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing embeddings not skipped if filters are used #2230

Merged
merged 5 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions haystack/document_stores/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,9 +1212,11 @@ def query_by_embedding(
# +1 in similarity to avoid negative numbers (for cosine sim)
body = {"size": top_k, "query": self._get_vector_similarity_query(query_emb, top_k)}
if filters:
body["query"]["script_score"]["query"] = {
"bool": {"filter": LogicalFilterClause.parse(filters).convert_to_elasticsearch()}
}
filter_ = {"bool": {"filter": LogicalFilterClause.parse(filters).convert_to_elasticsearch()}}
if body["query"]["script_score"]["query"] == {"match_all": {}}:
body["query"]["script_score"]["query"] = filter_
else:
body["query"]["script_score"]["query"]["bool"]["filter"]["bool"]["must"].append(filter_)

excluded_meta_data: Optional[list] = None

Expand Down
13 changes: 13 additions & 0 deletions test/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,19 @@ def test_elasticsearch_delete_index():
index_exists = client.indices.exists(index=index_name)
assert not index_exists

@pytest.mark.parametrize("document_store", ["elasticsearch"], indirect=True)
def test_elasticsearch_query_with_filters_and_missing_embeddings(document_store):
document_store.write_documents(DOCUMENTS)
document_without_embedding = Document(content="Doc without embedding", meta={"name": "name_7", "year": "2021", "month": "04"})
document_store.write_documents([document_without_embedding])
filters = {"year": "2021"}
document_store.skip_missing_embeddings = False
with pytest.raises(RequestError):
document_store.query_by_embedding(np.random.rand(768), filters=filters)

document_store.skip_missing_embeddings = True
documents = document_store.query_by_embedding(np.random.rand(768), filters=filters)
assert len(documents) == 3

@pytest.mark.elasticsearch
def test_get_document_count_only_documents_without_embedding_arg():
Expand Down