[Synthetics] Add msearch helper to Synthetics ES client (#191747)

## Summary

This is a nice-to-have as it simplifies future usage of the
Elasticsearch client's `msearch` API for handing multiple queries to ES
at the same time.

We could also refactor this to consolidate similar helpers in other
places, for instance APM has a very similar

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
This commit is contained in:
Justin Kambic 2024-08-30 13:08:16 -04:00 committed by GitHub
parent 8436f45fd1
commit 7441e9ce84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 100 additions and 1 deletions

View file

@ -5,6 +5,7 @@
* 2.0.
*/
import { MsearchResponse } from '@elastic/elasticsearch/lib/api/types';
import { SyntheticsEsClient } from './lib';
import { savedObjectsClientMock, uiSettingsServiceMock } from '@kbn/core/server/mocks';
import { elasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks';
@ -22,6 +23,73 @@ describe('SyntheticsEsClient', () => {
jest.clearAllMocks();
});
describe('msearch', () => {
it('should call baseESClient.msearch with correct parameters', async () => {
esClient.msearch.mockResolvedValueOnce({
body: {
responses: [
{ aggregations: { aggName: { value: 'str' } } },
{ aggregations: { aggName: { value: 'str' } } },
],
},
} as unknown as MsearchResponse);
const mockSearchParams = [
{
query: {
match_all: {},
},
},
{
query: {
match_all: {},
},
},
];
const result = await syntheticsEsClient.msearch(mockSearchParams);
expect(esClient.msearch).toHaveBeenCalledWith(
{
searches: [
{
index: 'synthetics-*',
ignore_unavailable: true,
},
mockSearchParams[0],
{
index: 'synthetics-*',
ignore_unavailable: true,
},
mockSearchParams[1],
],
},
{ meta: true }
);
expect(result).toMatchInlineSnapshot(`
Object {
"responses": Array [
Object {
"aggregations": Object {
"aggName": Object {
"value": "str",
},
},
},
Object {
"aggregations": Object {
"aggName": Object {
"value": "str",
},
},
},
],
}
`);
});
});
describe('search', () => {
it('should call baseESClient.search with correct parameters', async () => {
const mockSearchParams = {

View file

@ -5,6 +5,10 @@
* 2.0.
*/
import {
MsearchMultisearchBody,
MsearchMultisearchHeader,
} from '@elastic/elasticsearch/lib/api/types';
import {
ElasticsearchClient,
SavedObjectsClientContract,
@ -13,7 +17,7 @@ import {
} from '@kbn/core/server';
import chalk from 'chalk';
import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey';
import type { ESSearchResponse } from '@kbn/es-types';
import type { ESSearchResponse, InferSearchResponseOf } from '@kbn/es-types';
import { RequestStatus } from '@kbn/inspector-plugin/common';
import { InspectResponse } from '@kbn/observability-plugin/typings/common';
import { enableInspectEsQueries } from '@kbn/observability-plugin/common';
@ -116,6 +120,33 @@ export class SyntheticsEsClient {
return res;
}
async msearch<
TDocument = unknown,
TSearchRequest extends estypes.SearchRequest = estypes.SearchRequest
>(
requests: MsearchMultisearchBody[]
): Promise<{ responses: Array<InferSearchResponseOf<TDocument, TSearchRequest>> }> {
const searches: Array<MsearchMultisearchHeader | MsearchMultisearchBody> = [];
for (const request of requests) {
searches.push({ index: SYNTHETICS_INDEX_PATTERN, ignore_unavailable: true });
searches.push(request);
}
const results = await this.baseESClient.msearch(
{
searches,
},
{ meta: true }
);
return {
responses: results.body.responses as unknown as Array<
InferSearchResponseOf<TDocument, TSearchRequest>
>,
};
}
async count<TParams>(params: TParams): Promise<CountResponse> {
let res: any;
let esError: any;