Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Elasticsearch 8.x support #6904

Merged
merged 11 commits into from
Oct 12, 2024
Merged
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"dotize": "0.3.0",
"elasticsearch6": "npm:@elastic/elasticsearch@6",
"elasticsearch7": "npm:@elastic/elasticsearch@7",
"elasticsearch8": "npm:@elastic/elasticsearch@8",
"emoji-regex": "10.2.1",
"eventemitter2": "6.4.9",
"express": "4.18.2",
Expand Down
3 changes: 2 additions & 1 deletion server/modules/search/elasticsearch/definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ props:
hint: Should match the version of the Elasticsearch nodes you are connecting to
order: 1
enum:
- '8.x'
- '7.x'
- '6.x'
default: '6.x'
default: '7.x'
hosts:
type: String
title: Host(s)
Expand Down
53 changes: 42 additions & 11 deletions server/modules/search/elasticsearch/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ module.exports = {
async init() {
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Initializing...`)
switch (this.config.apiVersion) {
case '8.x':
WIKI.logger.info(`Using ElasticSearch 8.X`) // TODO Remove this line
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':
const { Client: Client7 } = require('elasticsearch7')
this.client = new Client7({
Expand Down Expand Up @@ -54,7 +65,8 @@ module.exports = {
async createIndex() {
try {
const indexExists = await this.client.indices.exists({ index: this.config.indexName })
if (!indexExists.body) {
// 6.x / 7.x return indexExists.body, while 8.x only returns indexExists so we need uniqe conditionals
if ((!indexExists.body && this.config.apiVersion !== '8.x') || (!indexExists && this.config.apiVersion === '8.x')) {
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`)
try {
const idxBody = {
Expand All @@ -68,12 +80,34 @@ module.exports = {
tags: { type: 'text', boost: 8.0 }
}
}
// 8.x Doesn't support boost in mappings, so we will need to boost at query time.
const idxBody8_x = {
properties: {
suggest: { type: 'completion' },
title: { type: 'text' },
description: { type: 'text' },
content: { type: 'text' },
locale: { type: 'keyword' },
path: { type: 'text' },
tags: { type: 'text' }
}
}

let mapping
if (this.config.apiVersion !== '8.x') {
// ElasticSearch 6.x || 7.x can use the same mapping
mapping = idxBody
} else {
// ElasticSearch 8.x needs to use a different mapping
mapping = idxBody8_x
}

await this.client.indices.create({
index: this.config.indexName,
body: {
mappings: (this.config.apiVersion === '6.x') ? {
_doc: idxBody
} : idxBody,
_doc: mapping
} : mapping,
settings: {
analysis: {
analyzer: {
Expand All @@ -85,12 +119,15 @@ module.exports = {
}
}
})

} catch (err) {
WIKI.logger.error(err)
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err))
}
}
} catch (err) {
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Index Check Error: `, _.get(err, 'meta.body.error', err))
WIKI.logger.error(err)
}
},
/**
Expand Down Expand Up @@ -129,15 +166,15 @@ module.exports = {
}
})
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,
locale: r._source.locale,
path: r._source.path,
title: r._source.title,
description: r._source.description
})),
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) {
WIKI.logger.warn('Search Engine Error: ', _.get(err, 'meta.body.error', err))
Expand Down Expand Up @@ -182,7 +219,6 @@ module.exports = {
async created(page) {
await this.client.index({
index: this.config.indexName,
type: '_doc',
jacobacon marked this conversation as resolved.
Show resolved Hide resolved
id: page.hash,
body: {
suggest: this.buildSuggest(page),
Expand All @@ -204,7 +240,6 @@ module.exports = {
async updated(page) {
await this.client.index({
index: this.config.indexName,
type: '_doc',
id: page.hash,
body: {
suggest: this.buildSuggest(page),
Expand All @@ -226,7 +261,6 @@ module.exports = {
async deleted(page) {
await this.client.delete({
index: this.config.indexName,
type: '_doc',
id: page.hash,
refresh: true
})
Expand All @@ -239,13 +273,11 @@ module.exports = {
async renamed(page) {
await this.client.delete({
index: this.config.indexName,
type: '_doc',
id: page.hash,
refresh: true
})
await this.client.index({
index: this.config.indexName,
type: '_doc',
id: page.destinationHash,
body: {
suggest: this.buildSuggest(page),
Expand Down Expand Up @@ -314,7 +346,6 @@ module.exports = {
result.push({
index: {
_index: this.config.indexName,
_type: '_doc',
_id: doc.id
}
})
Expand Down
Loading