Multi-Category/Tag Filters¶
Sometimes you may want to filter documents in Chroma based on multiple categories or tags e.g. games and movies.
Adding Categories¶
Store categories directly as an array metadata field:
On older Chroma versions that don't support array metadata, add each category as a separate boolean field:
No Empty Categories/Tags
Only add categories an item belongs to with flags set to True.
Do not add categories an item does not belong to and set the flag to False.
Querying by Category¶
Use $contains to match documents with a specific category:
results = collection.query(
query_texts=["This is a query document"],
where={"categories": {"$contains": "games"}},
)
Match documents in any of several categories with $or:
results = collection.query(
query_texts=["This is a query document"],
where={
"$or": [
{"categories": {"$contains": "games"}},
{"categories": {"$contains": "movies"}},
]
},
)
Exclude a category with $not_contains: