Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -473,6 +473,7 @@ def get_metadata_field_unique_values(
search_term: str | None = None,
from_: int = 0,
size: int = 10,
filters: dict[str, Any] | None = None,
) -> tuple[list[Any], int]:
"""
Returns unique values for a metadata field, optionally filtered by a search term, with pagination.
Expand All @@ -482,11 +483,12 @@ def get_metadata_field_unique_values(
against the metadata field's value.
:param from_: The offset to start returning values from (for pagination).
:param size: The maximum number of unique values to return.
:param filters: Optional filters to restrict the documents considered.
:returns: A tuple containing list of unique values (in their original type) and total count of
unique values.
"""
values = []
for doc in self.documents.values():
for doc in self.filter_documents(filters=filters):
val = FAISSDocumentStore._get_doc_value(doc, metadata_field)
if val is not None:
values.append(val)
Expand Down
13 changes: 13 additions & 0 deletions integrations/faiss/tests/test_document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,16 @@ def test_get_metadata_field_unique_values_preserves_non_string_types(self, docum
values, total_count = document_store.get_metadata_field_unique_values("priority")
assert set(values) == {1, 2}
assert total_count == 2

def test_get_metadata_field_unique_values_with_filters(self, document_store):
docs = [
Document(content="Doc 1", meta={"category": "A", "status": "active"}),
Document(content="Doc 2", meta={"category": "B", "status": "active"}),
Document(content="Doc 3", meta={"category": "C", "status": "inactive"}),
]
document_store.write_documents(docs)

filters = {"field": "meta.status", "operator": "==", "value": "active"}
values, total = document_store.get_metadata_field_unique_values("category", filters=filters)
assert set(values) == {"A", "B"}
assert total == 2
Loading