MetadataFilter

Base Classes · AiDotNet.RetrievalAugmentedGeneration.Filtering

An immutable, provider-agnostic boolean metadata-filter expression tree. Supports equality, inequality, full ranges, set membership, existence and arbitrary AND/OR/NOT nesting, mirroring the filter capabilities of Pinecone, Qdrant, Weaviate, Milvus, Azure AI Search, pgvector and Elasticsearch.

Known derived types: ComparisonFilter, ExistsFilter, InFilter, LogicalFilter, NotFilter

For Beginners

This is a small, database-independent way to describe “which documents do I want”. For example:

The same filter object works against every store: an in-memory store evaluates it directly, while Pinecone/Qdrant/etc. translate it into their own query syntax.

How It Works

Build filters with the static factory methods (Eq, Range, In, …) and compose them with the fluent combinators (And, Or, Not). The resulting tree can be evaluated in-memory with Matches or translated to a backend’s native filter language by a document store.

Properties

Property Summary
MetadataFilterOperator Operator { get; } Gets the operator that identifies this node’s kind.

Methods

Method Summary
MetadataFilter And(IEnumerable<MetadataFilter> filters) Returns a new filter that is the logical AND of this filter and other.
MetadataFilter And(MetadataFilter other) Returns a new filter that is the logical AND of this filter and other.
MetadataFilter And(MetadataFilter[] filters) Returns a new filter that is the logical AND of this filter and other.
MetadataFilter Eq(string key, object value) Creates a filter that matches when key equals value.
MetadataFilter Exists(string key) Creates a filter that matches when key is present in the metadata.
MetadataFilter Gt(string key, object value) Creates a filter that matches when key is strictly greater than value.
MetadataFilter Gte(string key, object value) Creates a filter that matches when key is greater than or equal to value.
MetadataFilter In(string key, IEnumerable<object> values) Creates a filter that matches when key equals one of values.
MetadataFilter In(string key, object[] values) Creates a filter that matches when key equals one of values.
MetadataFilter Lt(string key, object value) Creates a filter that matches when key is strictly less than value.
MetadataFilter Lte(string key, object value) Creates a filter that matches when key is less than or equal to value.
MetadataFilter Ne(string key, object value) Creates a filter that matches when key is missing or does not equal value.
MetadataFilter Not() Returns a new filter that is the logical negation of this filter.
MetadataFilter Or(IEnumerable<MetadataFilter> filters) Returns a new filter that is the logical OR of this filter and other.
MetadataFilter Or(MetadataFilter other) Returns a new filter that is the logical OR of this filter and other.
MetadataFilter Or(MetadataFilter[] filters) Returns a new filter that is the logical OR of this filter and other.
MetadataFilter Range(string key, object lower, object upper) Creates an inclusive range filter (lower <= key <= upper), the equivalent of Gte(key, lower).And(Lte(key, upper)).
bool IsNumeric(object value) Returns true when value is a built-in numeric type.
bool Matches(IReadOnlyDictionary<string, object> metadata) Evaluates this filter against a metadata dictionary in memory.
bool TryToDouble(object value, double result) Attempts to interpret a metadata value as a Double (numeric types and numeric strings).
bool ValuesEqual(object a, object b) Compares two metadata values for equality using lenient, provider-like semantics: numbers are compared numerically (so 2020 and 2020L and "2020" match), booleans by value, and everything else by ordinal string form.
int? CompareValues(object left, object right) Orders two metadata values, returning a negative/zero/positive result like CompareTo, or null when they are not comparable.