-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
229 lines (192 loc) · 5.96 KB
/
index.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import {
SearchResults,
Index,
Mappings,
TextFieldIndex,
NumberFieldIndex,
KeywordFieldIndex,
Analyzer,
Tokenizer,
QueryOptions,
SearchResultsHit
} from './interfaces'
import {
DEFAULT_ANALYZER,
DEFAULT_TOKENIZER,
DEFAULT_QUERY_OPTIONS
} from './constants'
import { scoreBM25F } from './utils/bm25f'
import { preprocessText } from './utils/preprocessing'
export * from './constants'
export * from './interfaces'
import * as _ from './utils/helper'
import KeywordField from './fields/keyword'
import NumberField from './fields/number'
import TextField from './fields/text'
import { evaluateFilter } from './utils/filter'
import { validateOptions } from './utils/options'
import { validateMappings } from './utils/mappings'
const getOriginalIdByInternalId = (index: Index, internalId: number): string =>
index.originalIds[internalId - index.internalIds.missing.filter((x: number) => x < internalId).length]
/**
* Function for building a search index that later can be used for querying.
*
* @param docs An array of documents / texts.
* @param options The search options to use for pre-processing.
*
* @returns A JSON-serializable object containing the search index to be used for subsequent queries. The raw documents are **not included** in the index and the provided `docs` array must be present without modificaton at query time. Depending on the size of the text corpus, the size of the index can very.
*/
export const createIndex = (mappings: Mappings): Index => {
const index: Index = {
length: 0,
mappings: validateMappings(mappings),
fields: {},
internalIds: { min: 0, max: -1, missing: [] },
originalIds: []
}
return index
}
export const indexDocument = (
index: Index,
doc: { [key: string]: any },
analyzer: Analyzer = DEFAULT_ANALYZER,
tokenizer: Tokenizer = DEFAULT_TOKENIZER
) => {
if (!(
typeof doc._id === 'string' && doc._id.length > 0 ||
typeof doc._id === 'number'
)) {
throw new Error('Missing id')
}
if (typeof index.originalIds[doc._id as any] !== 'undefined') {
throw new Error('Duplicate ID')
}
if (!index.originalIds) {
index.originalIds = []
}
index.originalIds.push(String(doc._id))
index.internalIds.max++
index.length++
const internalId = index.internalIds.max
for (const [field, value] of Object.entries(doc)) {
if (field === '_id') continue
const type = index.mappings[field]
if (type === 'text') {
if (!index.fields[field]) {
index.fields[field] = TextField.initialize()
}
TextField.indexDocument(
index.fields[field] as TextFieldIndex,
internalId,
value,
analyzer,
tokenizer
)
} else if (type === 'keyword') {
if (!index.fields[field]) {
index.fields[field] = KeywordField.initialize()
}
KeywordField.indexDocument(
index.fields[field] as KeywordFieldIndex,
internalId,
value
)
} else if (type === 'number') {
if (!index.fields[field]) {
index.fields[field] = NumberField.initialize()
}
NumberField.indexDocument(
index.fields[field] as NumberFieldIndex,
internalId,
value
)
} else {
throw new Error('invalid field')
}
}
}
/**
* Function querying an existing search index.
*
* @param query A word or sequence of words for searching the text corpus.
* @param index The search index build previously.
* @param options The **exactly same** options as provided to the function for creating the index.
* @param size The maximum amount of result items to return.
* @param offset The offset for returning search items, e.g., for using pagination.
*
* @returns Returns an array of matches sorted by scores descending (starting with the most relevant item).
*/
export const searchIndex = async (
index: Index,
query: string,
options: Partial<QueryOptions>,
analyzer: Analyzer = DEFAULT_ANALYZER,
tokenizer: Tokenizer = DEFAULT_TOKENIZER
): Promise<SearchResults> => {
// const analyzer = checkSearchOptions(options)
const optionsValid: QueryOptions = validateOptions(index, options)
let filteredDocumentIds: number[] | null = null
if (optionsValid.filter) {
filteredDocumentIds = evaluateFilter(index, optionsValid.filter)
}
if (query) {
let queryTokens = preprocessText(query, analyzer, tokenizer)
if (queryTokens.length === 0) {
return {
total: 0,
maxScore: 0,
hits: []
}
}
if (optionsValid.synonyms) {
queryTokens = [
...queryTokens,
...Object.entries(optionsValid.synonyms)
.filter(([token]) => queryTokens.includes(token))
.flatMap(([token, syns]) => syns)
]
}
queryTokens = [...new Set(queryTokens)]
let ranked = scoreBM25F(
queryTokens,
index,
optionsValid
)
if (filteredDocumentIds) {
// TODO: O(1) map?
ranked = ranked.filter(([docId]) => (filteredDocumentIds as number[]).includes(docId))
}
const hits: SearchResultsHit[] = []
const sourcePromises = []
if (optionsValid.getDocument) {
for (const [docId] of ranked.slice(optionsValid.offset, optionsValid.offset + optionsValid.size)) {
const originalId = getOriginalIdByInternalId(index, docId)
sourcePromises.push(optionsValid.getDocument(originalId))
}
}
const sources = await Promise.all(sourcePromises)
let i = 0
for (const [docId, _score] of ranked.slice(optionsValid.offset, optionsValid.offset + optionsValid.size)) {
const _source: any = optionsValid.getDocument ? sources[i] : null
const _id = getOriginalIdByInternalId(index, docId)
const hit: SearchResultsHit = {
_id,
_score,
_source
}
hits.push(hit)
i++
}
return {
total: ranked.length,
maxScore: hits.length > 0 ? hits[0]._score : 0,
hits
}
}
return {
total: 0,
maxScore: 0,
hits: []
}
}
export { DEFAULT_QUERY_OPTIONS, DEFAULT_ANALYZER }