Skip to content

Commit

Permalink
[SearchSource] Deserialize query string options for serverside ES Que…
Browse files Browse the repository at this point in the history
…ry (#90050)

* [SearchSource] Deserialize query string options for serverside ES Query

* fix test
  • Loading branch information
tsullivan authored Feb 2, 2021
1 parent 198c6fb commit 0370f6b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ describe('Query decorator', () => {
expect(decoratedQuery).toEqual({ query_string: { query: '*', analyze_wildcard: true } });
});

test('should merge in serialized query string options', () => {
const queryStringOptions = '{ "analyze_wildcard": true }';
const decoratedQuery = decorateQuery({ query_string: { query: '*' } }, queryStringOptions);

expect(decoratedQuery).toEqual({ query_string: { query: '*', analyze_wildcard: true } });
});

test('should add a default of a time_zone parameter if one is provided', () => {
const decoratedQuery = decorateQuery(
{ query_string: { query: '*' } },
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/data/common/es_query/es_query/decorate_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ import { DslQuery, isEsQueryString } from './es_query_dsl';

export function decorateQuery(
query: DslQuery,
queryStringOptions: Record<string, any>,
queryStringOptions: Record<string, any> | string,
dateFormatTZ?: string
) {
if (isEsQueryString(query)) {
// NOTE queryStringOptions comes from UI Settings and, in server context, is a serialized string
// https://github.com/elastic/kibana/issues/89902
if (typeof queryStringOptions === 'string') {
queryStringOptions = JSON.parse(queryStringOptions);
}

extend(query.query_string, queryStringOptions);
if (dateFormatTZ) {
defaults(query.query_string, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ describe('AggConfig Filters', () => {
const aggConfigs = getAggConfigs();
const filter = createFilterFilters(aggConfigs.aggs[0] as IBucketAggConfig, 'type:nginx');

expect(filter).toMatchInlineSnapshot(`
Object {
"meta": Object {
"alias": "type:nginx",
"index": "1234",
},
"query": Object {
"bool": Object {
"filter": Array [],
"must": Array [
Object {
"query_string": Object {
"query": "type:nginx",
"time_zone": "dateFormat:tz",
},
},
],
"must_not": Array [],
"should": Array [],
},
},
}
`);

expect(filter!.query.bool.must[0].query_string.query).toBe('type:nginx');
expect(filter!.meta).toHaveProperty('index', '1234');
expect(filter!.meta).toHaveProperty('alias', 'type:nginx');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mockGetConfig = jest.fn().mockImplementation((key: string) => {
['P1DT', 'YYYY-MM-DD'],
['P1YT', 'YYYY'],
],
'query:queryString:options': {},
};
return config[key] ?? key;
});
Expand Down

0 comments on commit 0370f6b

Please sign in to comment.