Skip to content

Commit

Permalink
mitigate kql bug (elastic#70712)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
flash1293 and elasticmachine committed Jul 9, 2020
1 parent 7dad41b commit 5e932ac
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ describe('IndexPattern Data Panel', () => {
};
}

async function testExistenceLoading(stateChanges?: unknown, propChanges?: unknown) {
const props = testProps();
async function testExistenceLoading(
stateChanges?: unknown,
propChanges?: unknown,
props = testProps()
) {
const inst = mountWithIntl(<IndexPatternDataPanel {...props} />);

await act(async () => {
Expand Down Expand Up @@ -536,6 +539,25 @@ describe('IndexPattern Data Panel', () => {
expect(core.http.post).toHaveBeenCalledTimes(2);
expect(overlapCount).toEqual(0);
});

it("should default to empty dsl if query can't be parsed", async () => {
const props = {
...testProps(),
query: {
language: 'kuery',
query: '@timestamp : NOT *',
},
};
await testExistenceLoading(undefined, undefined, props);

expect((props.core.http.post as jest.Mock).mock.calls[0][1].body).toContain(
JSON.stringify({
must_not: {
match_all: {},
},
})
);
});
});

describe('displaying field list', () => {
Expand Down
25 changes: 23 additions & 2 deletions x-pack/plugins/lens/public/indexpattern_datasource/datapanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n/react';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { DataPublicPluginStart, EsQueryConfig, Query, Filter } from 'src/plugins/data/public';
import { DatasourceDataPanelProps, DataType, StateSetter } from '../types';
import { ChildDragDropProvider, DragContextState } from '../drag_drop';
import { FieldItem } from './field_item';
Expand Down Expand Up @@ -74,6 +74,27 @@ const fieldTypeNames: Record<DataType, string> = {
ip: i18n.translate('xpack.lens.datatypes.ipAddress', { defaultMessage: 'IP' }),
};

// Wrapper around esQuery.buildEsQuery, handling errors (e.g. because a query can't be parsed) by
// returning a query dsl object not matching anything
function buildSafeEsQuery(
indexPattern: IIndexPattern,
query: Query,
filters: Filter[],
queryConfig: EsQueryConfig
) {
try {
return esQuery.buildEsQuery(indexPattern, query, filters, queryConfig);
} catch (e) {
return {
bool: {
must_not: {
match_all: {},
},
},
};
}
}

export function IndexPatternDataPanel({
setState,
state,
Expand Down Expand Up @@ -106,7 +127,7 @@ export function IndexPatternDataPanel({
timeFieldName: indexPatterns[id].timeFieldName,
}));

const dslQuery = esQuery.buildEsQuery(
const dslQuery = buildSafeEsQuery(
indexPatterns[currentIndexPatternId] as IIndexPattern,
query,
filters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,12 @@ export const InnerFieldItem = function InnerFieldItem(props: FieldItemProps) {
className={`lnsFieldItem__info ${infoIsOpen ? 'lnsFieldItem__info-isOpen' : ''}`}
data-test-subj={`lnsFieldListPanelField-${field.name}`}
onClick={() => {
togglePopover();
if (exists) {
togglePopover();
}
}}
onKeyPress={(event) => {
if (event.key === 'ENTER') {
if (exists && event.key === 'ENTER') {
togglePopover();
}
}}
Expand Down

0 comments on commit 5e932ac

Please sign in to comment.