Class MetadataFilter
A metadata filter expression evaluated against the string key/value metadata attached to stored entries. A filter is a tree: comparison nodes test one key against one or more typed values (MetadataFilterValue), and composite nodes combine children with And or Or.
public abstract class MetadataFilter
- Inheritance
-
MetadataFilter
- Derived
- Inherited Members
Examples
// status == "published" AND (year >= 2024 OR department IN ("legal", "finance"))
var filter = MetadataFilter.And(
MetadataFilter.Equal("status", "published"),
MetadataFilter.Or(
MetadataFilter.GreaterThanOrEqual("year", 2024),
MetadataFilter.In("department", "legal", "finance")));
Remarks
Supported comparison operators: eq, ne, gt, gte, lt,
lte, in, nin and exists. Every comparison except
exists(false) requires the key to be present: a missing key never matches, including
for ne and nin. Metadata values are stored as strings; the filter value's kind
decides how the stored string is interpreted (text, number, boolean, or ISO 8601 date), and a
stored value that cannot be interpreted in that domain never matches.
The JSON form produced by ToJson() and accepted by FromJson(string) is a
superset of the OpenAI attribute-filter shape: comparison nodes are
{"type":"eq","key":"status","value":"published"} and composite nodes are
{"type":"and","filters":[...]}. The extensions are in/nin (the value is
an array), exists (the value is an optional boolean, default true), and an
optional "value_type":"date" marker that switches a string value to temporal
comparison.
Structural limits, enforced at construction and parse time: at most MaxDepth
levels of nesting, MaxConditions nodes in total, and MaxListValues
values per in/nin list.
Fields
- MaxConditions
Maximum total number of nodes (comparisons and composites) in a filter tree.
- MaxDepth
Maximum nesting depth of a filter tree.
- MaxListValues
Maximum number of values in an
inorninlist.
Properties
- ConditionCount
Gets the total number of nodes in this node's subtree, itself included.
- Depth
Gets the nesting depth of this node's subtree (a single comparison is 1).
- Operator
Gets the operation this node applies.
Methods
- And(params MetadataFilter[])
Creates a filter matching entries that satisfy every one of
filters.
- Equal(string, MetadataFilterValue)
Creates a filter matching entries whose value for
keyequalsvalue.
- Exists(string)
Creates a filter matching entries that carry
key, whatever its value.
- FromJson(string)
Parses a filter from its JSON form (see the class remarks for the accepted shape).
- FromMetadata(MetadataCollection)
Builds the filter equivalent to legacy containment filtering: the conjunction of one string-equality comparison per metadata entry.
- GreaterThan(string, MetadataFilterValue)
Creates a filter matching entries whose value for
keyis strictly greater thanvalue.
- GreaterThanOrEqual(string, MetadataFilterValue)
Creates a filter matching entries whose value for
keyis greater than or equal tovalue.
- In(string, params MetadataFilterValue[])
Creates a filter matching entries whose value for
keyequals at least one ofvalues.
- In(string, IEnumerable<string>)
Creates a filter matching entries whose value for
keyequals at least one ofvalues.
- LessThan(string, MetadataFilterValue)
Creates a filter matching entries whose value for
keyis strictly less thanvalue.
- LessThanOrEqual(string, MetadataFilterValue)
Creates a filter matching entries whose value for
keyis less than or equal tovalue.
- Matches(MetadataCollection)
Evaluates this filter against a metadata collection.
- NotEqual(string, MetadataFilterValue)
Creates a filter matching entries that carry
keywith a value different fromvalue. A missing key does not match.
- NotExists(string)
Creates a filter matching entries that do not carry
key.
- NotIn(string, params MetadataFilterValue[])
Creates a filter matching entries that carry
keywith a value equal to none ofvalues. A missing key does not match.
- NotIn(string, IEnumerable<string>)
Creates a filter matching entries that carry
keywith a value equal to none ofvalues. A missing key does not match.
- Or(params MetadataFilter[])
Creates a filter matching entries that satisfy at least one of
filters.
- ToJson()
Serializes this filter to its canonical JSON form.