From 62be8ee05043b01daffd3c508e9db7bc221083fe Mon Sep 17 00:00:00 2001 From: scienceasdf Date: Mon, 7 Dec 2020 21:13:59 +0800 Subject: [PATCH 1/3] Feature: Custom configuration for elasticsearch For better search results especially in Chinese, which the standard token analyzer may not work well. --- server/modules/search/elasticsearch/definition.yml | 10 ++++++++-- server/modules/search/elasticsearch/engine.js | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/server/modules/search/elasticsearch/definition.yml b/server/modules/search/elasticsearch/definition.yml index 834f39350e..856f651b02 100644 --- a/server/modules/search/elasticsearch/definition.yml +++ b/server/modules/search/elasticsearch/definition.yml @@ -26,16 +26,22 @@ props: hint: The index name to use during creation default: wiki order: 3 + analyzer: + type: String + title: Analyzer + hint: 'The token analyzer in elasticsearch' + default: simple + order: 4 sniffOnStart: type: Boolean title: Sniff on start hint: 'Should Wiki.js attempt to detect the rest of the cluster on first connect? (Default: off)' default: false - order: 4 + order: 5 sniffInterval: type: Number title: Sniff Interval hint: '0 = disabled, Interval in seconds to check for updated list of nodes in cluster. (Default: 0)' default: 0 - order: 5 + order: 6 diff --git a/server/modules/search/elasticsearch/engine.js b/server/modules/search/elasticsearch/engine.js index 7129c0f6c2..117e9e6413 100644 --- a/server/modules/search/elasticsearch/engine.js +++ b/server/modules/search/elasticsearch/engine.js @@ -57,12 +57,12 @@ module.exports = { const idxBody = { properties: { suggest: { type: 'completion' }, - title: { type: 'text', boost: 10.0 }, - description: { type: 'text', boost: 3.0 }, - content: { type: 'text', boost: 1.0 }, + title: { type: 'text', boost: 10.0, analyzer: this.config.analyzer }, + description: { type: 'text', boost: 3.0, analyzer: this.config.analyzer }, + content: { type: 'text', boost: 1.0, analyzer: this.config.analyzer }, locale: { type: 'keyword' }, path: { type: 'text' }, - tags: { type: 'text', boost: 8.0 } + tags: { type: 'text', boost: 8.0, analyzer: this.config.analyzer } } } await this.client.indices.create({ From 9637064677c5ca4587a6f29c493a7f23a7ac80f0 Mon Sep 17 00:00:00 2001 From: scienceasdf Date: Tue, 8 Dec 2020 16:56:31 +0800 Subject: [PATCH 2/3] Set default analyzer in settings when building index --- server/modules/search/elasticsearch/engine.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/server/modules/search/elasticsearch/engine.js b/server/modules/search/elasticsearch/engine.js index 117e9e6413..132e161340 100644 --- a/server/modules/search/elasticsearch/engine.js +++ b/server/modules/search/elasticsearch/engine.js @@ -57,20 +57,29 @@ module.exports = { const idxBody = { properties: { suggest: { type: 'completion' }, - title: { type: 'text', boost: 10.0, analyzer: this.config.analyzer }, - description: { type: 'text', boost: 3.0, analyzer: this.config.analyzer }, - content: { type: 'text', boost: 1.0, analyzer: this.config.analyzer }, + title: { type: 'text', boost: 10.0 }, + description: { type: 'text', boost: 3.0 }, + content: { type: 'text', boost: 1.0 }, locale: { type: 'keyword' }, path: { type: 'text' }, - tags: { type: 'text', boost: 8.0, analyzer: this.config.analyzer } - } + tags: { type: 'text', boost: 8.0 } + }, } await this.client.indices.create({ index: this.config.indexName, body: { mappings: (this.config.apiVersion === '6.x') ? { _doc: idxBody - } : idxBody + } : idxBody, + settings: { + analysis: { + analyzer: { + default: { + type: this.config.analyzer + } + } + } + } } }) } catch (err) { From e7098df94cba8acb770b2752c2f12069b5ba8fd0 Mon Sep 17 00:00:00 2001 From: scienceasdf Date: Wed, 9 Dec 2020 10:26:05 +0800 Subject: [PATCH 3/3] Remove dangling comma --- server/modules/search/elasticsearch/engine.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/modules/search/elasticsearch/engine.js b/server/modules/search/elasticsearch/engine.js index 132e161340..4a41df8861 100644 --- a/server/modules/search/elasticsearch/engine.js +++ b/server/modules/search/elasticsearch/engine.js @@ -63,7 +63,7 @@ module.exports = { locale: { type: 'keyword' }, path: { type: 'text' }, tags: { type: 'text', boost: 8.0 } - }, + } } await this.client.indices.create({ index: this.config.indexName,