forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-elasticsearch.js
executable file
·434 lines (393 loc) · 14.3 KB
/
index-elasticsearch.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#!/usr/bin/env node
// [start-readme]
//
// Creates Elasticsearch index, populates from records,
// moves the index alias, deletes old indexes.
//
// [end-readme]
import fs from 'fs/promises'
import path from 'path'
import { Client, errors } from '@elastic/elasticsearch'
import { program, Option } from 'commander'
import chalk from 'chalk'
import dotenv from 'dotenv'
import { retryOnErrorTest } from '../helpers/retry-on-error-test.js'
import { languageKeys } from '../../lib/languages.js'
import { allVersions } from '../../lib/all-versions.js'
import statsd from '../../lib/statsd.js'
// Now you can optionally have set the ELASTICSEARCH_URL in your .env file.
dotenv.config()
// Create an object that maps the "short name" of a version to
// all information about it. E.g
//
// {
// 'ghes-3.5': {
// hasNumberedReleases: true,
// currentRelease: '3.5',
// version: '[email protected]',
// miscBaseName: 'ghes-'
// ...
// },
// ...
//
// We need this later to be able to map CLI arguments to what the
// records are called when found on disk.
const shortNames = Object.fromEntries(
Object.values(allVersions).map((info) => {
const shortName = info.hasNumberedReleases
? info.miscBaseName + info.currentRelease
: info.miscBaseName
return [shortName, info]
})
)
const allVersionKeys = Object.keys(shortNames)
program
.description('Creates Elasticsearch index from records')
.option('-v, --verbose', 'Verbose outputs')
.addOption(new Option('-V, --version [VERSION...]', 'Specific versions').choices(allVersionKeys))
.addOption(
new Option('-l, --language <LANGUAGE...>', 'Which languages to focus on').choices(languageKeys)
)
.addOption(
new Option('--not-language <LANGUAGE...>', 'Specific language to omit').choices(languageKeys)
)
.option('-u, --elasticsearch-url <url>', 'If different from $ELASTICSEARCH_URL')
.option('-p, --index-prefix <prefix>', 'Index string to put before index name')
.argument('<source-directory>', 'where the indexable files are')
.parse(process.argv)
main(program.opts(), program.args)
async function main(opts, args) {
if (!args.length) {
throw new Error('Must pass the source as the first argument')
}
const { verbose, language, notLanguage, elasticsearchUrl } = opts
if (!elasticsearchUrl && !process.env.ELASTICSEARCH_URL) {
throw new Error(
'Must passed the elasticsearch URL option or ' +
'set the environment variable ELASTICSEARCH_URL'
)
}
let node = elasticsearchUrl || process.env.ELASTICSEARCH_URL
// Allow the user to lazily set it to `localhost:9200` for example.
if (!node.startsWith('http') && !node.startsWith('://') && node.split(':').length === 2) {
node = `http://${node}`
}
try {
const parsed = new URL(node)
if (!parsed.hostname) throw new Error('no valid hostname')
} catch (err) {
console.error(chalk.bold('URL for Elasticsearch not a valid URL', err))
throw err
}
// The notLanguage is useful you want to, for example, index all languages
// *except* English.
if (language && notLanguage) {
throw new Error("Can't combine --language and --not-language")
}
if (verbose) {
console.log(`Connecting to ${chalk.bold(safeUrlDisplay(node))}`)
}
const sourceDirectory = args[0]
try {
await fs.stat(sourceDirectory)
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`The specified directory '${sourceDirectory}' does not exist.`)
}
throw error
}
try {
await indexAll(node, sourceDirectory, opts)
} catch (error) {
// If any error is thrown from within the SDK, that error object will
// contain a `Connection` object which, when printed, can reveal the
// username/password or the base64 Basic auth credentials.
// So we want to carefully re-throw it so it only contains the minimal
// information for debugging without exposing the Connection credentials
// in Actions logs.
if (error instanceof errors.ElasticsearchClientError) {
// All ElasticsearchClientError error subclasses have a `name` and
// `message` but only some have a `meta`.
if (error.meta) console.error(error.meta)
throw new Error(error.message)
}
// If any other error happens that isn't from the elasticsearch SDK,
// let it bubble up.
throw error
}
}
async function indexAll(node, sourceDirectory, opts) {
const client = new Client({ node })
const { language, verbose, notLanguage, indexPrefix } = opts
let version
if ('version' in opts) {
version = opts.version
if (process.env.VERSION) {
console.warn(
`'version' specified as argument ('${version}') AND environment variable ('${process.env.VERSION}')`
)
}
} else {
if (process.env.VERSION && process.env.VERSION !== 'all') {
version = process.env.VERSION
if (!allVersionKeys.includes(version)) {
throw new Error(
`Environment variable 'VERSION' (${version}) is not recognized. Must be one of ${allVersionKeys}`
)
}
}
}
let versionKeys = allVersionKeys
// If it came from the `--version` argument parsing, it might be a string
// or an array of strings because it uses `--version [VERSION...]`.
if (version) {
if (Array.isArray(version)) {
versionKeys = version
} else {
versionKeys = [version]
}
}
// This will throw if it can't ping
await client.ping()
const languages =
language || languageKeys.filter((lang) => !notLanguage || !notLanguage.includes(lang))
if (verbose) {
console.log(`Indexing on languages ${chalk.bold(languages.join(', '))}`)
}
const prefix = indexPrefix ? `${indexPrefix}_` : ''
for (const language of languages) {
for (const versionKey of versionKeys) {
console.log(chalk.yellow(`Indexing ${chalk.bold(versionKey)} in ${chalk.bold(language)}`))
const indexName = `${prefix}github-docs-${versionKey}-${language}`
console.time(`Indexing ${indexName}`)
await indexVersion(client, indexName, versionKey, language, sourceDirectory, verbose)
console.timeEnd(`Indexing ${indexName}`)
if (verbose) {
console.log(`To view index: ${safeUrlDisplay(node + `/${indexName}`)}`)
console.log(`To search index: ${safeUrlDisplay(node + `/${indexName}/_search`)}`)
}
}
}
}
function safeUrlDisplay(url) {
const parsed = new URL(url)
if (parsed.password) {
parsed.password = '***'
}
if (parsed.username) {
parsed.username = parsed.username.slice(0, 4) + '***'
}
return parsed.toString()
}
// Return '20220719012012' if the current date is
// 2022-07-19T01:20:12.172Z. Note how the 6th month (July) becomes
// '07'. All numbers become 2 character zero-padding strings individually.
function utcTimestamp() {
const d = new Date()
return (
[
`${d.getUTCFullYear()}`,
d.getUTCMonth() + 1,
d.getUTCDate(),
d.getUTCHours(),
d.getUTCMinutes(),
d.getUTCSeconds(),
]
// If it's a number make it a zero-padding 2 character string
.map((x) => (typeof x === 'number' ? ('0' + x).slice(-2) : x))
.join('')
)
}
// Consider moving this to lib
async function indexVersion(
client,
indexName,
version,
language,
sourceDirectory,
verbose = false
) {
// Note, it's a bit "weird" that numbered releases versions are
// called the number but that's the convention the previous
// search backend used
const indexVersion = shortNames[version].hasNumberedReleases
? shortNames[version].currentRelease
: shortNames[version].miscBaseName
const recordsName = `github-docs-${indexVersion}-${language}`
const records = await loadRecords(recordsName, sourceDirectory)
const thisAlias = `${indexName}__${utcTimestamp()}`
// CREATE INDEX
const settings = {
analysis: {
analyzer: {
// We defined to analyzers. Both based on a "common core" with the
// `standard` tokenizer. But the second one adds Snowball filter.
// That means the tokenization of "Dependency naming" becomes
// `[dependency, naming]` in the explicit one and `[depend, name]`
// in the Snowball one.
// We do this to give a chance to boost the more exact spelling a
// bit higher with the assumption that if the user knew exactly
// what it was called, we should show that higher.
// A great use-case of this when users search for keywords that are
// code words like `dependency-name`.
text_analyzer_explicit: {
filter: ['lowercase', 'stop', 'asciifolding'],
tokenizer: 'standard',
type: 'custom',
},
text_analyzer: {
filter: ['lowercase', 'stop', 'asciifolding'],
tokenizer: 'standard',
type: 'custom',
},
},
filter: {
// Will later, conditionally, put the snowball configuration here.
},
},
}
const snowballLanguage = getSnowballLanguage(language)
if (snowballLanguage) {
settings.analysis.analyzer.text_analyzer.filter.push('languaged_snowball')
settings.analysis.filter.languaged_snowball = {
type: 'snowball',
language: snowballLanguage,
}
} else {
if (verbose) {
console.warn(`No snowball language for '${language}'`)
}
}
await client.indices.create({
index: thisAlias,
body: {
mappings: {
properties: {
url: { type: 'keyword' },
title: { type: 'text', analyzer: 'text_analyzer', norms: false },
title_explicit: { type: 'text', analyzer: 'text_analyzer_explicit', norms: false },
content: { type: 'text', analyzer: 'text_analyzer' },
content_explicit: { type: 'text', analyzer: 'text_analyzer_explicit' },
headings: { type: 'text', analyzer: 'text_analyzer', norms: false },
headings_explicit: { type: 'text', analyzer: 'text_analyzer_explicit', norms: false },
breadcrumbs: { type: 'text' },
popularity: { type: 'float' },
intro: { type: 'text' },
},
},
settings,
},
})
// POPULATE
const allRecords = Object.values(records).sort((a, b) => b.popularity - a.popularity)
const operations = allRecords.flatMap((doc) => {
const { title, objectID, content, breadcrumbs, headings, intro } = doc
const contentEscaped = escapeHTML(content)
const record = {
url: objectID,
title,
title_explicit: title,
content: contentEscaped,
content_explicit: contentEscaped,
breadcrumbs,
headings,
headings_explicit: headings,
// This makes sure the popularities are always greater than 1.
// Generally the 'popularity' is a ratio where the most popular
// one of all is 1.0.
// By making it >=1.0 when we multiply a relevance score,
// you never get a product of 0.0.
popularity: doc.popularity + 1,
intro,
}
return [{ index: { _index: thisAlias } }, record]
})
// It's important to use `client.bulk.bind(client)` here because
// `client.bulk` is a meta-function that is attached to the Client
// class. Internally, it depends on `this.` even though it's a
// free-standing function. So if called indirectly by the `statsd.asyncTimer`
// the `this` becomes undefined.
const timed = statsd.asyncTimer(client.bulk.bind(client), 'search.bulk_index', [
`version:${version}`,
`language:${language}`,
])
const bulkResponse = await timed({ refresh: true, body: operations })
if (bulkResponse.errors) {
// Some day, when we're more confident how and why this might happen
// we can rewrite this code to "massage" the errors better.
// For now, if it fails, it's "OK". It means we won't be proceeding,
// an error is thrown in Actions and we don't have to worry about
// an incompletion index.
console.error(bulkResponse.errors)
throw new Error('Bulk errors happened.')
}
const {
body: { count },
} = await client.count({ index: thisAlias })
console.log(`Documents now in ${chalk.bold(thisAlias)}: ${chalk.bold(count.toLocaleString())}`)
// To perform an atomic operation that creates the new alias and removes
// the old indexes, we can use the updateAliases API with a body that
// includes an "actions" array. The array includes the added alias
// and the removed indexes. If any of the actions fail, none of the operations
// are performed.
// https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-aliases.html
const aliasUpdates = [
{
add: {
index: thisAlias,
alias: indexName,
},
},
]
console.log(`Alias ${indexName} -> ${thisAlias}`)
// const indices = await client.cat.indices({ format: 'json' })
const { body: indices } = await retryOnErrorTest(
(error) => {
return error instanceof errors.ResponseError && error.statusCode === 404
},
() => client.cat.indices({ format: 'json' }),
{
// Combined, this is a total of 30 seconds which is not long
// for an Action that runs based on automation.
attempts: 10,
sleepTime: 3000,
onError: (error, attempts) => {
console.warn(
chalk.yellow(
`Failed to get a list of indexes for '${indexName}' (${error.message}). Will attempt ${attempts} more times.`
)
)
},
}
)
for (const index of indices) {
if (index.index !== thisAlias && index.index.startsWith(indexName)) {
aliasUpdates.push({ remove_index: { index: index.index } })
console.log('Deleting index', index.index)
}
}
if (verbose) console.log('Updating alias actions:', aliasUpdates)
await client.indices.updateAliases({ body: { actions: aliasUpdates } })
}
function escapeHTML(content) {
return content.replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"')
}
async function loadRecords(indexName, sourceDirectory) {
const filePath = path.join(sourceDirectory, `${indexName}-records.json`)
const payload = await fs.readFile(filePath)
return JSON.parse(payload)
}
function getSnowballLanguage(language) {
// Based on https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-snowball-tokenfilter.html
// Note, not all languages are supported. So this function might return
// undefined. That implies that you can't use snowballing.
return {
en: 'English',
fr: 'French',
es: 'Spanish',
ru: 'Russian',
it: 'Italian',
de: 'German',
pt: 'Portuguese',
}[language]
}