Filters
Learn how to use filters in ZBSearch search.
You can use the filters interface to filter the search results.
Filters are available for numeric, boolean, string, enum, and geopoint properties. Depending on the type of the property, you can use different operators.
String operators
On string properties it performs an exact matching on tokens so it is advised to disable stemming for the properties
you want to use filters on (when using the default tokenizer you can provide the stemmerSkipProperties configuration property).
If we consider the following schema:
const db = create({
schema: {
title: "string",
tag: "string",
},
components: {
tokenizer: {
stemming: true,
stemmerSkipProperties: ["tag"],
},
},
});
const results = search(db, {
term: "prestige",
where: {
tag: "new",
},
});The results will contain all documents that contain the word prestige in the title property and have tags property equal to new.
You can also specify a list of string, in this case it will return all documents that contain at least one of the values provided:
const results = search(db, {
term: "prestige",
where: {
tag: ["favorite", "new"],
},
});Number operators
The number properties support the following operators:
| Operator | Description | Example |
|---|---|---|
gt | Greater than | year: { gt: 2000 } |
gte | Greater than or equal to | year: { gte: 2000 } |
lt | Less than | year: { lt: 2000 } |
lte | Less than or equal to | year: { lte: 2000 } |
eq | Equal to | year: { eq: 2000 } |
between | Between two values (inclusive) | year: { between: [2000, 2008] } |
const db = create({
schema: {
id: "string",
title: "string",
year: "number",
meta: {
rating: "number",
length: "number",
favorite: "boolean",
tags: "string",
},
},
components: {
tokenizer: {
stemming: true,
stemmerSkipProperties: ["meta.tags"],
},
},
});
const results = search(db, {
term: "prestige",
where: {
year: {
gte: 2000,
},
"meta.rating": {
between: [5, 10],
},
"meta.length": {
lte: 60,
},
},
});Boolean operators
For boolean properties, you can simply set the property to true or false:
const results = search(db, {
term: "prestige",
where: {
"meta.favorite": true,
},
});String[] | Number[] | Boolean[] operators
The available operators depend on the type (string, number of boolean) as described in the previous sections. A document matches if at least one of the array elements matches the filter condition.
const db = create({
schema: {
title: "string",
tags: "string[]",
editions: "number[]",
limited: "boolean[]",
}
});
insertMultiple(db, [
{title: "a", tags: ["foo", "bar"], editions: [1990, 2024], limited: [false, false]},
{title: "b", tags: ["foo"], editions: [1942, 2024], limited: [false, true]},
{title: "c", tags: ["bar"], editions: [2020], limited: [false]},
])
// Books with tag foo
search(db, {where: {tags: "foo"}}); // returns a, b
// Books tagged either as foo or bar
search(db, {where: {tags: ["foo", "bar"]}}); // returns a, b, c
// Books with a 2024 edition
search(db, {where: {editions: {eq: 2024}}}); // returns a, b
// Books with a limited edition
search(db, {where: {limited: true}}); // returns bEnum operators
The enum properties support the following operators:
| Operator | Description | Example |
|---|---|---|
eq | Equal to | genre: { eq: 'drama' } |
in | Contained in the given array | genre: { in: ['drama', 'horror'] } |
nin | Not contained in the given array | genre: { nin: ['comedy'] } |
Enum[] operators
The enum properties support the following operators:
| Operator | Description | Example |
|---|---|---|
containsAll | Contains all the given values | genre: { containsAll: ['comedy', 'action'] } |
containsAny | Contains any of the given values | genre: { containsAny: ['comedy', 'action', 'horror'] } |
Geosearch
Starting from ZBSearch v2.0.0, you can perform geosearch queries.
Even though the APIs are very simple, we decided to dedicate a separate section for them. This lets us explain the concepts behind the geosearch and how it works with more details.