Search for documents matching the given search query.
The result is a list of scored document IDs matching the query, sorted by
descending score, and each including data about which terms were matched and
in which fields.
// Search for "zen art motorcycle" with default options: terms have to match // exactly, and individual terms are joined with OR search(searchIndex, 'zen art motorcycle') // => [ { id: 2, score: 2.77258, match: { ... } }, { id: 4, score: 1.38629, match: { ... } } ]
// Search for "moto" with prefix search (it will match documents // containing terms that start with "moto" or "neuro") search(searchIndex, 'moto neuro', { prefix:true })
// Search for "ismael" with fuzzy search (it will match documents containing // terms similar to "ismael", with a maximum edit distance of 0.2 term.length // (rounded to nearest integer) search(searchIndex, 'ismael', { fuzzy:0.2 })
// Combine search terms with AND (to match only documents that contain both // "motorcycle" and "art") search(searchIndex, 'motorcycle art', { combineWith:'AND' })
There is also an AND_NOT combinator, that finds documents that match the
first term, but do not match any of the other terms. This combinator is
rarely useful with simple queries, and is meant to be used with advanced
query combinations (see later for more details).
// Filter only results in the 'fiction' category (assuming that 'category' // is a stored field) search(searchIndex, 'motorcycle art', { filter: (result) =>result.category === 'fiction' })
Searching for an empty string (assuming the default tokenizer) returns no
results. Sometimes though, one needs to match all documents, like in a
"wildcard" search. This is possible by passing the special value
wildcard as the query:
// Return search results for all documents search(index, WILDCARD)
Note that search options such as filter and boostDocument are still
applied, influencing which results are returned, and their order:
// Return search results for all documents in the 'fiction' category search(index, WILDCARD, { filter: (result) =>result.category === 'fiction' })
It is possible to combine different subqueries with OR, AND, and AND_NOT,
and even with different search options, by passing a query expression
tree object as the first argument, instead of a string.
// Search for documents that contain "zen" and ("motorcycle" or "archery") search(searchIndex, { combineWith:'AND', queries: [ 'zen', { combineWith:'OR', queries: ['motorcycle', 'archery'] } ] })
// Search for documents that contain ("apple" or "pear") but not "juice" and // not "tree" search(searchIndex, { combineWith:'AND_NOT', queries: [ { combineWith:'OR', queries: ['apple', 'pear'] }, 'juice', 'tree' ] })
Each node in the expression tree can be either a string, or an object that
supports all SearchOptions fields, plus a queries array field for
subqueries.
Note that, while this can become complicated to do by hand for complex or
deeply nested queries, it provides a formalized expression tree API for
external libraries that implement a parser for custom query languages.
Type Parameters
Document
ID
Field extends Record<string, any> = Partial<Document>
Search for documents matching the given search query.
The result is a list of scored document IDs matching the query, sorted by descending score, and each including data about which terms were matched and in which fields.
Basic usage:
Restrict search to specific fields:
Field boosting:
Prefix search:
Fuzzy search:
Combining strategies:
Advanced prefix and fuzzy search:
Combine with AND:
Combine with AND_NOT:
There is also an AND_NOT combinator, that finds documents that match the first term, but do not match any of the other terms. This combinator is rarely useful with simple queries, and is meant to be used with advanced query combinations (see later for more details).
Filtering results:
Wildcard query
Searching for an empty string (assuming the default tokenizer) returns no results. Sometimes though, one needs to match all documents, like in a "wildcard" search. This is possible by passing the special value
wildcard
as the query:Note that search options such as
filter
andboostDocument
are still applied, influencing which results are returned, and their order:Advanced combination of queries:
It is possible to combine different subqueries with OR, AND, and AND_NOT, and even with different search options, by passing a query expression tree object as the first argument, instead of a string.
Each node in the expression tree can be either a string, or an object that supports all
SearchOptions
fields, plus aqueries
array field for subqueries.Note that, while this can become complicated to do by hand for complex or deeply nested queries, it provides a formalized expression tree API for external libraries that implement a parser for custom query languages.