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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
60 changes: 47 additions & 13 deletions server/modules/search/elasticsearch/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ module.exports = {
async init() {
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Initializing...`)
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':
const { Client: Client7 } = require('elasticsearch7')
this.client = new Client7({
Expand Down Expand Up @@ -54,7 +64,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 +79,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,
mappings: (this.config.apiVersion !== '8.x') ? {
_doc: mapping
} : mapping,
settings: {
analysis: {
analyzer: {
Expand All @@ -85,6 +118,7 @@ module.exports = {
}
}
})

} catch (err) {
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err))
}
Expand Down Expand Up @@ -129,15 +163,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 +216,7 @@ 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
...(this.config.apiVersion !== '8.x' && {type: '_doc'}),
id: page.hash,
body: {
suggest: this.buildSuggest(page),
Expand All @@ -204,7 +238,7 @@ module.exports = {
async updated(page) {
await this.client.index({
index: this.config.indexName,
type: '_doc',
...(this.config.apiVersion !== '8.x' && {type: '_doc'}),
id: page.hash,
body: {
suggest: this.buildSuggest(page),
Expand All @@ -226,7 +260,7 @@ module.exports = {
async deleted(page) {
await this.client.delete({
index: this.config.indexName,
type: '_doc',
...(this.config.apiVersion !== '8.x' && {type: '_doc'}),
id: page.hash,
refresh: true
})
Expand All @@ -239,13 +273,13 @@ module.exports = {
async renamed(page) {
await this.client.delete({
index: this.config.indexName,
type: '_doc',
...(this.config.apiVersion !== '8.x' && {type: '_doc'}),
id: page.hash,
refresh: true
})
await this.client.index({
index: this.config.indexName,
type: '_doc',
...(this.config.apiVersion !== '8.x' && {type: '_doc'}),
id: page.destinationHash,
body: {
suggest: this.buildSuggest(page),
Expand Down Expand Up @@ -314,8 +348,8 @@ module.exports = {
result.push({
index: {
_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)
Expand Down