Table of Contents

Class MetadataFilter

Namespace
LMKit.Data
Assembly
LM-Kit.NET.dll

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 in or nin list.

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 key equals value.

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 key is strictly greater than value.

GreaterThanOrEqual(string, MetadataFilterValue)

Creates a filter matching entries whose value for key is greater than or equal to value.

In(string, params MetadataFilterValue[])

Creates a filter matching entries whose value for key equals at least one of values.

In(string, IEnumerable<string>)

Creates a filter matching entries whose value for key equals at least one of values.

LessThan(string, MetadataFilterValue)

Creates a filter matching entries whose value for key is strictly less than value.

LessThanOrEqual(string, MetadataFilterValue)

Creates a filter matching entries whose value for key is less than or equal to value.

Matches(MetadataCollection)

Evaluates this filter against a metadata collection.

NotEqual(string, MetadataFilterValue)

Creates a filter matching entries that carry key with a value different from value. 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 key with a value equal to none of values. A missing key does not match.

NotIn(string, IEnumerable<string>)

Creates a filter matching entries that carry key with a value equal to none of values. 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.

Share