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 <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-03-28 09:44:34 +03:00
committed by Renan Mendes Carvalho
parent 9930c700af
commit 104b6b1948
3 changed files with 36 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-search-backend-module-elasticsearch': patch
---
fix ElasticSearch throwing error when index is missing
@@ -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);
@@ -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');