feat: add elasticsearch 8.x support (#6904)

pull/7414/head
Jacob Beneski 1 month ago committed by GitHub
parent ad37325773
commit 7757de3af7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -75,6 +75,7 @@
"dotize": "0.3.0", "dotize": "0.3.0",
"elasticsearch6": "npm:@elastic/elasticsearch@6", "elasticsearch6": "npm:@elastic/elasticsearch@6",
"elasticsearch7": "npm:@elastic/elasticsearch@7", "elasticsearch7": "npm:@elastic/elasticsearch@7",
"elasticsearch8": "npm:@elastic/elasticsearch@8",
"emoji-regex": "10.2.1", "emoji-regex": "10.2.1",
"eventemitter2": "6.4.9", "eventemitter2": "6.4.9",
"express": "4.18.2", "express": "4.18.2",

@ -12,9 +12,10 @@ props:
hint: Should match the version of the Elasticsearch nodes you are connecting to hint: Should match the version of the Elasticsearch nodes you are connecting to
order: 1 order: 1
enum: enum:
- '8.x'
- '7.x' - '7.x'
- '6.x' - '6.x'
default: '6.x' default: '7.x'
hosts: hosts:
type: String type: String
title: Host(s) title: Host(s)

@ -19,6 +19,16 @@ module.exports = {
async init() { async init() {
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Initializing...`) WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Initializing...`)
switch (this.config.apiVersion) { switch (this.config.apiVersion) {
case '8.x':
const { Client: Client8 } = require('elasticsearch8')
this.client = new Client8({
nodes: this.config.hosts.split(',').map(_.trim),
sniffOnStart: this.config.sniffOnStart,
sniffInterval: (this.config.sniffInterval > 0) ? this.config.sniffInterval : false,
tls: getTlsOptions(this.config),
name: 'wiki-js'
})
break
case '7.x': case '7.x':
const { Client: Client7 } = require('elasticsearch7') const { Client: Client7 } = require('elasticsearch7')
this.client = new Client7({ this.client = new Client7({
@ -54,7 +64,8 @@ module.exports = {
async createIndex() { async createIndex() {
try { try {
const indexExists = await this.client.indices.exists({ index: this.config.indexName }) const indexExists = await this.client.indices.exists({ index: this.config.indexName })
if (!indexExists.body) { // Elasticsearch 6.x / 7.x
if (this.config.apiVersion !== '8.x' && !indexExists.body) {
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`) WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`)
try { try {
const idxBody = { const idxBody = {
@ -68,6 +79,7 @@ module.exports = {
tags: { type: 'text', boost: 8.0 } tags: { type: 'text', boost: 8.0 }
} }
} }
await this.client.indices.create({ await this.client.indices.create({
index: this.config.indexName, index: this.config.indexName,
body: { body: {
@ -88,7 +100,42 @@ module.exports = {
} catch (err) { } catch (err) {
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err)) WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err))
} }
} // Elasticsearch 8.x
} else if (this.config.apiVersion === '8.x' && !indexExists) {
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`)
try {
// 8.x Doesn't support boost in mappings, so we will need to boost at query time.
const idxBody = {
properties: {
suggest: { type: 'completion' },
title: { type: 'text' },
description: { type: 'text' },
content: { type: 'text' },
locale: { type: 'keyword' },
path: { type: 'text' },
tags: { type: 'text' }
}
}
await this.client.indices.create({
index: this.config.indexName,
body: {
mappings: idxBody,
settings: {
analysis: {
analyzer: {
default: {
type: this.config.analyzer
}
}
}
}
}
})
} catch (err) {
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err))
}
}
} catch (err) { } catch (err) {
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Index Check Error: `, _.get(err, 'meta.body.error', err)) WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Index Check Error: `, _.get(err, 'meta.body.error', err))
} }
@ -129,7 +176,7 @@ module.exports = {
} }
}) })
return { return {
results: _.get(results, 'body.hits.hits', []).map(r => ({ results: _.get(results, this.config.apiVersion === '8.x' ? 'hits.hits' : 'body.hits.hits', []).map(r => ({
id: r._id, id: r._id,
locale: r._source.locale, locale: r._source.locale,
path: r._source.path, path: r._source.path,
@ -137,7 +184,7 @@ module.exports = {
description: r._source.description description: r._source.description
})), })),
suggestions: _.reject(_.get(results, 'suggest.suggestions', []).map(s => _.get(s, 'options[0].text', false)), s => !s), suggestions: _.reject(_.get(results, 'suggest.suggestions', []).map(s => _.get(s, 'options[0].text', false)), s => !s),
totalHits: _.get(results, 'body.hits.total.value', _.get(results, 'body.hits.total', 0)) totalHits: _.get(results, this.config.apiVersion === '8.x' ? 'hits.total.value' : 'body.hits.total.value', _.get(results, this.config.apiVersion === '8.x' ? 'hits.total' : 'body.hits.total', 0))
} }
} catch (err) { } catch (err) {
WIKI.logger.warn('Search Engine Error: ', _.get(err, 'meta.body.error', err)) WIKI.logger.warn('Search Engine Error: ', _.get(err, 'meta.body.error', err))
@ -182,7 +229,7 @@ module.exports = {
async created(page) { async created(page) {
await this.client.index({ await this.client.index({
index: this.config.indexName, index: this.config.indexName,
type: '_doc', ...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
id: page.hash, id: page.hash,
body: { body: {
suggest: this.buildSuggest(page), suggest: this.buildSuggest(page),
@ -204,7 +251,7 @@ module.exports = {
async updated(page) { async updated(page) {
await this.client.index({ await this.client.index({
index: this.config.indexName, index: this.config.indexName,
type: '_doc', ...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
id: page.hash, id: page.hash,
body: { body: {
suggest: this.buildSuggest(page), suggest: this.buildSuggest(page),
@ -226,7 +273,7 @@ module.exports = {
async deleted(page) { async deleted(page) {
await this.client.delete({ await this.client.delete({
index: this.config.indexName, index: this.config.indexName,
type: '_doc', ...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
id: page.hash, id: page.hash,
refresh: true refresh: true
}) })
@ -239,13 +286,13 @@ module.exports = {
async renamed(page) { async renamed(page) {
await this.client.delete({ await this.client.delete({
index: this.config.indexName, index: this.config.indexName,
type: '_doc', ...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
id: page.hash, id: page.hash,
refresh: true refresh: true
}) })
await this.client.index({ await this.client.index({
index: this.config.indexName, index: this.config.indexName,
type: '_doc', ...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
id: page.destinationHash, id: page.destinationHash,
body: { body: {
suggest: this.buildSuggest(page), suggest: this.buildSuggest(page),
@ -314,8 +361,8 @@ module.exports = {
result.push({ result.push({
index: { index: {
_index: this.config.indexName, _index: this.config.indexName,
_type: '_doc', _id: doc.id,
_id: doc.id ...(this.config.apiVersion !== '8.x' && { _type: '_doc' })
} }
}) })
doc.safeContent = WIKI.models.pages.cleanHTML(doc.render) doc.safeContent = WIKI.models.pages.cleanHTML(doc.render)

Loading…
Cancel
Save