With a filter, the engine returns only the documents whose fields pass a test. With a facet, it counts how many of the matching documents contain each value of a field, which is the number that a shop's filter sidebar shows beside each option. On this page, you'll build one file on a catalogue of twelve bicycles and change its search step by step. Every test on this page works in all four packages, which print the same output. Each code block below shows the whole file, with green lines marking the lines that you add at each step and struck lines marking the lines that you remove.
Search the whole catalogue
Every summary in the catalogue contains the word bicycle, so a search for it matches all twelve bicycles. The program prints the number of matches and then the id of each hit, so that you can see which hits each filter below removes.
The engine answers a test on a field only when the type of the field supports that test, which is why three fields here take an extra option. maker is a string:pattern field and tags is a string[]:pattern field, because the engine compares the whole value of a text field only when it builds a pattern index for that field. A pattern index maps every run of three characters to the documents that contain it. sku, the stock code, is a verbatim field, which the engine stores exactly as you write it and never splits into words. Each code starts with three letters for the category, such as CTY for city. The sortable option on the field lets the engine compare codes by their order.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 12
folding-metro
folding-tourer
gravel-allroad
city-step
kids-first
kids-junior
road-endurance
road-racer
cargo-trike
cargo-haulercount is 12, the number of matches, while hits contains ten, because the engine returns ten hits when a search leaves out limit.
Return only the documents whose fields pass a test
filters.fields maps each field to its tests, which the engine applies together, so a document has to pass every test before the engine returns it. In this step, the engine returns the bicycles in stock that cost from 500 to 1,500 and whose tags include commuter or gravel.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: {
fields: {
inStock: { eq: true },
price: { between: [500, 1500] },
tags: { matchesAny: ['commuter', 'gravel'] },
},
},
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 4
folding-metro
city-step
gravel-explorer
urban-commuterFour bicycles pass all three tests. The allroad gravel bicycle fails inStock, while the tags of the folding tourer include neither commuter nor gravel. Every other bicycle costs less than 500 or more than 1,500.
The engine answers every test from an index that it builds for the field, so on each field type it answers only the tests in this table.
| Field type | Tests that the engine answers |
|---|---|
number | eq, ne, gt, gte, lt, lte, and between |
boolean | eq and ne |
enum | eq, ne, in, nin, startsWith, and endsWith |
string:pattern or verbatim | eq, ne, in, nin, startsWith, endsWith, contains, wildcard, and regex, each with an optional caseFold, and tolerance on eq |
string[]:pattern or verbatim[] | eq, ne, in, nin, startsWith, endsWith, contains, wildcard, and regex on each element, together with matchesAny, containsAll, and size |
a single string or verbatim field whose type includes sortable | gt, gte, lt, lte, and between with text bounds |
| any declared field | exists, notExists, isEmpty, and isNotEmpty |
For a string or string[] field whose type leaves out pattern, the engine builds only an index of its words. It therefore answers the presence tests on that field, and the text bounds as well when the field is a single field whose type includes sortable. For a whole-value test such as eq or matchesAny on such a field, the engine throws SEARCH_INVALID_FILTER with a message that tells you to declare the field as string:pattern in a new index. A full-text search matches the words of a string:pattern field in the same way as those of a string field.
The engine throws SEARCH_INVALID_FILTER in each of these cases:
- The filter contains an unknown operator.
- The filter contains a field name outside
fields. - The filter contains a test that the engine doesn't answer for the type of the field, such as
startsWithon anumberfield orinon abooleanfield. - The filter contains an operand of the wrong shape, such as
eq: '700'on anumberfield or abetweenwith one bound. - The filter contains more than 1,024 tests, or an
in,nin,matchesAny, orcontainsAlllist of more than 65,536 values.
For a test on a field that the schema doesn't declare, the engine throws SEARCH_INVALID_FIELD on every index, because it builds no index for such a field.
Combine tests with and, or, and not
and, or, and not each take whole filter expressions, so you can nest one expression inside another. In this step, the engine returns the bicycles in stock that are gravel or road bicycles or that have a rating of 4.5 or more. It then drops every bicycle whose maker starts with road.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: {
fields: {
inStock: { eq: true },
price: { between: [500, 1500] },
tags: { matchesAny: ['commuter', 'gravel'] },
},
and: [
{ fields: { inStock: { eq: true } } },
{ or: [{ fields: { category: { in: ['gravel', 'road'] } } }, { fields: { rating: { gte: 4.5 } } }] },
{ not: { fields: { maker: { startsWith: 'road' } } } },
],
},
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 3
cargo-hauler
gravel-explorer
urban-commuterThree bicycles pass. The gravel explorer passes on its category and on its rating of 4.8, while the cargo hauler and the urban commuter pass on their ratings of 4.6 and 4.5. Both bicycles from the road-bike-maker pass the or on their category, but the engine then removes them under the not. Each and or or takes a list of expressions, which can nest up to 30 levels deep.
Match part of a value
With contains, the engine returns the documents whose value contains the text anywhere inside it. With caseFold: true, the engine treats upper and lower case as the same when it compares the two texts. Both work only on a pattern field, which is a verbatim field or a text field whose type includes pattern. In this step, the engine returns the bicycles whose maker contains Fold in any case.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: {
and: [
{ fields: { inStock: { eq: true } } },
{ or: [{ fields: { category: { in: ['gravel', 'road'] } } }, { fields: { rating: { gte: 4.5 } } }] },
{ not: { fields: { maker: { startsWith: 'road' } } } },
],
},
filters: { fields: { maker: { contains: 'Fold', caseFold: true } } },
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 2
folding-metro
folding-tourerBoth bicycles from the folding-bike-maker pass, because their maker contains fold once the engine folds the case of both texts. Without caseFold, the same test matches no document, since the engine then compares the characters exactly and the maker contains fold with a lower-case f. The engine folds case by the Unicode rules, so a test for strasse under caseFold matches Straße. However, the engine applies no Unicode normalisation, so it treats café with a precomposed é and café with a combining accent as different values. If your app receives both forms, normalise the text before you insert it.
For a contains test, the engine picks up to 16 runs of three characters from the text and takes the documents of the rarest run from the pattern index as candidates. It then checks each candidate against the whole text. A text shorter than three characters contains no run, so for such a text the engine checks every document that has a value in the field. A browser bundle includes the pattern code only when the app imports patternSearch from @delali/narsil/pattern and passes it to registerPatternSearch before it creates or loads an index with a pattern field. On Node.js and in the other three languages, the engine loads that code by itself.
Match a value against a pattern
With regex, the engine returns the documents whose value contains a match of a regular expression, while with wildcard it returns those whose whole value matches a pattern. In this step, the engine returns the bicycles whose stock code matches -<2023-2024>$, which means that the code ends in a model year from 2023 to 2024.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: { fields: { maker: { contains: 'Fold', caseFold: true } } },
filters: { fields: { sku: { regex: '-<2023-2024>$' } } },
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 8
folding-tourer
gravel-allroad
kids-first
road-endurance
road-racer
cargo-hauler
gravel-explorer
urban-commuterEight bicycles have a 2023 or a 2024 code, while the engine drops the four from 2021 and 2022. In the syntax of Narsil, <2023-2024> matches the digits of any whole number from 2023 to 2024. $ anchors the match to the end of the value, because a regular expression otherwise matches any part of the value. The syntax supports sets such as [A-Z], the classes \d, \w, and \s, alternatives with |, groups, the repeats *, +, ?, and {n,m}, the anchors ^ and $, and the word boundary \b. The engine refuses a back-reference, a lookahead, a lookbehind, and a lazy repeat with SEARCH_INVALID_FILTER, because its matcher never backtracks. A check of one value therefore takes at most as many steps as the number of characters in the value multiplied by the number of positions in the pattern.
A wildcard pattern has to match the whole value, with * standing for any run of characters and ? standing for exactly one character, so the engine returns the five bicycles from 2024 under { wildcard: '*-2024' }. Write \ before *, ?, or \ to match that character itself. The engine refuses a pattern or a text of more than 1,024 code points. It also limits the work that the pattern tests of one search can do to the patternWorkCap engine setting, which defaults to 25,000,000 units, and it throws SEARCH_WORK_CAP_EXCEEDED when a search goes beyond that limit.
Find a code through a typo
A tolerance on an eq test of a pattern field lets the test match a value within that many edits of the text. An edit inserts, deletes, or replaces one character. In this step, the filter contains a mistyped stock code, with the letter O in place of the zero in 2024.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: { fields: { sku: { regex: '-<2023-2024>$' } } },
filters: { fields: { sku: { eq: 'GRV-EXP-2O24', tolerance: 1 } } },
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 1
gravel-explorerThe mistyped code is one replacement away from GRV-EXP-2024, so the gravel explorer passes, while the same test without tolerance matches no document. A swap of two neighbouring characters counts as two edits. A tolerance can be 0, 1, 2, or 'auto'. With caseFold, the engine counts the edits between the case folds of the two texts. The tolerance of a search applies only to its words and leaves its filters exact, as in the examples on Full-text search.
The engine takes its candidates from the pattern index. A value within k edits of the text has to share at least one of k × (2 + f) + 1 distinct runs of three characters with the text, where f is 1 for most text and 2 for a text that contains a character such as ß that folds to two characters. A tolerance of k therefore requires the text to contain that many distinct runs. The 12-character code here contains 10 distinct runs, so it accepts a tolerance of 1 or 2, while the five-character text appel contains 3 runs and accepts neither. For a text with too few runs, the engine throws SEARCH_INVALID_FILTER with a message that gives the number of runs that the tolerance requires. Under 'auto', the engine uses the largest tolerance for which the text contains enough runs, which can be 0, so 'auto' never raises that error. The engine also throws SEARCH_INVALID_FILTER for a tolerance on a field that is not a pattern field and for a tolerance in a filter that sets no eq test.
Compare text by its order and its start
On a single string or verbatim field whose type includes sortable, the engine also answers gt, gte, lt, lte, and between with text bounds. On an enum field, it answers startsWith and endsWith. In this step, the engine returns the bicycles whose stock code is from CTY up to but not including GRV, and whose category starts with c.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: { fields: { sku: { eq: 'GRV-EXP-2O24', tolerance: 1 } } },
filters: { fields: { sku: { gte: 'CTY', lt: 'GRV' }, category: { startsWith: 'c' } } },
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
await narsil.shutdown()count 2
city-step
urban-commuterThe codes from CTY up to GRV are the four city and folding codes, since a code such as GRV-ALR-2023 sorts after the bound GRV. The categories that start with c are city and cargo, so only the two city bicycles pass both tests. The engine sorts text by its Unicode case fold first, and by its raw characters only when two folds are equal, so gte: 'apple' matches apple and Banana but not Apple, which sorts before apple. It answers a text bound from a column of sorted values, which it builds for sorting the field on the first range test or sort on that field. The engine throws SEARCH_INVALID_FILTER for a text bound on any other field, including a string[]:sortable field, because it sorts the list of each document by a single element.
On an enum field, the engine finds the matches of startsWith and endsWith through a sorted list of the distinct values of the field, so the engine returns the two folding bicycles under endsWith: 'ing'. It compares the characters of an enum value exactly. It refuses caseFold, contains, and the text bounds on an enum field with SEARCH_INVALID_FILTER.
Count each value for a filter sidebar
facets lists the fields to count. The engine counts every document that matches the term and the filters, whatever limit you set, so a search with limit: 0 returns only the counts. A number field takes ranges in place of single values, while a text or enum field takes a limit and a sort.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:pattern',
sku: 'verbatim:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]:pattern',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', sku: 'CTY-URB-2024', category: 'city', price: 749, rating: 4.5, inStock: true, tags: ['commuter', 'aluminium'] },
{ id: 'city-step', name: 'Step-through city bicycle', summary: 'A step-through city bicycle with a chain guard and a hub dynamo.', maker: 'city-bike-maker', sku: 'CTY-STP-2022', category: 'city', price: 549, rating: 4.0, inStock: true, tags: ['commuter', 'steel'] },
{ id: 'gravel-explorer', name: 'Gravel explorer', summary: 'A steel gravel bicycle built for long unpaved rides.', maker: 'gravel-bike-maker', sku: 'GRV-EXP-2024', category: 'gravel', price: 1299, rating: 4.8, inStock: true, tags: ['gravel', 'steel'] },
{ id: 'gravel-allroad', name: 'Allroad gravel bicycle', summary: 'An aluminium gravel bicycle with mounts for three bottles.', maker: 'gravel-bike-maker', sku: 'GRV-ALR-2023', category: 'gravel', price: 999, rating: 4.4, inStock: false, tags: ['gravel', 'aluminium'] },
{ id: 'folding-metro', name: 'Metro folding bicycle', summary: 'A folding bicycle that fits under a train seat.', maker: 'folding-bike-maker', sku: 'FLD-MET-2021', category: 'folding', price: 899, rating: 4.2, inStock: true, tags: ['folding', 'commuter'] },
{ id: 'folding-tourer', name: 'Tourer folding bicycle', summary: 'A folding bicycle with a rear rack and eight gears.', maker: 'folding-bike-maker', sku: 'FLD-TOU-2024', category: 'folding', price: 1150, rating: 4.1, inStock: true, tags: ['folding', 'aluminium'] },
{ id: 'cargo-hauler', name: 'Cargo hauler', summary: 'An electric cargo bicycle rated to carry two children.', maker: 'cargo-bike-maker', sku: 'CRG-HAU-2023', category: 'cargo', price: 3499, rating: 4.6, inStock: true, tags: ['cargo', 'electric'] },
{ id: 'cargo-trike', name: 'Cargo trike', summary: 'A three-wheeled cargo bicycle with a lockable box.', maker: 'cargo-bike-maker', sku: 'CRG-TRK-2022', category: 'cargo', price: 4195, rating: 4.3, inStock: false, tags: ['cargo', 'electric'] },
{ id: 'road-racer', name: 'Road racer', summary: 'A carbon road bicycle with electronic shifting.', maker: 'road-bike-maker', sku: 'ROD-RAC-2024', category: 'road', price: 4250, rating: 4.9, inStock: true, tags: ['road', 'carbon'] },
{ id: 'road-endurance', name: 'Endurance road bicycle', summary: 'A carbon road bicycle tuned for comfort over long distances.', maker: 'road-bike-maker', sku: 'ROD-END-2023', category: 'road', price: 2799, rating: 4.7, inStock: true, tags: ['road', 'carbon'] },
{ id: 'kids-first', name: 'First ride bicycle', summary: 'A first bicycle for a child, with stabilisers that come off in one minute.', maker: 'kids-bike-maker', sku: 'KID-FST-2024', category: 'kids', price: 189, rating: 4.3, inStock: true, tags: ['kids'] },
{ id: 'kids-junior', name: 'Junior mountain bicycle', summary: 'A junior mountain bicycle with a suspension fork and six gears.', maker: 'kids-bike-maker', sku: 'KID-JNR-2022', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'bicycle',
filters: { fields: { sku: { gte: 'CTY', lt: 'GRV' }, category: { startsWith: 'c' } } },
filters: { fields: { inStock: { eq: true } } },
facets: {
category: {},
maker: { limit: 3 },
tags: {},
price: { ranges: [{ from: 0, to: 500 }, { from: 500, to: 1500 }, { from: 1500, to: 5000 }] },
},
document: false,
limit: 0,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id)
}
for (const [field, facet] of Object.entries(results.facets ?? {})) {
const counts = Object.entries(facet.values).map(([value, count]) => `${value} ${count}`)
console.log(`${field}: ${counts.join(', ')} (error bound ${facet.errorBound})`)
}
await narsil.shutdown()category: city 2, folding 2, kids 2, road 2, cargo 1, gravel 1 (error bound 0)
maker: city-bike-maker 2, folding-bike-maker 2, kids-bike-maker 2 (error bound 2)
tags: aluminium 3, commuter 3, carbon 2, folding 2, kids 2, road 2, steel 2, cargo 1, electric 1, gravel 1 (error bound 0)
price: 500-1500 5, 1500-5000 3, 0-500 2 (error bound 0)For each facet, the engine counts the ten bicycles in stock and returns the most frequent value first. It counts each entry of the tags list separately, while it counts each whole value of maker as one value, never as separate words. It counts a plain string field in the same way. Four makers have two bicycles each in stock, so the engine leaves one of them out under maker: { limit: 3 }. The error bound of 2 means that no maker that the engine leaves out has more than 2 bicycles in stock.
The counts are exact on an index that you split into partitions. On an index of 400 documents from 8 makers, with 50 documents for each maker over four partitions, the engine returns three makers at 50 each under limit: 3, with an error bound of 50. However, when worker copies or cluster nodes share the counting, each of them returns only its top values, so the count for a value that is common overall but rare on one of them can fall short by up to the error bound.
Where to go next
- Read Sorting, grouping, and pagination to order the filtered hits by a field, to collapse them into groups, and to page through them.
- Read Indexes and documents for the listing that pages through an index without a search term, which takes the same filters.
- Read Vector search to search by meaning with the same filter expression.