Filter by metadata
Store metadata on memories, then narrow a search with equality, range and set filters.
Metadata is a set of key and value pairs that you attach to a memory. You can then filter a search by those pairs. Metadata values must be strings, integers or floats.
These examples reuse client and a scope named alice, built as in Scope memories. The Go and Java examples take the client and scope values they use as parameters. The Go examples also take ctx.
Add metadata
Pass metadata to add. The server rejects a value that is a boolean, null, an object or an array. It answers HTTP 400 with the code VALIDATION_FAILED.
client.add(
"Order A-1001 shipped",
scope=alice,
metadata={"order_id": "A-1001", "total": 42.5, "priority": 2},
)Filter a search
Pass metadata_filter to search. The filter combines with your scope and your query.
- Write a filter object. A plain value means equality:
{"order_id": "A-1001"}. - Use an operator object to compare:
{"priority": {"$gte": 2}}. - Pass the object as
metadata_filter.
results = client.search(
"shipped orders",
scope=alice,
metadata_filter={"priority": {"$gte": 2}},
)TypeScript names the option metadataFilter. Go and C# use the WithMetadataFilter method. Java uses withMetadataFilter.
Operators
The table lists the filter operators, what each one means, and the value type each one accepts.
| Operator | Meaning | Value type |
|---|---|---|
$eq | Equals. | String or number |
$ne | Does not equal. | String or number |
$gt, $gte | Greater than, or greater than or equal to. | Number |
$lt, $lte | Less than, or less than or equal to. | Number |
$in | Equals any value in a list. | Non-empty list of strings, or of numbers |
$nin | Equals no value in a list. | Non-empty list of strings, or of numbers |
$and, $or | Combines filters. | List of filters |
$not | Negates a filter. | A filter |
The four comparison operators $gt, $gte, $lt and $lte compare numbers. They do not compare strings. Operators on the same field combine with AND. This filter matches a priority from 1 to 3:
metadata_filter = {"priority": {"$gte": 1, "$lte": 3}}A filter can nest to a depth of 5 and hold up to 50 nodes.
Handle the unsupported operator
The $contains operator is not supported. A search that uses it fails with HTTP 422 and the code CONTAINS_NOT_SUPPORTED. The SDK raises its unprocessable error.
from smritea import SmriteaUnprocessableError
try:
client.search("orders", metadata_filter={"note": {"$contains": "a"}})
except SmriteaUnprocessableError as e:
print(e.code) # CONTAINS_NOT_SUPPORTEDUse $eq or $in for exact matches instead.
Related pages
Last updated on



