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: get_documents_by_id should return docs for all passed ids #2064

Merged
merged 6 commits into from
Jan 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/_src/api/api/document_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ Fetch a document by specifying its text id string
| get_documents_by_id(ids: List[str], index: Optional[str] = None, batch_size: int = 10_000, headers: Optional[Dict[str, str]] = None) -> List[Document]
```

Fetch documents by specifying a list of text id strings
Fetch documents by specifying a list of text id strings. Be aware that passing a large number of ids might lead
to performance issues.

<a name="elasticsearch.ElasticsearchDocumentStore.get_metadata_values_by_key"></a>
#### get\_metadata\_values\_by\_key
Expand Down
7 changes: 5 additions & 2 deletions haystack/document_stores/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,12 @@ def get_document_by_id(self, id: str, index: Optional[str] = None, headers: Opti
return None

def get_documents_by_id(self, ids: List[str], index: Optional[str] = None, batch_size: int = 10_000, headers: Optional[Dict[str, str]] = None) -> List[Document]:
"""Fetch documents by specifying a list of text id strings"""
"""
Fetch documents by specifying a list of text id strings. Be aware that passing a large number of ids might lead
to performance issues.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could mention here that the maximum is 10,000 (with elasticsearch). We might even limit that further to prevent performance issues.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a short sentence about the 10k ES default limit. And I think it's a good idea to monitor first before limiting it for performance reasons. If we decide to limit this we should probably also limit other queries, shouldn't we?

"""
index = index or self.index
query = {"query": {"ids": {"values": ids}}}
query = {"size": len(ids), "query": {"ids": {"values": ids}}}
result = self.client.search(index=index, body=query, headers=headers)["hits"]["hits"]
documents = [self._convert_es_hit_to_document(hit, return_embedding=self.return_embedding) for hit in result]
return documents
Expand Down
19 changes: 18 additions & 1 deletion test/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,30 @@ def test_get_all_documents_with_incorrect_filter_value(document_store_with_docs)
assert len(documents) == 0


def test_get_documents_by_id(document_store_with_docs):
def test_get_document_by_id(document_store_with_docs):
documents = document_store_with_docs.get_all_documents()
doc = document_store_with_docs.get_document_by_id(documents[0].id)
assert doc.id == documents[0].id
assert doc.content == documents[0].content


def test_get_documents_by_id(document_store):
# generate more documents than the elasticsearch default query size limit of 10
docs_to_generate = 15
documents = [{'content': 'doc-' + str(i)} for i in range(docs_to_generate)]
doc_idx = 'green_fields'
document_store.write_documents(documents, index=doc_idx)

all_docs = document_store.get_all_documents(index=doc_idx)
all_ids = [doc.id for doc in all_docs]

retrieved_by_id = document_store.get_documents_by_id(all_ids, index=doc_idx)
retrieved_ids = [doc.id for doc in retrieved_by_id]

# all documents in the index should be retrieved when passing all document ids in the index
assert set(retrieved_ids) == set(all_ids)


def test_get_document_count(document_store):
documents = [
{"content": "text1", "id": "1", "meta_field_for_count": "a"},
Expand Down