From fbfbfee74be266c4a15bccb47f589af5385a471b Mon Sep 17 00:00:00 2001 From: Nicolas Giard Date: Sat, 12 Oct 2024 04:28:08 -0400 Subject: [PATCH] fix: Update engine.js --- server/modules/search/elasticsearch/engine.js | 47 ++++++++++++------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/server/modules/search/elasticsearch/engine.js b/server/modules/search/elasticsearch/engine.js index 8f5b4ddb..9c070059 100644 --- a/server/modules/search/elasticsearch/engine.js +++ b/server/modules/search/elasticsearch/engine.js @@ -64,8 +64,8 @@ module.exports = { async createIndex() { try { 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 - if ((!indexExists.body && this.config.apiVersion !== '8.x') || (!indexExists && this.config.apiVersion === '8.x')) { + // Elasticsearch 6.x / 7.x + if (this.config.apiVersion !== '8.x' && !indexExists.body) { WIKI.logger.info(`(SEARCH/ELASTICSEARCH) Creating index...`) try { const idxBody = { @@ -79,8 +79,33 @@ module.exports = { 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. - const idxBody8_x = { + const idxBody = { properties: { suggest: { type: 'completion' }, 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({ index: this.config.indexName, body: { - mappings: (this.config.apiVersion !== '8.x') ? { - _doc: mapping - } : mapping, + mappings: idxBody, settings: { analysis: { analyzer: { @@ -118,11 +132,10 @@ module.exports = { } } }) - } catch (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)) }