forked from lucaong/minisearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.ts
182 lines (175 loc) · 5.23 KB
/
search.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { type SearchIndex } from "./SearchIndex.js";
import { executeQuery } from "./results.js";
import {
type Query,
type SearchOptions,
type SearchResult,
} from "./typings.js";
import { byScore } from "./utils.js";
/**
* 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:
*
* ```js
* // 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: { ... } } ]
* ```
*
* ### Restrict search to specific fields:
*
* ```js
* // Search only in the 'title' field
* search(searchIndex, 'zen', { fields: ['title'] })
* ```
*
* ### Field boosting:
*
* ```js
* // Boost a field
* search(searchIndex, 'zen', { boost: { title: 2 } })
* ```
*
* ### Prefix search:
*
* ```js
* // Search for "moto" with prefix search (it will match documents
* // containing terms that start with "moto" or "neuro")
* search(searchIndex, 'moto neuro', { prefix: true })
* ```
*
* ### Fuzzy search:
*
* ```js
* // 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 })
* ```
*
* ### Combining strategies:
*
* ```js
* // Mix of exact match, prefix search, and fuzzy search
* search(searchIndex, 'ismael mob', {
* prefix: true,
* fuzzy: 0.2
* })
* ```
*
* ### Advanced prefix and fuzzy search:
*
* ```js
* // Perform fuzzy and prefix search depending on the search term. Here
* // performing prefix and fuzzy search only on terms longer than 3 characters
* search(searchIndex, 'ismael mob', {
* prefix: term => term.length > 3
* fuzzy: term => term.length > 3 ? 0.2 : null
* })
* ```
*
* ### Combine with AND:
*
* ```js
* // Combine search terms with AND (to match only documents that contain both
* // "motorcycle" and "art")
* search(searchIndex, 'motorcycle art', { combineWith: '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:
*
* ```js
* // Filter only results in the 'fiction' category (assuming that 'category'
* // is a stored field)
* search(searchIndex, 'motorcycle art', {
* filter: (result) => result.category === 'fiction'
* })
* ```
*
* ### 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.
*
* ```js
* // 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.
*
* @param searchIndex Search Index
* @param query Search query
* @param options Search options. Each option, if not given, defaults to the corresponding value of `searchOptions` given to the constructor, or to the library default.
*/
export const search = <
Document,
ID,
Field extends Record<string, any> = Partial<Document>,
>(
searchIndex: SearchIndex<Document, ID>,
query: Query,
searchOptions: SearchOptions<ID> = {},
): SearchResult<ID, Field>[] => {
const combinedResults = executeQuery(searchIndex, query, searchOptions);
const results: SearchResult<ID, Field>[] = [];
for (const [docId, { score, terms, match }] of combinedResults) {
// Final score takes into account the number of matching QUERY terms.
// The end user will only receive the MATCHED terms.
const quality = terms.length;
const result = {
id: searchIndex._documentIds.get(docId)!,
score: score * quality,
terms: Object.keys(match),
match,
};
Object.assign(result, searchIndex._storedFields.get(docId));
if (searchOptions.filter == null || searchOptions.filter(result))
results.push(<SearchResult<ID, Field>>result);
}
results.sort(byScore);
return results;
};