Skip to content

Commit

Permalink
[Synthetics] Add msearch helper to Synthetics ES client (elastic#19…
Browse files Browse the repository at this point in the history
…1747)

## 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 <[email protected]>
  • Loading branch information
justinkambic and shahzad31 authored Aug 30, 2024
1 parent 8436f45 commit 7441e9c
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = {
Expand Down
33 changes: 32 additions & 1 deletion x-pack/plugins/observability_solution/synthetics/server/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* 2.0.
*/

import {
MsearchMultisearchBody,
MsearchMultisearchHeader,
} from '@elastic/elasticsearch/lib/api/types';
import {
ElasticsearchClient,
SavedObjectsClientContract,
Expand All @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7441e9c

Please sign in to comment.