The Key/K Class
TheKey class (aliased as K for brevity) provides a fluent interface for building filter expressions. Use K to reference document fields, IDs, and metadata properties.
Filterable Fields
Comparison Operators
Supported operators:==- Equality (all types: string, numeric, boolean)!=- Inequality (all types: string, numeric, boolean)>- Greater than (numeric only)>=- Greater than or equal (numeric only)<- Less than (numeric only)<=- Less than or equal (numeric only)
Set and String Operators
Supported operators:is_in()- Value matches any in the listnot_in()- Value doesn’t match any in the listcontains()- OnK.DOCUMENT: substring search (case-sensitive). On metadata fields: checks if an array contains a scalar value.not_contains()- OnK.DOCUMENT: excludes by substring. On metadata fields: checks that an array does not contain a scalar value.regex()- String matches regex pattern (currently K.DOCUMENT only)not_regex()- String doesn’t match regex pattern (currently K.DOCUMENT only)
Array Metadata
Chroma supports storing arrays of values in metadata fields. You can usecontains() / not_contains() (or $contains / $not_contains in dictionary syntax) to filter records based on whether an array includes a specific scalar value.
Storing Array Metadata
Arrays can contain strings, numbers, or booleans. All elements in an array must be the same type. Empty arrays are not allowed.Filtering Arrays
Usecontains() to check if a metadata array includes a value, and not_contains() to check that it does not.
Supported Array Types
Logical Operators
Supported operators:&- Logical AND (all conditions must match)|- Logical OR (any condition can match)
Common Filtering Patterns
Edge Cases and Important Behavior
Missing Keys
When filtering on a metadata field that doesn’t exist for a document:- Most operators (
==,>,<,>=,<=,is_in()) evaluate tofalse- the document won’t match !=evaluates totrue- documents without the field are considered “not equal” to any valuenot_in()evaluates totrue- documents without the field are not in any list
Mixed Types
Avoid storing different data types under the same metadata key across documents. Query behavior is undefined when comparing values of different types.String Pattern Matching Limitations
regex() and not_regex() only work on K.DOCUMENT. These operators do not yet support metadata fields.
contains() and not_contains() have different behavior depending on the field:
- On
K.DOCUMENT: substring search (the pattern must have at least 3 literal characters) - On metadata fields: array membership check (see Array Metadata above)
Complete Example
Here’s a practical example combining different filter types:Tips and Best Practices
- Use parentheses liberally when combining conditions with
&and|to avoid precedence issues - Filter before ranking when possible to reduce the number of vectors to score
- Be specific with ID filters - using
K.ID.is_in()with a small list is very efficient - String matching is case-sensitive - normalize your data if case-insensitive matching is needed
- Use the right operator -
is_in()for multiple exact matches,contains()for substring search
Next Steps
- Learn about ranking and scoring to order your filtered results
- See practical examples of filtering in real-world scenarios
- Explore batch operations for running multiple filtered searches