Filter language
The JSON filter language - operators, nested paths, strict typing rules, how missing fields behave, and examples checked against the shared conformance file.
A filter is a JSON object that selects documents. It is compiled by raftdb_exec::filter::compile(json_text, &Dict) into a plan (Plan IR) and run over documents in batches. The compiler never panics: bad JSON, an unknown operator or a wrong value type returns an error message (BAD_FILTER on the wire, 400 syntax_error over HTTP).
Syntax#
filter = { field: condition, ... } fields are ANDed
| { "$and": [filter, ...] } | { "$or": [filter, ...] }
condition = literal same as {"$eq": literal}
| { operator: value, ... } several operators on one field are ANDed
operator = $eq $ne $gt $gte $lt $lte $in $nin $exists $not $text{} matches every document. A dotted name such as "address.zip" reaches nested objects. $and and $or take arrays of filters. $in and $nin take arrays of literals, $exists takes true or false, and $not wraps a condition.
Operators#
| Operator | True when |
|---|---|
$eq | the field exists and has the literal's type and value |
$ne | not $eq; true for a missing field or a field of another type |
$gt $gte $lt $lte | the field exists, has the literal's type and compares as asked |
$in | equals one of the literals (same type) |
$nin | equals none of them; true for missing or different-typed values |
$exists | the field is present (true) or absent (false) |
$not | the wrapped condition is false; true for missing values |
$text | the field is a string and matches a text search: {"match":"red apple"} (every word, any order), {"phrase":"red apple"} (the words side by side), {"prefix":"app"} (a word starting with it), {"contains":"d, app"} (a substring, also across words and punctuation). Case-insensitive; words are runs of letters and digits (Unicode); false for a missing or non-string field |
Strict types#
There is no coercion (decision D5). A comparison is true only if the field exists and has the literal's type. An integer literal never matches a float field and a number never matches text. A literal written without . or an exponent is an integer; 30.0 is a double.
Using the ten documents of conformance/filter-cases.json (the same file is run by the Rust executor and by the Studio's JavaScript filter), where the documents have age as 30 (integer, twice), "30" (text), 30.0 (double):
| Filter | Matches |
|---|---|
{"age":30} | the two documents with integer 30 |
{"age":30.0} | only the document with the double 30.0 |
{"age":"30"} | only the document with the text "30" |
{"score":10} | nothing (the stored 10.0 is a double) |
{"score":10.0} | the document with 10.0 |
Missing fields and null#
Unknown field names compile to id u32::MAX: nothing matches an equality on them, and $exists:false matches all. A stored null is a value of its own type; use {"note":null} to match it, and $exists to test presence.
Examples#
{"age":{"$gt":30},"active":true} age above 30 and active
{"city":{"$in":["Pune","Delhi"]}} one of two cities
{"address.zip":{"$gte":400000,"$lt":500000}} nested field, two operators ANDed
{"$or":[{"city":"Pune"},{"age":{"$lt":18}}]}
{"note":{"$exists":false}}Over HTTP the filter is a string inside the request body, so quotes are escaped:
POST /v1/query
{"text":"{\"n\":{\"$gte\":190}}","count":true} -> {"count":10}Over the binary protocol the filter text is the last field of Filter (see Wire protocol). In the Rust client c.count(r#"{"n":{"$gte":2990}}"#) returns the count and c.scan(filter, page) iterates all matches.
Limits#
The JSON reader has a depth limit of 32. Duplicate field names in one object are refused, integers must fit in 64 bits, and \uXXXX escapes (with surrogate pairs) are accepted in strings.
Indexes#
A secondary index narrows a filter when one of its required comparisons (Plan::required_leaves: comparisons reached through And nodes only) can use it. An index is used only when it points to at most about 3 % of the rows; otherwise a scan is faster and is used. Without an index every filter scans every document of the table; a scan runs at 67 M documents/s on 6 threads over 223 MB.
A full-text index on a field answers a required $text condition on it with exactly the rows the scan would return (D133), in microseconds instead of a scan (2 to 4 us per document for text search): for example 100,000 documents of 100 words, {"body":{"$text":{"match":"<common word>"}}} 0.13 ms instead of 190 ms. It is defined on the engine's table (Table::create_text_index in raftdb-db); the server creates one on a table field since D137 (the binary request TextIndex, or HTTP POST /v1/databases/{db}/tables/{t}/indexes with {"column":"body","kind":"fulltext"}; GET lists, DELETE with the same body drops), on the in-memory and the lsm engine; without an index $text runs as a scan.
SQL and JSON agree#
The SQL subset compiles into the same plan. The test in raftdb-exec checks that SQL selects exactly what the equivalent JSON filter selects over the conformance documents (29 pairs). a != 5 is NOT (a = 5), so rows without a match, like $ne.
Not supported yet#
$regex, and filter parameters (typed named parameters are only in the draft API).