Skip to content

Commit

Permalink
Add queryTerms to the search results (#241)
Browse files Browse the repository at this point in the history
The `queryTerms` field is useful to know which query terms matched in
the search result. When performing an exact search, `terms` and
`queryTerms` are identical. In case of prefix or fuzzy match though,
they can be different.

For example, if one searches for "moto" using prefix search, and a
result matches with the word "motorcycle", then for that result the
`terms` array will include "motorcycle", while the `queryTerms` array
will include "moto".
  • Loading branch information
lucaong authored Nov 22, 2023
1 parent c3101a3 commit 0a5e9fa
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/MiniSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,10 @@ describe('MiniSearch', () => {
['vita', 'nova'],
['vita']
])
expect(results.map(({ queryTerms }) => queryTerms)).toEqual([
['vita', 'nova'],
['vita']
])
})

it('reports correct info when combining terms with AND', () => {
Expand All @@ -1359,6 +1363,9 @@ describe('MiniSearch', () => {
expect(results.map(({ terms }) => terms)).toEqual([
['vita', 'nova']
])
expect(results.map(({ queryTerms }) => queryTerms)).toEqual([
['vita', 'nova']
])
})

it('reports correct info for fuzzy and prefix queries', () => {
Expand All @@ -1371,6 +1378,10 @@ describe('MiniSearch', () => {
['vita', 'nova'],
['vita']
])
expect(results.map(({ queryTerms }) => queryTerms)).toEqual([
['vi', 'nuova'],
['vi']
])
})

it('reports correct info for many fuzzy and prefix queries', () => {
Expand All @@ -1385,6 +1396,11 @@ describe('MiniSearch', () => {
['vita', 'mezzo', 'del'],
['del']
])
expect(results.map(({ queryTerms }) => queryTerms)).toEqual([
['vi', 'nuova', 'm', 'de'],
['vi', 'm', 'de'],
['de']
])
})

it('passes only the query to tokenize', () => {
Expand Down
16 changes: 13 additions & 3 deletions src/MiniSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,17 @@ export type SearchResult = {
id: any,

/**
* List of terms that matched
* List of document terms that matched. For example, if a prefix search for
* `"moto"` matches `"motorcycle"`, `terms` will contain `"motorcycle"`.
*/
terms: string[],

/**
* List of query terms that matched. For example, if a prefix search for
* `"moto"` matches `"motorcycle"`, `queryTerms` will contain `"moto"`.
*/
queryTerms: string[],

/**
* Score of the search results
*/
Expand Down Expand Up @@ -1229,14 +1236,17 @@ export default class MiniSearch<T = any> {
const results = []

for (const [docId, { score, terms, match }] of rawResults) {
// Final score takes into account the number of matching QUERY terms.
// The end user will only receive the MATCHED terms.
// terms are the matched query terms, which will be returned to the user
// as queryTerms. The quality is calculated based on them, as opposed to
// the matched terms in the document (which can be different due to
// prefix and fuzzy match)
const quality = terms.length || 1

const result = {
id: this._documentIds.get(docId),
score: score * quality,
terms: Object.keys(match),
queryTerms: terms,
match
}

Expand Down

0 comments on commit 0a5e9fa

Please sign in to comment.