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');