mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
# Backport This will backport the following commits from `main` to `8.8`: - [[KQL] Fix node builder with empty array (#155901)](https://github.com/elastic/kibana/pull/155901) <!--- Backport version: 8.9.7 --> ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) <!--BACKPORT [{"author":{"name":"Lukas Olson","email":"lukas@elastic.co"},"sourceCommit":{"committedDate":"2023-04-27T18:28:12Z","message":"[KQL] Fix node builder with empty array (#155901)\n\n## Summary\r\n\r\nFixes https://github.com/elastic/kibana/issues/95657.\r\n\r\nUpdates the logic for `nodeBuilder` when passing an empty array to\r\n`nodeBuilder.and`/`nodeBuilder.or` to actually return a `KueryNode`\r\ninstead of `undefined`.\r\n\r\n### Checklist\r\n\r\n- [ ]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios","sha":"654287b3adc928dd1582998504b7ed043259c3ae","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:KQL","release_note:skip","backport:skip","Team:DataDiscovery","v8.9.0"],"number":155901,"url":"https://github.com/elastic/kibana/pull/155901","mergeCommit":{"message":"[KQL] Fix node builder with empty array (#155901)\n\n## Summary\r\n\r\nFixes https://github.com/elastic/kibana/issues/95657.\r\n\r\nUpdates the logic for `nodeBuilder` when passing an empty array to\r\n`nodeBuilder.and`/`nodeBuilder.or` to actually return a `KueryNode`\r\ninstead of `undefined`.\r\n\r\n### Checklist\r\n\r\n- [ ]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios","sha":"654287b3adc928dd1582998504b7ed043259c3ae"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/155901","number":155901,"mergeCommit":{"message":"[KQL] Fix node builder with empty array (#155901)\n\n## Summary\r\n\r\nFixes https://github.com/elastic/kibana/issues/95657.\r\n\r\nUpdates the logic for `nodeBuilder` when passing an empty array to\r\n`nodeBuilder.and`/`nodeBuilder.or` to actually return a `KueryNode`\r\ninstead of `undefined`.\r\n\r\n### Checklist\r\n\r\n- [ ]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas added for features that require explanation or tutorials\r\n- [x] [Unit or functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere updated or added to match the most common scenarios","sha":"654287b3adc928dd1582998504b7ed043259c3ae"}}]}] BACKPORT--> Co-authored-by: Lukas Olson <lukas@elastic.co>
This commit is contained in:
parent
1d1b4dfbba
commit
f657db7004
4 changed files with 53 additions and 6 deletions
|
@ -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));
|
||||
|
|
|
@ -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<RangeFilterParams, 'gt' | 'gte' | 'lt' | 'lte'>,
|
||||
value: number | string
|
||||
) => {
|
||||
): KueryNode => {
|
||||
return nodeTypes.function.buildNodeWithArgumentNodes('range', [
|
||||
nodeTypes.literal.buildNode(fieldName),
|
||||
operator,
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue