From 54e1280b6a2db8eeb2736eeec0b35874da914ee9 Mon Sep 17 00:00:00 2001 From: Jussi Hallila Date: Tue, 3 Aug 2021 13:07:25 +0200 Subject: [PATCH] * Modify index/alias name handling. * Change config key name to be all lowercase * Documentation updates Signed-off-by: Jussi Hallila --- .changeset/two-ravens-warn.md | 10 +++++----- docs/features/search/search-engines.md | 13 ++++++++----- packages/backend/src/plugins/search.ts | 2 +- .../config.d.ts | 4 ++-- .../src/engines/ElasticSearchSearchEngine.ts | 17 +++++++++++++---- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/.changeset/two-ravens-warn.md b/.changeset/two-ravens-warn.md index 348d4aff21..5659b7bd49 100644 --- a/.changeset/two-ravens-warn.md +++ b/.changeset/two-ravens-warn.md @@ -15,7 +15,7 @@ that environment variables for AWS access key id and secret access key are defin ```yaml search: - elasticSearch: + elasticsearch: provider: aws node: https://my-backstage-search-asdfqwerty.eu-west-1.es.amazonaws.com ``` @@ -26,7 +26,7 @@ Elastic Cloud hosted ElasticSearch uses a Cloud ID to determine the instance of ```yaml search: - elasticSearch: + elasticsearch: provider: elastic cloudId: backstage-elastic:asdfqwertyasdfqwertyasdfqwertyasdfqwerty== auth: @@ -44,7 +44,7 @@ Other ElasticSearch instances can be connected to by using standard ElasticSearc ```yaml search: - elasticSearch: + elasticsearch: node: http://localhost:9200 auth: username: elastic @@ -55,7 +55,7 @@ search: ```yaml search: - elasticSearch: + elasticsearch: node: http://localhost:9200 auth: bearer: token @@ -65,7 +65,7 @@ search: ```yaml search: - elasticSearch: + elasticsearch: node: http://localhost:9200 auth: apiKey: base64EncodedKey diff --git a/docs/features/search/search-engines.md b/docs/features/search/search-engines.md index 0cb0f3507d..a683c8ad84 100644 --- a/docs/features/search/search-engines.md +++ b/docs/features/search/search-engines.md @@ -60,6 +60,9 @@ ElasticSearch needs some additional configuration before it is ready to use within your instance. The configuration options are documented in the [configuration schema definition file.](https://github.com/backstage/backstage/blob/master/plugins/search-backend-module-elasticsearch/config.d.ts) +The underlying functionality is using official ElasticSearch client version 7.x, +meaning that ElasticSearch version 7 is the only one confirmed to be supported. + ## Example configurations ### AWS @@ -72,7 +75,7 @@ to the ```yaml search: - elasticSearch: + elasticsearch: provider: aws node: https://my-backstage-search-asdfqwerty.eu-west-1.es.amazonaws.com ``` @@ -86,7 +89,7 @@ be provided either directly or using environment variables like defined in ```yaml search: - elasticSearch: + elasticsearch: provider: elastic cloudId: backstage-elastic:asdfqwertyasdfqwertyasdfqwertyasdfqwerty== auth: @@ -113,7 +116,7 @@ and how to create a bearer token see ```yaml search: - elasticSearch: + elasticsearch: node: http://localhost:9200 auth: username: elastic @@ -124,7 +127,7 @@ search: ```yaml search: - elasticSearch: + elasticsearch: node: http://localhost:9200 auth: bearer: token @@ -134,7 +137,7 @@ search: ```yaml search: - elasticSearch: + elasticsearch: node: http://localhost:9200 auth: apiKey: base64EncodedKey diff --git a/packages/backend/src/plugins/search.ts b/packages/backend/src/plugins/search.ts index eb13bc9df6..78ce09ee07 100644 --- a/packages/backend/src/plugins/search.ts +++ b/packages/backend/src/plugins/search.ts @@ -30,7 +30,7 @@ export default async function createPlugin({ config, }: PluginEnvironment) { // Initialize a connection to a search engine. - const searchEngine = config.has('search.elasticSearch') + const searchEngine = config.has('search.elasticsearch') ? await ElasticSearchSearchEngine.fromConfig({ logger, config, diff --git a/plugins/search-backend-module-elasticsearch/config.d.ts b/plugins/search-backend-module-elasticsearch/config.d.ts index d808166574..2ea18763fa 100644 --- a/plugins/search-backend-module-elasticsearch/config.d.ts +++ b/plugins/search-backend-module-elasticsearch/config.d.ts @@ -20,7 +20,7 @@ export interface Config { /** * Options for ElasticSearch */ - elasticSearch?: + elasticsearch?: | // elastic = Elastic.co ElasticSearch provider { provider: 'elastic'; @@ -75,7 +75,7 @@ export interface Config { * Authentication credentials for ElasticSearch * If both ApiKey/Bearer token and username+password is provided, tokens take precedence */ - auth: { + auth?: { username?: string; /** diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts index b068df666a..09f2838abf 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchSearchEngine.ts @@ -87,7 +87,7 @@ export class ElasticSearchSearchEngine implements SearchEngine { return new ElasticSearchSearchEngine( await ElasticSearchSearchEngine.constructElasticSearchClient( logger, - config.getConfig('search.elasticSearch'), + config.getConfig('search.elasticsearch'), ), aliasPostfix, indexPrefix, @@ -251,7 +251,7 @@ export class ElasticSearchSearchEngine implements SearchEngine { }); return { results: result.body.hits.hits.map((d: ElasticSearchResult) => ({ - type: d._index.split('__')[0], + type: this.getTypeFromIndex(d._index), document: d._source, })), }; @@ -264,11 +264,20 @@ export class ElasticSearchSearchEngine implements SearchEngine { } } + private readonly indexSeparator = '-index__'; + private constructIndexName(type: string, postFix: string) { - return `${this.indexPrefix}${type}-index__${postFix}`; + return `${this.indexPrefix}${type}${this.indexSeparator}${postFix}`; + } + + private getTypeFromIndex(index: string) { + return index + .substring(this.indexPrefix.length) + .split(this.indexSeparator)[0]; } private constructSearchAlias(type: string) { - return `${this.indexPrefix}${type}__${this.aliasPostfix}`; + const postFix = this.aliasPostfix ? `__${this.aliasPostfix}` : ''; + return `${this.indexPrefix}${type}${postFix}`; } }