forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[data] change KQL node builder to not generate recursive and/or claus…
…es (elastic#89345) (elastic#89890) resolves elastic#88367 Prior to this PR, the KQL node_builder code was using recursion to generate "and" & "or" expressions. Eg, `and(foo1=bar1, foo2=bar2, foo3=bar3)` would be generated as if was specified as `and(foo1=bar1, and(foo2=bar2, foo3=bar3))`. Calls to the builder with long lists of expressions would generate nested JSON as deep as the lists are long. This is problematic, as Elasticsearch is changing the default limit on nested bools to 20 levels, and alerting already generates nested bools greater than that limit. See: elastic/elasticsearch#55303 This PR changes the generated shape of above, so that all the nodes are at the same level, instead of the previous "recursive" treatment.
- Loading branch information
Showing
8 changed files
with
1,086 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
280 changes: 280 additions & 0 deletions
280
src/plugins/data/common/es_query/kuery/node_types/node_builder.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* and the Server Side Public License, v 1; you may not use this file except in | ||
* compliance with, at your election, the Elastic License or the Server Side | ||
* Public License, v 1. | ||
*/ | ||
|
||
import { nodeBuilder } from './node_builder'; | ||
import { toElasticsearchQuery } from '../index'; | ||
|
||
describe('nodeBuilder', () => { | ||
describe('is method', () => { | ||
test('string value', () => { | ||
const nodes = nodeBuilder.is('foo', 'bar'); | ||
const query = toElasticsearchQuery(nodes); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo": "bar", | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('KueryNode value', () => { | ||
const literalValue = { | ||
type: 'literal' as 'literal', | ||
value: 'bar', | ||
}; | ||
const nodes = nodeBuilder.is('foo', literalValue); | ||
const query = toElasticsearchQuery(nodes); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo": "bar", | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
}); | ||
|
||
describe('and method', () => { | ||
test('single clause', () => { | ||
const nodes = [nodeBuilder.is('foo', 'bar')]; | ||
const query = toElasticsearchQuery(nodeBuilder.and(nodes)); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo": "bar", | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('two clauses', () => { | ||
const nodes = [nodeBuilder.is('foo1', 'bar1'), nodeBuilder.is('foo2', 'bar2')]; | ||
const query = toElasticsearchQuery(nodeBuilder.and(nodes)); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"filter": Array [ | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo1": "bar1", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo2": "bar2", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('three clauses', () => { | ||
const nodes = [ | ||
nodeBuilder.is('foo1', 'bar1'), | ||
nodeBuilder.is('foo2', 'bar2'), | ||
nodeBuilder.is('foo3', 'bar3'), | ||
]; | ||
const query = toElasticsearchQuery(nodeBuilder.and(nodes)); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"filter": Array [ | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo1": "bar1", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo2": "bar2", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo3": "bar3", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
}); | ||
|
||
describe('or method', () => { | ||
test('single clause', () => { | ||
const nodes = [nodeBuilder.is('foo', 'bar')]; | ||
const query = toElasticsearchQuery(nodeBuilder.or(nodes)); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo": "bar", | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('two clauses', () => { | ||
const nodes = [nodeBuilder.is('foo1', 'bar1'), nodeBuilder.is('foo2', 'bar2')]; | ||
const query = toElasticsearchQuery(nodeBuilder.or(nodes)); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo1": "bar1", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo2": "bar2", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
|
||
test('three clauses', () => { | ||
const nodes = [ | ||
nodeBuilder.is('foo1', 'bar1'), | ||
nodeBuilder.is('foo2', 'bar2'), | ||
nodeBuilder.is('foo3', 'bar3'), | ||
]; | ||
const query = toElasticsearchQuery(nodeBuilder.or(nodes)); | ||
expect(query).toMatchInlineSnapshot(` | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo1": "bar1", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo2": "bar2", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
Object { | ||
"bool": Object { | ||
"minimum_should_match": 1, | ||
"should": Array [ | ||
Object { | ||
"match": Object { | ||
"foo3": "bar3", | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
} | ||
`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.