Skip to content

Commit

Permalink
Update getIndicesViaSearch with datasource (opensearch-project#2176)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristen Tian <[email protected]>

Signed-off-by: Kristen Tian <[email protected]>
  • Loading branch information
kristenTian committed Sep 14, 2022
1 parent c9af194 commit 161bdbc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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');
Expand All @@ -101,6 +126,7 @@ describe('getIndices', () => {
getIndexTags,
pattern: '*:opensearch-dashboards',
searchClient,
dataSourceId,
});

expect(http.get).toHaveBeenCalled();
Expand All @@ -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);
});

Expand Down Expand Up @@ -168,6 +201,7 @@ describe('getIndices', () => {
getIndexTags,
pattern: 'opensearch-dashboards',
searchClient,
dataSourceId,
});
expect(result.length).toBe(0);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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<MatchedItem[]> =>
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()
Expand Down Expand Up @@ -221,6 +209,7 @@ export async function getIndices({
pattern,
searchClient,
showAllIndices,
dataSourceId,
}).catch(() => []);
requests.push(promiseSearch);
}
Expand Down Expand Up @@ -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;
};

0 comments on commit 161bdbc

Please sign in to comment.