Skip to content

Commit 7757de3

Browse files
authored
feat: add elasticsearch 8.x support (#6904)
1 parent ad37325 commit 7757de3

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"dotize": "0.3.0",
7676
"elasticsearch6": "npm:@elastic/elasticsearch@6",
7777
"elasticsearch7": "npm:@elastic/elasticsearch@7",
78+
"elasticsearch8": "npm:@elastic/elasticsearch@8",
7879
"emoji-regex": "10.2.1",
7980
"eventemitter2": "6.4.9",
8081
"express": "4.18.2",

server/modules/search/elasticsearch/definition.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ props:
1212
hint: Should match the version of the Elasticsearch nodes you are connecting to
1313
order: 1
1414
enum:
15+
- '8.x'
1516
- '7.x'
1617
- '6.x'
17-
default: '6.x'
18+
default: '7.x'
1819
hosts:
1920
type: String
2021
title: Host(s)

server/modules/search/elasticsearch/engine.js

+58-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ module.exports = {
1919
async init() {
2020
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Initializing...`)
2121
switch (this.config.apiVersion) {
22+
case '8.x':
23+
const { Client: Client8 } = require('elasticsearch8')
24+
this.client = new Client8({
25+
nodes: this.config.hosts.split(',').map(_.trim),
26+
sniffOnStart: this.config.sniffOnStart,
27+
sniffInterval: (this.config.sniffInterval > 0) ? this.config.sniffInterval : false,
28+
tls: getTlsOptions(this.config),
29+
name: 'wiki-js'
30+
})
31+
break
2232
case '7.x':
2333
const { Client: Client7 } = require('elasticsearch7')
2434
this.client = new Client7({
@@ -54,7 +64,8 @@ module.exports = {
5464
async createIndex() {
5565
try {
5666
const indexExists = await this.client.indices.exists({ index: this.config.indexName })
57-
if (!indexExists.body) {
67+
// Elasticsearch 6.x / 7.x
68+
if (this.config.apiVersion !== '8.x' && !indexExists.body) {
5869
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`)
5970
try {
6071
const idxBody = {
@@ -68,6 +79,7 @@ module.exports = {
6879
tags: { type: 'text', boost: 8.0 }
6980
}
7081
}
82+
7183
await this.client.indices.create({
7284
index: this.config.indexName,
7385
body: {
@@ -88,7 +100,42 @@ module.exports = {
88100
} catch (err) {
89101
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err))
90102
}
91-
}
103+
// Elasticsearch 8.x
104+
} else if (this.config.apiVersion === '8.x' && !indexExists) {
105+
WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`)
106+
try {
107+
// 8.x Doesn't support boost in mappings, so we will need to boost at query time.
108+
const idxBody = {
109+
properties: {
110+
suggest: { type: 'completion' },
111+
title: { type: 'text' },
112+
description: { type: 'text' },
113+
content: { type: 'text' },
114+
locale: { type: 'keyword' },
115+
path: { type: 'text' },
116+
tags: { type: 'text' }
117+
}
118+
}
119+
120+
await this.client.indices.create({
121+
index: this.config.indexName,
122+
body: {
123+
mappings: idxBody,
124+
settings: {
125+
analysis: {
126+
analyzer: {
127+
default: {
128+
type: this.config.analyzer
129+
}
130+
}
131+
}
132+
}
133+
}
134+
})
135+
} catch (err) {
136+
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Create Index Error: `, _.get(err, 'meta.body.error', err))
137+
}
138+
}
92139
} catch (err) {
93140
WIKI.logger.error(`(SEARCH/ELASTICSEARCH) Index Check Error: `, _.get(err, 'meta.body.error', err))
94141
}
@@ -129,15 +176,15 @@ module.exports = {
129176
}
130177
})
131178
return {
132-
results: _.get(results, 'body.hits.hits', []).map(r => ({
179+
results: _.get(results, this.config.apiVersion === '8.x' ? 'hits.hits' : 'body.hits.hits', []).map(r => ({
133180
id: r._id,
134181
locale: r._source.locale,
135182
path: r._source.path,
136183
title: r._source.title,
137184
description: r._source.description
138185
})),
139186
suggestions: _.reject(_.get(results, 'suggest.suggestions', []).map(s => _.get(s, 'options[0].text', false)), s => !s),
140-
totalHits: _.get(results, 'body.hits.total.value', _.get(results, 'body.hits.total', 0))
187+
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))
141188
}
142189
} catch (err) {
143190
WIKI.logger.warn('Search Engine Error: ', _.get(err, 'meta.body.error', err))
@@ -182,7 +229,7 @@ module.exports = {
182229
async created(page) {
183230
await this.client.index({
184231
index: this.config.indexName,
185-
type: '_doc',
232+
...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
186233
id: page.hash,
187234
body: {
188235
suggest: this.buildSuggest(page),
@@ -204,7 +251,7 @@ module.exports = {
204251
async updated(page) {
205252
await this.client.index({
206253
index: this.config.indexName,
207-
type: '_doc',
254+
...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
208255
id: page.hash,
209256
body: {
210257
suggest: this.buildSuggest(page),
@@ -226,7 +273,7 @@ module.exports = {
226273
async deleted(page) {
227274
await this.client.delete({
228275
index: this.config.indexName,
229-
type: '_doc',
276+
...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
230277
id: page.hash,
231278
refresh: true
232279
})
@@ -239,13 +286,13 @@ module.exports = {
239286
async renamed(page) {
240287
await this.client.delete({
241288
index: this.config.indexName,
242-
type: '_doc',
289+
...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
243290
id: page.hash,
244291
refresh: true
245292
})
246293
await this.client.index({
247294
index: this.config.indexName,
248-
type: '_doc',
295+
...(this.config.apiVersion !== '8.x' && { type: '_doc' }),
249296
id: page.destinationHash,
250297
body: {
251298
suggest: this.buildSuggest(page),
@@ -314,8 +361,8 @@ module.exports = {
314361
result.push({
315362
index: {
316363
_index: this.config.indexName,
317-
_type: '_doc',
318-
_id: doc.id
364+
_id: doc.id,
365+
...(this.config.apiVersion !== '8.x' && { _type: '_doc' })
319366
}
320367
})
321368
doc.safeContent = WIKI.models.pages.cleanHTML(doc.render)

0 commit comments

Comments
 (0)