Skip to content

Commit

Permalink
[data.search.autocomplete] Remove usage of ES client transport.request (
Browse files Browse the repository at this point in the history
#106288)

* Use es client method rather than transport.request

* Fix test
  • Loading branch information
lukasolson authored Jul 21, 2021
1 parent 444ef9d commit 4e26bfe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
15 changes: 8 additions & 7 deletions src/plugins/data/server/autocomplete/terms_enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ElasticsearchClient, SavedObjectsClientContract } from 'kibana/server';
import { ConfigSchema } from '../../config';
import type { DeeplyMockedKeys } from '@kbn/utility-types/jest';
import type { ApiResponse } from '@elastic/elasticsearch';
import { TermsEnumResponse } from '@elastic/elasticsearch/api/types';

let savedObjectsClientMock: jest.Mocked<SavedObjectsClientContract>;
let esClientMock: DeeplyMockedKeys<ElasticsearchClient>;
Expand All @@ -29,7 +30,9 @@ describe('_terms_enum suggestions', () => {
const requestHandlerContext = coreMock.createRequestHandlerContext();
savedObjectsClientMock = requestHandlerContext.savedObjects.client;
esClientMock = requestHandlerContext.elasticsearch.client.asCurrentUser;
esClientMock.transport.request.mockResolvedValue((mockResponse as unknown) as ApiResponse);
esClientMock.termsEnum.mockResolvedValue(
(mockResponse as unknown) as ApiResponse<TermsEnumResponse>
);
});

it('calls the _terms_enum API with the field, query, filters, and config tiers', async () => {
Expand All @@ -44,7 +47,7 @@ describe('_terms_enum suggestions', () => {
{ name: 'field_name', type: 'string' }
);

const [[args]] = esClientMock.transport.request.mock.calls;
const [[args]] = esClientMock.termsEnum.mock.calls;

expect(args).toMatchInlineSnapshot(`
Object {
Expand All @@ -67,8 +70,7 @@ describe('_terms_enum suggestions', () => {
},
"string": "query",
},
"method": "POST",
"path": "/index/_terms_enum",
"index": "index",
}
`);
expect(result).toEqual(mockResponse.body.terms);
Expand All @@ -85,7 +87,7 @@ describe('_terms_enum suggestions', () => {
[]
);

const [[args]] = esClientMock.transport.request.mock.calls;
const [[args]] = esClientMock.termsEnum.mock.calls;

expect(args).toMatchInlineSnapshot(`
Object {
Expand All @@ -108,8 +110,7 @@ describe('_terms_enum suggestions', () => {
},
"string": "query",
},
"method": "POST",
"path": "/index/_terms_enum",
"index": "index",
}
`);
expect(result).toEqual(mockResponse.body.terms);
Expand Down
43 changes: 18 additions & 25 deletions src/plugins/data/server/autocomplete/terms_enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { estypes } from '@elastic/elasticsearch';
import { IFieldType } from '../../common';
import { findIndexPatternById, getFieldByName } from '../index_patterns';
import { shimAbortSignal } from '../search';
import { getKbnServerError } from '../../../kibana_utils/server';
import { ConfigSchema } from '../../config';

export async function termsEnumSuggestions(
Expand All @@ -31,32 +30,26 @@ export async function termsEnumSuggestions(
field = indexPattern && getFieldByName(fieldName, indexPattern);
}

try {
const promise = esClient.transport.request({
method: 'POST',
path: encodeURI(`/${index}/_terms_enum`),
body: {
field: field?.name ?? fieldName,
string: query,
index_filter: {
bool: {
must: [
...(filters ?? []),
{
terms: {
_tier: tiers,
},
const promise = esClient.termsEnum({
index,
body: {
field: field?.name ?? fieldName,
string: query,
index_filter: {
bool: {
must: [
...(filters ?? []),
{
terms: {
_tier: tiers,
},
],
},
},
],
},
},
});
},
});

const result = await shimAbortSignal(promise, abortSignal);

return result.body.terms;
} catch (e) {
throw getKbnServerError(e);
}
const result = await shimAbortSignal(promise, abortSignal);
return result.body.terms;
}

0 comments on commit 4e26bfe

Please sign in to comment.