Skip to content

searchKeys

searchKeys(query, keyTexts, weights, options?): KeySearchResult[]

Perform fuzzy search across multiple text keys with weights.

keyTexts[k] is an array of strings for key k, one per item. All inner arrays must have the same length (the number of items). weights specifies the relative importance of each key.

By default every key is scored like search() scores an item, against the whole query; with matchMode: 'crossKey' every term of the query is matched against the keys on its own, so the terms may match different keys (see KeySearchOptions.matchMode). keyScores holds the key scores for every key, including keys whose weight is 0. By default the combined score is sum(keyScore * weight) / sum(weights); the scoreMode option selects another way to combine them (see KeySearchOptions.scoreMode). Items whose combined score is 0 (no match on a key with a positive weight) are not returned.

Returns results sorted by combined score (best match first), then by the UTF-8 byte length of the best-matching key’s text (shorter first, like search()), then by index. new KeyedFuzzyIndex(keyTexts, weights) returns exactly the same results.

The fourth argument accepts either a number (maxResults) or a KeySearchOptions object. Throws when the key texts have different lengths, when there is not exactly one weight per key, or when a weight is negative, NaN or infinite, or the weights sum to 0 or to Infinity (the same errors as the KeyedFuzzyIndex constructor), and for an unknown scoreMode or matchMode.

Parameter Type
query string
keyTexts readonly (readonly string[])[]
weights readonly number[]
options? number | KeySearchOptions | null

KeySearchResult[]