fix: Update engine.js

pull/6904/head
Nicolas Giard 1 year ago committed by GitHub
parent 71ac67d0f7
commit fbfbfee74b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -64,8 +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 })
// 6.x / 7.x return indexExists.body, while 8.x only returns indexExists so we need uniqe conditionals // Elasticsearch 6.x / 7.x
if ((!indexExists.body && this.config.apiVersion !== '8.x') || (!indexExists && this.config.apiVersion === '8.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 = {
@ -79,8 +79,33 @@ module.exports = {
tags: { type: 'text', boost: 8.0 } tags: { type: 'text', boost: 8.0 }
} }
} }
await this.client.indices.create({
index: this.config.indexName,
body: {
mappings: {
_doc: 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))
}
// 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. // 8.x Doesn't support boost in mappings, so we will need to boost at query time.
const idxBody8_x = { const idxBody = {
properties: { properties: {
suggest: { type: 'completion' }, suggest: { type: 'completion' },
title: { type: 'text' }, title: { type: 'text' },
@ -92,21 +117,10 @@ module.exports = {
} }
} }
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({ await this.client.indices.create({
index: this.config.indexName, index: this.config.indexName,
body: { body: {
mappings: (this.config.apiVersion !== '8.x') ? { mappings: idxBody,
_doc: mapping
} : mapping,
settings: { settings: {
analysis: { analysis: {
analyzer: { analyzer: {
@ -118,11 +132,10 @@ 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))
} }
} }
} 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))
} }

Loading…
Cancel
Save