-
Notifications
You must be signed in to change notification settings - Fork 13
/
search.js
407 lines (370 loc) · 10.8 KB
/
search.js
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
'use strict';
const elasticsearch = require('elasticsearch');
const config = require('config');
const debug = require('./util/debug');
const client = new elasticsearch.Client({
host: `${config.search.host}:${config.search.port}`,
log: config.search.log
});
const mlString = require('./models/helpers/ml-string');
const languages = require('./locales/languages');
// All supported stemmers as of ElasticSearch 5.2.0
let analyzers = {
ar: 'arabic',
hy: 'armenian',
eu: 'basque',
bn: 'bengali',
pt: 'brazilian',
bg: 'bulgarian',
ca: 'catalan',
zh: 'cjk',
'zh-Hant': 'cjk',
cs: 'czech',
da: 'danish',
nl: 'dutch',
en: 'english',
et: 'estonian',
fi: 'finnish',
fr: 'french',
gl: 'galician',
de: 'german',
el: 'greek',
hi: 'hindi',
hu: 'hungarian',
id: 'indonesian',
ga: 'irish',
it: 'italian',
lv: 'latvian',
lt: 'lithuanian',
no: 'norwegian',
fa: 'persian',
'pt-PT': 'portuguese',
ro: 'romanian',
ru: 'russian',
ckb: 'sorani',
es: 'spanish',
sv: 'swedish',
tr: 'turkish',
th: 'thai'
};
let search = {
// For testing queries
_raw(obj) {
return client.search(obj);
},
// Find things by their label or description; performs language fallback
searchThings(query, lang = 'en') {
let options = search.getSearchOptions('label', lang);
let descriptionOptions = search.getSearchOptions('description', lang);
options.fields = options.fields.concat(descriptionOptions.fields);
Object.assign(options.highlight.fields, descriptionOptions.highlight.fields);
return client.search({
index: 'libreviews',
body: {
query: {
bool: {
must: [{
match: {
type: 'thing'
}
},
{
simple_query_string: {
fields: options.fields,
query,
default_operator: 'and'
}
}
],
},
},
highlight: options.highlight
}
});
},
// Find reviews by their text or title; performs language fallback and includes
// the thing via parent-child join. The review is returned as an inner hit.
searchReviews(query, lang = 'en') {
// Add text fields
let options = search.getSearchOptions('text', lang);
// Add title fields
let titleOptions = search.getSearchOptions('title', lang);
options.fields = options.fields.concat(titleOptions.fields);
Object.assign(options.highlight.fields, titleOptions.highlight.fields);
return client.search({
index: 'libreviews',
body: {
query: {
has_child: {
type: 'review',
query: {
simple_query_string: {
fields: options.fields,
query,
default_operator: 'and'
}
},
inner_hits: {
highlight: options.highlight
},
}
}
}
});
},
// We may be getting highlights from both the processed (stememd) index
// and the unprocessed one. This function filters the dupes from inner hits.
filterDuplicateInnerHighlights(hits, type) {
for (let hit of hits) {
if (hit.inner_hits && hit.inner_hits[type] && hit.inner_hits[type].hits) {
for (let innerHit of hit.inner_hits[type].hits.hits) {
if (innerHit.highlight) {
let seenHighlights = [];
for (let key in innerHit.highlight) {
innerHit.highlight[key] = innerHit.highlight[key].filter(highlight => {
if (seenHighlights.indexOf(highlight) === -1) {
seenHighlights.push(highlight);
return true;
} else {
return false;
}
});
}
}
}
}
}
return hits;
},
// Generate language fallback and highlight options.
getSearchOptions(fieldPrefix, lang) {
let langs = languages.getFallbacks(lang);
if (lang !== 'en')
langs.unshift(lang);
// Searches both stemmed and non-stemmed version
let fields = langs.map(lang => `${fieldPrefix}.${lang}*`);
// Add search highlighters
let highlight = {
pre_tags: ['<span class="search-highlight">'],
post_tags: ['</span>'],
fields: {}
};
for (let lang of langs)
highlight.fields[`${fieldPrefix}.${lang}*`] = {};
return {
fields,
highlight
};
},
// Get search suggestions based on entered characters for review subjects
// (things).
suggestThing(prefix = '', lang = 'en') {
// We'll query all fallbacks back to English, and return all results
let langs = languages.getFallbacks(lang);
if (lang !== 'en')
langs.unshift(lang);
let query = {
index: 'libreviews',
body: {
suggest: {}
}
};
for (let currentLanguage of langs) {
query.body.suggest[`labels-${currentLanguage}`] = {
prefix,
completion: {
field: `label.${currentLanguage}.completion`
}
};
}
return client.search(query);
},
// Index a new review. Returns a promise; logs errors
indexReview(review) {
return client.index({
index: 'libreviews',
id: review.id,
routing: review.thingID,
body: {
createdOn: review.createdOn,
title: mlString.stripHTML(review.title),
text: mlString.stripHTML(review.html),
starRating: review.starRating,
type: 'review',
joined: {
name: 'review',
parent: review.thingID
}
}
})
.catch(error => debug.error({
error
}));
},
// Index a new review subject (thing). Returns a promise; logs errors
indexThing(thing) {
return client.index({
index: 'libreviews',
id: thing.id,
body: {
createdOn: thing.createdOn,
label: mlString.stripHTML(thing.label),
aliases: mlString.stripHTMLFromArray(thing.aliases),
description: mlString.stripHTML(thing.description),
joined: 'thing',
type: 'thing',
urls: thing.urls,
urlID: thing.urlID
}
})
.catch(error => debug.error({
error
}));
},
deleteThing(thing) {
return client.delete({
index: 'libreviews',
id: thing.id
})
.catch(error => debug.error({
error
}));
},
deleteReview(review) {
return client.delete({
index: 'libreviews',
id: review.id
})
.catch(error => debug.error({
error
}));
},
// Create the initial index for holding reviews and review subjects (things).
// If index already exists, does nothing. Logs all other errors.
createIndices() {
return client.indices.create({
index: 'libreviews',
body: {
settings: {
analysis: {
tokenizer: {
whitespace: {
type: 'whitespace'
}
},
analyzer: {
label: {
type: 'custom',
tokenizer: 'whitespace',
filter: ['trim', 'lowercase']
}
}
}
},
mappings: {
properties: {
createdOn: {
type: 'date'
},
joined: {
type: 'join',
relations: {
thing: 'review'
}
},
text: search.getMultilingualTextProperties(),
title: search.getMultilingualTextProperties(),
urls: search.getURLProperties(),
label: search.getMultilingualTextProperties(true),
aliases: search.getMultilingualTextProperties(true),
description: search.getMultilingualTextProperties(),
type: {
type: 'keyword'
}
}
}
}
})
.catch(error => {
if (/\[index_already_exists_exception\]/.test(error.message))
return;
debug.error({
error
});
});
},
// Generate the mappings (ElasticSearch schemas) for indexing URLs. We index
// each URL three times to enable multiple search strategies
getURLProperties() {
return {
// https://www.wikidata.org/wiki/Q27940587 -> https,www.wikidata.org,wiki,q27940587
type: 'text',
fields: {
raw: {
type: 'keyword' // https://www.wikidata.org/wiki/Q27940587 -> https://www.wikidata.org/wiki/Q27940587
},
simple: {
type: 'text',
analyzer: 'simple' // https,www,wikidata,org,wiki,q
}
}
};
},
// Generate the mappings (ElasticSearch schemas) for indexing multilingual
// strings
getMultilingualTextProperties(completionMapping = false) {
let obj = {
properties: {}
};
let validLangs = languages.getValidLanguagesAndUndetermined();
// We add all analyzers for all languages ElasticSearch has stemming support
// for to the index, even if they're not yet supported by lib.reviews, so
// we don't have to keep updating the index. Languages without analyzers
// will be processed by the 'standard' analyzer (no stemming)
for (let lang in analyzers) {
// Splice from language array so we can process remaining languages differently
let langPos = validLangs.indexOf(lang);
if (langPos !== -1)
validLangs.splice(langPos, 1);
obj.properties[lang] = {
type: 'text',
index_options: 'offsets', // for sentence-based highlighting
fields: {
// The 'processed' property of the text field contains the stemmed
// version (run through appropriate language analyzer) so we can
// run searches against both the full text and the stemmed version,
// as appropriate
processed: {
type: 'text',
analyzer: analyzers[lang],
index_options: 'offsets' // for sentence-based highlighting
}
}
};
if (completionMapping)
obj.properties[lang].fields.completion = search.getCompletionMapping();
}
// Add remaining languages so we can do completion & offsets for those
// as well.
for (let lang of validLangs) {
obj.properties[lang] = {
type: 'text',
index_options: 'offsets', // for sentence-based highlighting
};
if (completionMapping)
obj.properties[lang].fields = {
completion: search.getCompletionMapping()
};
}
return obj;
},
// Return mapping for label autocompletion
getCompletionMapping() {
return {
type: 'completion',
analyzer: 'label',
max_input_length: 256 // default is 50, our labels are 256
};
}
};
module.exports = search;