Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/plugins/data/server/autocomplete/terms_agg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const mockResponse = {
},
} as ApiResponse<SearchResponse<any>>;

jest.mock('../index_patterns');

describe('terms agg suggestions', () => {
beforeEach(() => {
const requestHandlerContext = coreMock.createRequestHandlerContext();
Expand Down Expand Up @@ -86,4 +88,50 @@ describe('terms agg suggestions', () => {
]
`);
});

it('calls the _search API with a terms agg and fallback to fieldName when field is null', async () => {
const result = await termsAggSuggestions(
configMock,
savedObjectsClientMock,
esClientMock,
'index',
'fieldName',
'query',
[]
);

const [[args]] = esClientMock.search.mock.calls;

expect(args).toMatchInlineSnapshot(`
Object {
"body": Object {
"aggs": Object {
"suggestions": Object {
"terms": Object {
"execution_hint": "map",
"field": "fieldName",
"include": "query.*",
"shard_size": 10,
},
},
},
"query": Object {
"bool": Object {
"filter": Array [],
},
},
"size": 0,
"terminate_after": 98430,
"timeout": "4513ms",
},
"index": "index",
}
`);
expect(result).toMatchInlineSnapshot(`
Array [
"whoa",
"amazing",
]
`);
});
});
43 changes: 43 additions & 0 deletions src/plugins/data/server/autocomplete/terms_enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const mockResponse = {
body: { terms: ['whoa', 'amazing'] },
};

jest.mock('../index_patterns');

describe('_terms_enum suggestions', () => {
beforeEach(() => {
const requestHandlerContext = coreMock.createRequestHandlerContext();
Expand Down Expand Up @@ -71,4 +73,45 @@ describe('_terms_enum suggestions', () => {
`);
expect(result).toEqual(mockResponse.body.terms);
});

it('calls the _terms_enum API and fallback to fieldName when field is null', async () => {
const result = await termsEnumSuggestions(
configMock,
savedObjectsClientMock,
esClientMock,
'index',
'fieldName',
'query',
[]
);

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

expect(args).toMatchInlineSnapshot(`
Object {
"body": Object {
"field": "fieldName",
"index_filter": Object {
"bool": Object {
"must": Array [
Object {
"terms": Object {
"_tier": Array [
"data_hot",
"data_warm",
"data_content",
],
},
},
],
},
},
"string": "query",
},
"method": "POST",
"path": "/index/_terms_enum",
}
`);
expect(result).toEqual(mockResponse.body.terms);
});
});
2 changes: 1 addition & 1 deletion src/plugins/data/server/autocomplete/terms_enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function termsEnumSuggestions(
method: 'POST',
path: encodeURI(`/${index}/_terms_enum`),
body: {
field: field?.name ?? field,
field: field?.name ?? fieldName,
string: query,
index_filter: {
bool: {
Expand Down