diff --git a/packages/kbn-es-query/src/kuery/node_types/node_builder.test.ts b/packages/kbn-es-query/src/kuery/node_types/node_builder.test.ts index 46e21245bf333..d414788afec1e 100644 --- a/packages/kbn-es-query/src/kuery/node_types/node_builder.test.ts +++ b/packages/kbn-es-query/src/kuery/node_types/node_builder.test.ts @@ -55,6 +55,25 @@ describe('nodeBuilder', () => { }); describe('and method', () => { + test('no clauses', () => { + const node = nodeBuilder.and([]); + const query = toElasticsearchQuery(node); + expect(node).toMatchInlineSnapshot(` + Object { + "arguments": Array [], + "function": "and", + "type": "function", + } + `); + expect(query).toMatchInlineSnapshot(` + Object { + "bool": Object { + "filter": Array [], + }, + } + `); + }); + test('single clause', () => { const nodes = [nodeBuilder.is('foo', 'bar')]; const query = toElasticsearchQuery(nodeBuilder.and(nodes)); @@ -166,6 +185,26 @@ describe('nodeBuilder', () => { }); describe('or method', () => { + test('no clauses', () => { + const node = nodeBuilder.or([]); + const query = toElasticsearchQuery(node); + expect(node).toMatchInlineSnapshot(` + Object { + "arguments": Array [], + "function": "or", + "type": "function", + } + `); + expect(query).toMatchInlineSnapshot(` + Object { + "bool": Object { + "minimum_should_match": 1, + "should": Array [], + }, + } + `); + }); + test('single clause', () => { const nodes = [nodeBuilder.is('foo', 'bar')]; const query = toElasticsearchQuery(nodeBuilder.or(nodes)); diff --git a/packages/kbn-es-query/src/kuery/node_types/node_builder.ts b/packages/kbn-es-query/src/kuery/node_types/node_builder.ts index 948985c965378..80fcd9607c7ab 100644 --- a/packages/kbn-es-query/src/kuery/node_types/node_builder.ts +++ b/packages/kbn-es-query/src/kuery/node_types/node_builder.ts @@ -10,23 +10,23 @@ import type { RangeFilterParams } from '../../filters'; import { KueryNode, nodeTypes } from '../types'; export const nodeBuilder = { - is: (fieldName: string, value: string | KueryNode) => { + is: (fieldName: string, value: string | KueryNode): KueryNode => { return nodeTypes.function.buildNodeWithArgumentNodes('is', [ nodeTypes.literal.buildNode(fieldName), typeof value === 'string' ? nodeTypes.literal.buildNode(value) : value, ]); }, or: (nodes: KueryNode[]): KueryNode => { - return nodes.length > 1 ? nodeTypes.function.buildNode('or', nodes) : nodes[0]; + return nodes.length === 1 ? nodes[0] : nodeTypes.function.buildNode('or', nodes); }, and: (nodes: KueryNode[]): KueryNode => { - return nodes.length > 1 ? nodeTypes.function.buildNode('and', nodes) : nodes[0]; + return nodes.length === 1 ? nodes[0] : nodeTypes.function.buildNode('and', nodes); }, range: ( fieldName: string, operator: keyof Pick, value: number | string - ) => { + ): KueryNode => { return nodeTypes.function.buildNodeWithArgumentNodes('range', [ nodeTypes.literal.buildNode(fieldName), operator, diff --git a/src/plugins/data/server/search/session/session_service.test.ts b/src/plugins/data/server/search/session/session_service.test.ts index 38a4f027765df..3f79049fa9666 100644 --- a/src/plugins/data/server/search/session/session_service.test.ts +++ b/src/plugins/data/server/search/session/session_service.test.ts @@ -575,7 +575,11 @@ describe('SearchSessionService', () => { const [[findOptions]] = savedObjectsClient.find.mock.calls; expect(findOptions).toMatchInlineSnapshot(` Object { - "filter": undefined, + "filter": Object { + "arguments": Array [], + "function": "and", + "type": "function", + }, "page": 0, "perPage": 5, "type": "search-session", diff --git a/x-pack/plugins/alerting/server/lib/convert_rule_ids_to_kuery_node.test.ts b/x-pack/plugins/alerting/server/lib/convert_rule_ids_to_kuery_node.test.ts index 0c17c68d6162b..258133dab6118 100644 --- a/x-pack/plugins/alerting/server/lib/convert_rule_ids_to_kuery_node.test.ts +++ b/x-pack/plugins/alerting/server/lib/convert_rule_ids_to_kuery_node.test.ts @@ -61,6 +61,10 @@ describe('convertRuleIdsToKueryNode', () => { }); test('should convert empty ids array correctly', () => { - expect(convertRuleIdsToKueryNode([])).toEqual(undefined); + expect(convertRuleIdsToKueryNode([])).toEqual({ + arguments: [], + function: 'or', + type: 'function', + }); }); });