The engine ranks the hits of a search by score until you ask for another order. On this page, you'll build one program that sorts a bicycle catalogue by field values, collapses the hits into groups, and pages through them by offset and by cursor. At the end of the page, two short programs browse the catalogue without a search term and pin a sponsored result in place. You can follow every step in TypeScript, Python, Go, or Rust, with all four packages printing the same output. The one exception is the sorted listing, which only the TypeScript package and the Go module accept. Each code block below shows a whole file, with green lines marking the lines that you add at each step and struck lines marking the lines that you remove.
Order the hits by field values
The sort option makes the engine order the hits by field values in place of their scores. When two hits have the same value in one field, the engine orders them by the next field in the sort. The engine can sort on any number, enum, or boolean field; however, it sorts on a text field only when the field's type includes sortable. The program declares maker as string:sortable in the schema, because the engine throws SEARCH_INVALID_FIELD for a sort on a plain string field.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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 } } },
sort: { maker: 'asc', price: 'asc' },
document: { include: ['maker', 'price'] },
limit: 6,
})
for (const hit of results.hits) {
console.log(hit.id, hit.document?.maker, hit.document?.price)
}
await narsil.shutdown()cargo-hauler cargo-bike-maker 3499
city-step city-bike-maker 549
urban-commuter city-bike-maker 749
folding-metro folding-bike-maker 899
folding-tourer folding-bike-maker 1150
gravel-explorer gravel-bike-maker 1299The engine orders the hits by maker and then by price within each maker, so the step-through city bicycle at 549 comes before the urban commuter at 749. The engine computes no score for a sorted query, so a sorted hit has a score only when you set includeScores: true. The engine compares text by its Unicode case fold, so apple sorts before Banana. Because the comparison uses no locale, every machine returns the same page. However, the engine leaves accents in place, so Ärla and Ćevapi sort after cevapi. The engine places a document that lacks the sorted field last, under asc and desc alike.
A sort can list at most eight fields, because the paging cursor stores one value for each of them. In TypeScript, write the sort as a list of { field, direction } entries whenever a field name is all digits, such as 2024, because JavaScript moves a key of that kind to the front of an object, which makes the engine sort by that field first. The Go module and the Rust crate accept only a list, while a Python dictionary preserves the order in which you write its keys.
Collapse the hits into groups
The group option makes the engine gather the hits that share values in the fields that you list. With maxPerGroup, you set how many hits the engine returns in each group. A reducer is a function that folds every hit of a group into one value. limit sets the most hits that the engine returns, while group.limit sets the most groups, so with limit: 0 the engine returns every group and no hits beside them.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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 } } },
sort: { maker: 'asc', price: 'asc' },
document: { include: ['maker', 'price'] },
limit: 6,
sort: { price: 'asc' },
group: {
fields: ['category'],
maxPerGroup: 1,
reduce: { initialValue: () => 0, reducer: (count) => Number(count) + 1 },
},
limit: 0,
})
for (const hit of results.hits) {
console.log(hit.id, hit.document?.maker, hit.document?.price)
}
for (const group of results.groups ?? []) {
const ids = group.hits.map((hit) => hit.id)
console.log(`${group.values.category}: ${ids.join(', ')} (${group.reduced} in stock)`)
}
await narsil.shutdown()folding: folding-metro (2 in stock)
city: city-step (2 in stock)
kids: kids-first (2 in stock)
road: road-endurance (2 in stock)
cargo: cargo-hauler (1 in stock)
gravel: gravel-explorer (1 in stock)The engine sorts the hits by price before it groups them, so the one hit that the engine returns in each group is the cheapest bicycle in stock in that category. However, the engine passes every hit of a group to the reducer, which is why the counts reach 2 while each group contains one bicycle. When you leave maxPerGroup out of a query, the engine returns one hit for each group. When the hits of a group lack a field that you group by, the group's values contain null for that field, which is nil in Go and None in Python and Rust. When the reducer throws, the engine puts the error message in the group's reducerError field and returns no reduced value for that group. The reducer is a function in the calling program, so the HTTP server refuses a query that includes one.
Page through the results
To fetch a page, set limit together with either offset, which skips that many hits, or searchAfter, which takes the cursor from the previous page. The program stores the shared options in one value, so that it sends the same query for every page.
import { createNarsil } from '@delali/narsil'
import { createNarsil, type QueryParams } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
const page: QueryParams = {
term: 'bicycle',
filters: { fields: { inStock: { eq: true } } },
sort: { price: 'asc' },
group: {
fields: ['category'],
maxPerGroup: 1,
reduce: { initialValue: () => 0, reducer: (count) => Number(count) + 1 },
},
limit: 0,
})
document: false,
limit: 4,
}
const first = await narsil.query('bicycles', page)
const secondByOffset = await narsil.query('bicycles', { ...page, offset: 4 })
const secondByCursor = await narsil.query('bicycles', { ...page, searchAfter: first.cursor })
const third = await narsil.query('bicycles', { ...page, searchAfter: secondByCursor.cursor })
for (const group of results.groups ?? []) {
const ids = group.hits.map((hit) => hit.id)
console.log(`${group.values.category}: ${ids.join(', ')} (${group.reduced} in stock)`)
}
const ids = (hits: Array<{ id: string }>): string => hits.map((hit) => hit.id).join(', ')
console.log('count', first.count)
console.log('page 1:', ids(first.hits))
console.log('page 2 by offset:', ids(secondByOffset.hits))
console.log('page 2 by cursor:', ids(secondByCursor.hits))
console.log('page 3 by cursor:', ids(third.hits))
console.log('cursor after page 3:', third.cursor === undefined ? 'none' : 'present')
await narsil.shutdown()count 10
page 1: kids-first, kids-junior, city-step, urban-commuter
page 2 by offset: folding-metro, folding-tourer, gravel-explorer, road-endurance
page 2 by cursor: folding-metro, folding-tourer, gravel-explorer, road-endurance
page 3 by cursor: cargo-hauler, road-racer
cursor after page 3: noneThe engine returns the same four bicycles on the second page whether you page by offset or by cursor, while count is 10, which is the number of hits that match. The engine returns the last page without a cursor, so stop paging there. That missing cursor is undefined in TypeScript, None in Python and Rust, and an empty string in Go. The engine ties each cursor to the query that you send for that page, so it throws SEARCH_INVALID_CURSOR when you send the cursor with a different term, filter, or sort. You can change the limit between pages, however. Paging by offset reaches at most 10,000 hits, because the engine throws SEARCH_RESULT_WINDOW_EXCEEDED when offset plus limit passes 10,000. With a cursor, you can page a keyword search past that point.
Browse an index in any order
A listing pages through an index without a search term. In TypeScript and Go, you can pass a listing the same filters, sort, and document options that you pass a query. There is more on the listing in id order, including what it returns while writes continue, in Indexes and documents. The program below lists the bicycles in stock from the most expensive down. The Python package refuses sort on a listing with CONFIG_INVALID, while the Rust crate's ListParams has no sort field. In those two languages, the engine always returns a listing in id order.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const listing = await narsil.listDocuments('bicycles', {
filters: { fields: { inStock: { eq: true } } },
sort: { price: 'desc' },
document: { include: ['price'] },
limit: 3,
})
console.log('total', listing.total)
for (const entry of listing.documents) {
console.log(entry.id, entry.document.price)
}
console.log('next cursor:', listing.cursor === undefined ? 'none' : 'present')
await narsil.shutdown()total 10
road-racer 4250
cargo-hauler 3499
road-endurance 2799
next cursor: presenttotal is 10, because ten bicycles match the filter, while the page contains the three most expensive of them along with a cursor for the next three. The engine ties that cursor to the sort as well, so send the same sort with it. When you leave the sort out of a listing, the engine returns the documents in id order.
Pin a result in place
The pinned option makes the engine place a document at a fixed position in the results, counting from 0. A shop can use it to put a sponsored or editorial pick at the top of a search. The program below pins the cargo hauler to the first position on a search for gravel.
import { createNarsil } from '@delali/narsil'
const narsil = await createNarsil()
await narsil.createIndex('bicycles', {
schema: {
name: 'string',
summary: 'string',
maker: 'string:sortable',
category: 'enum',
price: 'number',
rating: 'number',
inStock: 'boolean',
tags: 'string[]',
},
})
await narsil.insertBatch('bicycles', [
{ id: 'urban-commuter', name: 'Urban commuter', summary: 'A lightweight aluminium commuter bicycle with hydraulic disc brakes.', maker: 'city-bike-maker', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', 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', category: 'kids', price: 329, rating: 4.1, inStock: true, tags: ['kids', 'aluminium'] },
])
const results = await narsil.query('bicycles', {
term: 'gravel',
filters: { fields: { inStock: { eq: true } } },
pinned: [{ docId: 'cargo-hauler', position: 0 }],
document: false,
})
console.log('count', results.count)
for (const hit of results.hits) {
console.log(hit.id, (hit.score ?? 0).toFixed(2))
}
await narsil.shutdown()count 2
cargo-hauler 0.00
gravel-explorer 6.67The engine places the cargo hauler first, although its document contains no word gravel. The gravel explorer comes second, with a score of 6.67. count is 2, because the engine counts a pinned document along with the documents that match the search. When you work out the number of pages from count, that number includes the pin. The engine counts each position from the top of the whole result set, so a page that you reach with searchAfter contains no pin.
Where to go next
- Read Filters and facets for the filters that narrow the hits before you sort, group, or page them.
- Read Indexes and documents for the listing in id order.
- Read Hybrid search for a search in which the engine fuses keyword and vector rankings into one order.