From 104b6b19487a49c6678fa7529c88a4517d6549cd Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Tue, 28 Mar 2023 09:44:34 +0300 Subject: [PATCH] fix: elastic search for missing or no indexes this occurs when the index is missing during the search for example the TechDocs provider creates the index when initiating, but if there's no docs to index, it removes the index but still tries to search from it. fixes #16423 Signed-off-by: Heikki Hellgren --- .changeset/shaggy-crews-carry.md | 5 +++++ .../ElasticSearchClientWrapper.test.ts | 21 ++++++++++++++++--- .../src/engines/ElasticSearchClientWrapper.ts | 15 +++++++++++-- 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 .changeset/shaggy-crews-carry.md diff --git a/.changeset/shaggy-crews-carry.md b/.changeset/shaggy-crews-carry.md new file mode 100644 index 0000000000..db9aa5225b --- /dev/null +++ b/.changeset/shaggy-crews-carry.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-search-backend-module-elasticsearch': patch +--- + +fix ElasticSearch throwing error when index is missing diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts index 2677eb1420..7355daf3b9 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.test.ts @@ -119,7 +119,12 @@ describe('ElasticSearchClientWrapper', () => { it('search', async () => { const wrapper = ElasticSearchClientWrapper.fromClientOptions(esOptions); - const searchInput = { index: 'xyz', body: { eg: 'etc' } }; + const searchInput = { + index: 'xyz', + body: { eg: 'etc' }, + ignore_unavailable: true, + allow_no_indices: true, + }; const result = (await wrapper.search(searchInput)) as any; // Should call the ElasticSearch client's search with expected input. @@ -241,7 +246,12 @@ describe('ElasticSearchClientWrapper', () => { it('search', async () => { const wrapper = ElasticSearchClientWrapper.fromClientOptions(osOptions); - const searchInput = { index: 'xyz', body: { eg: 'etc' } }; + const searchInput = { + index: 'xyz', + body: { eg: 'etc' }, + ignore_unavailable: true, + allow_no_indices: true, + }; const result = (await wrapper.search(searchInput)) as any; // Should call the OpenSearch client's search with expected input. @@ -350,7 +360,12 @@ describe('ElasticSearchClientWrapper', () => { const wrapper = ElasticSearchClientWrapper.fromClientOptions(osOptions); expect(OpenSearchClient).toHaveBeenCalledWith(osOptions); - const searchInput = { index: 'xyz', body: { eg: 'etc' } }; + const searchInput = { + index: 'xyz', + body: { eg: 'etc' }, + ignore_unavailable: true, + allow_no_indices: true, + }; const result = (await wrapper.search(searchInput)) as any; expect(result.client).toBe('os'); expect(result.args).toStrictEqual(searchInput); diff --git a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.ts b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.ts index 395f861223..a7aca3d03f 100644 --- a/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.ts +++ b/plugins/search-backend-module-elasticsearch/src/engines/ElasticSearchClientWrapper.ts @@ -91,12 +91,23 @@ export class ElasticSearchClientWrapper { } search(options: { index: string | string[]; body: Object }) { + const searchOptions = { + ignore_unavailable: true, + allow_no_indices: true, + }; + if (this.openSearchClient) { - return this.openSearchClient.search(options); + return this.openSearchClient.search({ + ...options, + ...searchOptions, + }); } if (this.elasticSearchClient) { - return this.elasticSearchClient.search(options); + return this.elasticSearchClient.search({ + ...options, + ...searchOptions, + }); } throw new Error('No client defined');