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: Allow search in ChromaDocumentStore without metadata #863

Merged
merged 10 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,13 @@ def _query_result_to_documents(result: QueryResult) -> List[List[Document]]:
}

# prepare metadata
if metadatas := result.get("metadatas"):
document_dict["meta"] = dict(metadatas[i][j])
metadatas = result.get("metadatas")
print (metadatas)
silvanocerza marked this conversation as resolved.
Show resolved Hide resolved
try:
if metadatas and metadatas[i][j] is not None:
document_dict["meta"] = metadatas[i][j]
except IndexError:
raise IndexError("No metadata found for document: " + document_dict['id'])

if embeddings := result.get("embeddings"):
document_dict["embedding"] = np.array(embeddings[i][j])
Expand Down
32 changes: 32 additions & 0 deletions integrations/chroma/tests/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ def test_delete_not_empty_nonexisting(self, document_store: ChromaDocumentStore)

assert document_store.filter_documents(filters={"id": doc.id}) == [doc]

def test_document_store_search_without_metadata(self):
document_store = ChromaDocumentStore()
document_store.write_documents([Document(content=e) for e in ["First document", "Second document"]])
result = document_store.search(["First document"], top_k=1)

# Assertions to verify correctness
assert len(result) == 1
assert result[0][0].content == "First document"

def test_document_store_search_with_metadata(self):
document_store = ChromaDocumentStore()

# Writing documents to the document store
documents = [
Document(content="First document", meta={"author": "Author1"}),
Document(content="Second document"), # No metadata
Document(content="Third document", meta={"author": "Author2"}),
Document(content="Fourth document") # No metadata
]

document_store.write_documents(documents)
# Search for a document with metadata
result_with_metadata = document_store.search(["Author1"], top_k=1)
print(result_with_metadata[0])
assert result_with_metadata[0][0].content == "First document"

# Search for a document without metadata
#result_without_metadata = document_store.search(["Author2"], top_k=1)

#assert result_without_metadata[0][0].content == "Second document"


@pytest.mark.integration
def test_to_json(self, request):
ds = ChromaDocumentStore(
Expand Down
Loading