[data.search.autocomplete] Remove usage of ES client transport.request (#106288)

* Use es client method rather than transport.request

* Fix test
This commit is contained in:
Lukas Olson 2021-07-21 09:10:53 -07:00 committed by GitHub
parent 444ef9d98b
commit 4e26bfe4da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 32 deletions

View file

@ -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>;
@ -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 () => {
@ -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 {
@ -67,8 +70,7 @@ describe('_terms_enum suggestions', () => {
},
"string": "query",
},
"method": "POST",
"path": "/index/_terms_enum",
"index": "index",
}
`);
expect(result).toEqual(mockResponse.body.terms);
@ -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 {
@ -108,8 +110,7 @@ describe('_terms_enum suggestions', () => {
},
"string": "query",
},
"method": "POST",
"path": "/index/_terms_enum",
"index": "index",
}
`);
expect(result).toEqual(mockResponse.body.terms);

View file

@ -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(
@ -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;
}