diff --git a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.test.ts b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.test.ts index 0d8b215394a0..56ae8515ddde 100644 --- a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.test.ts +++ b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.test.ts @@ -74,11 +74,32 @@ const searchClient = () => observer.next(successfulSearchResponse); observer.complete(); }) as any; +const dataSourceId = 'dataSourceId'; const http = httpServiceMock.createStartContract(); http.get.mockResolvedValue(successfulResolveResponse); describe('getIndices', () => { + it('should work in a basic case with data source', async () => { + const uncalledSearchClient = jest.fn(); + const result = await getIndices({ + http, + getIndexTags, + pattern: 'opensearch-dashboards', + searchClient: uncalledSearchClient, + dataSourceId, + }); + expect(http.get).toHaveBeenCalled(); + expect(http.get).toHaveBeenCalledWith( + expect.anything(), + expect.objectContaining({ query: { data_source: dataSourceId } }) + ); + expect(uncalledSearchClient).not.toHaveBeenCalled(); + expect(result.length).toBe(3); + expect(result[0].name).toBe('f-alias'); + expect(result[1].name).toBe('foo'); + }); + it('should work in a basic case', async () => { const uncalledSearchClient = jest.fn(); const result = await getIndices({ @@ -88,6 +109,10 @@ describe('getIndices', () => { searchClient: uncalledSearchClient, }); expect(http.get).toHaveBeenCalled(); + expect(http.get).toHaveBeenCalledWith( + expect.anything(), + expect.not.objectContaining({ query: { data_source: dataSourceId } }) + ); expect(uncalledSearchClient).not.toHaveBeenCalled(); expect(result.length).toBe(3); expect(result[0].name).toBe('f-alias'); @@ -101,6 +126,7 @@ describe('getIndices', () => { getIndexTags, pattern: '*:opensearch-dashboards', searchClient, + dataSourceId, }); expect(http.get).toHaveBeenCalled(); @@ -112,14 +138,21 @@ describe('getIndices', () => { }); it('should ignore ccs query-all', async () => { - expect((await getIndices({ http, getIndexTags, pattern: '*:', searchClient })).length).toBe(0); + expect( + (await getIndices({ http, getIndexTags, pattern: '*:', searchClient, dataSourceId })).length + ).toBe(0); }); it('should ignore a single comma', async () => { - expect((await getIndices({ http, getIndexTags, pattern: ',', searchClient })).length).toBe(0); - expect((await getIndices({ http, getIndexTags, pattern: ',*', searchClient })).length).toBe(0); expect( - (await getIndices({ http, getIndexTags, pattern: ',foobar', searchClient })).length + (await getIndices({ http, getIndexTags, pattern: ',', searchClient, dataSourceId })).length + ).toBe(0); + expect( + (await getIndices({ http, getIndexTags, pattern: ',*', searchClient, dataSourceId })).length + ).toBe(0); + expect( + (await getIndices({ http, getIndexTags, pattern: ',foobar', searchClient, dataSourceId })) + .length ).toBe(0); }); @@ -168,6 +201,7 @@ describe('getIndices', () => { getIndexTags, pattern: 'opensearch-dashboards', searchClient, + dataSourceId, }); expect(result.length).toBe(0); }); diff --git a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts index bc3b4e8ad7f7..39bd044d8011 100644 --- a/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts +++ b/src/plugins/index_pattern_management/public/components/create_index_pattern_wizard/lib/get_indices.ts @@ -34,7 +34,11 @@ import { i18n } from '@osd/i18n'; import { map, scan } from 'rxjs/operators'; import { IndexPatternCreationConfig } from '../../../../../index_pattern_management/public'; import { MatchedItem, ResolveIndexResponse, ResolveIndexResponseItemIndexAttrs } from '../types'; -import { DataPublicPluginStart, IOpenSearchSearchResponse } from '../../../../../data/public'; +import { + DataPublicPluginStart, + IOpenSearchSearchRequest, + IOpenSearchSearchResponse, +} from '../../../../../data/public'; import { MAX_SEARCH_SIZE } from '../constants'; const aliasLabel = i18n.translate('indexPatternManagement.aliasLabel', { defaultMessage: 'Alias' }); @@ -80,35 +84,19 @@ export const searchResponseToArray = ( }; export const getIndicesViaSearch = async ({ - // todo: #2151 getIndexTags, pattern, searchClient, showAllIndices, + dataSourceId, }: { getIndexTags: IndexPatternCreationConfig['getIndexTags']; pattern: string; searchClient: DataPublicPluginStart['search']['search']; showAllIndices: boolean; + dataSourceId?: string; }): Promise => - searchClient({ - params: { - ignoreUnavailable: true, - expand_wildcards: showAllIndices ? 'all' : 'open', - index: pattern, - body: { - size: 0, // no hits - aggs: { - indices: { - terms: { - field: '_index', - size: MAX_SEARCH_SIZE, - }, - }, - }, - }, - }, - }) + searchClient(buildSearchRequest(showAllIndices, pattern, dataSourceId)) .pipe(map(searchResponseToArray(getIndexTags, showAllIndices))) .pipe(scan((accumulator = [], value) => accumulator.join(value))) .toPromise() @@ -221,6 +209,7 @@ export async function getIndices({ pattern, searchClient, showAllIndices, + dataSourceId, }).catch(() => []); requests.push(promiseSearch); } @@ -285,3 +274,30 @@ const buildQuery = (showAllIndices: boolean, dataSourceId?: string) => { return query; }; + +const buildSearchRequest = (showAllIndices: boolean, pattern: string, dataSourceId?: string) => { + const request: IOpenSearchSearchRequest = { + params: { + ignoreUnavailable: true, + expand_wildcards: showAllIndices ? 'all' : 'open', + index: pattern, + body: { + size: 0, // no hits + aggs: { + indices: { + terms: { + field: '_index', + size: MAX_SEARCH_SIZE, + }, + }, + }, + }, + }, + }; + + if (dataSourceId) { + request.dataSourceId = dataSourceId; + } + + return request; +};