Skip to content

Commit dac720d

Browse files
authored
Add a cluster setting to disallow expensive queries (#51385) (#52279)
Add a new cluster setting `search.allow_expensive_queries` which by default is `true`. If set to `false`, certain queries that have usually slow performance cannot be executed and an error message is returned. - Queries that need to do linear scans to identify matches: - Script queries - Queries that have a high up-front cost: - Fuzzy queries - Regexp queries - Prefix queries (without index_prefixes enabled - Wildcard queries - Range queries on text and keyword fields - Joining queries - HasParent queries - HasChild queries - ParentId queries - Nested queries - Queries on deprecated 6.x geo shapes (using PrefixTree implementation) - Queries that may have a high per-document cost: - Script score queries - Percolate queries Closes: #29050 (cherry picked from commit a8b39ed)
1 parent 40b58e6 commit dac720d

File tree

83 files changed

+1371
-152
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+1371
-152
lines changed

docs/reference/mapping/types/geo-shape.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ between index size and a reasonable level of precision of 50m at the
252252
equator. This allows for indexing tens of millions of shapes without
253253
overly bloating the resulting index too much relative to the input size.
254254

255+
[NOTE]
256+
Geo-shape queries on geo-shapes implemented with PrefixTrees will not be executed if
257+
<<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set to false.
258+
255259
[[input-structure]]
256260
[float]
257261
==== Input Structure

docs/reference/query-dsl.asciidoc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@ or to alter their behaviour (such as the
2525

2626
Query clauses behave differently depending on whether they are used in
2727
<<query-filter-context,query context or filter context>>.
28+
29+
[[query-dsl-allow-expensive-queries]]
30+
Allow expensive queries::
31+
Certain types of queries will generally execute slowly due to the way they are implemented, which can affect
32+
the stability of the cluster. Those queries can be categorised as follows:
33+
* Queries that need to do linear scans to identify matches:
34+
** <<query-dsl-script-query, `script queries`>>
35+
* Queries that have a high up-front cost:
36+
** <<query-dsl-fuzzy-query,`fuzzy queries`>>
37+
** <<query-dsl-regexp-query,`regexp queries`>>
38+
** <<query-dsl-prefix-query,`prefix queries`>> without <<index-prefixes, `index_prefixes`>>
39+
** <<query-dsl-wildcard-query, `wildcard queries`>>
40+
** <<query-dsl-range-query, `range queries>> on <<text, `text`>> and <<keyword, `keyword`>> fields
41+
* <<joining-queries, `Joining queries`>>
42+
* Queries on <<prefix-trees, deprecated geo shapes>>
43+
* Queries that may have a high per-document cost:
44+
** <<query-dsl-script-score-query, `script score queries`>>
45+
** <<query-dsl-percolate-query, `percolate queries`>>
46+
47+
The execution of such queries can be prevented by setting the value of the `search.allow_expensive_queries`
48+
setting to `false` (defaults to `true`).
2849
--
2950

3051
include::query-dsl/query_filter_context.asciidoc[]
@@ -51,4 +72,4 @@ include::query-dsl/minimum-should-match.asciidoc[]
5172

5273
include::query-dsl/multi-term-rewrite.asciidoc[]
5374

54-
include::query-dsl/regexp-syntax.asciidoc[]
75+
include::query-dsl/regexp-syntax.asciidoc[]

docs/reference/query-dsl/fuzzy-query.asciidoc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,8 @@ adjacent characters (ab → ba). Defaults to `true`.
9797

9898
`rewrite`::
9999
(Optional, string) Method used to rewrite the query. For valid values and more
100-
information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
100+
information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
101+
102+
==== Notes
103+
Fuzzy queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
104+
is set to false.

docs/reference/query-dsl/geo-shape-query.asciidoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,7 @@ and will not match any documents for this query. This can be useful when
161161
querying multiple indexes which might have different mappings. When set to
162162
`false` (the default value) the query will throw an exception if the field
163163
is not mapped.
164+
165+
==== Notes
166+
Geo-shape queries on geo-shapes implemented with <<prefix-trees, `PrefixTrees`>> will not be executed if
167+
<<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set to false.

docs/reference/query-dsl/joining-queries.asciidoc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ include::has-parent-query.asciidoc[]
2929

3030
include::parent-id-query.asciidoc[]
3131

32-
32+
=== Notes
33+
==== Allow expensive queries
34+
Joining queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
35+
is set to false.

docs/reference/query-dsl/percolate-query.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,8 @@ being percolated, as opposed to a single index as we do in examples. There are a
693693
allows for fields to be stored in a denser, more efficient way.
694694
- Percolate queries do not scale in the same way as other queries, so percolation performance may benefit from using
695695
a different index configuration, like the number of primary shards.
696+
697+
=== Notes
698+
==== Allow expensive queries
699+
Percolate queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
700+
is set to false.

docs/reference/query-dsl/prefix-query.asciidoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@ GET /_search
6464
You can speed up prefix queries using the <<index-prefixes,`index_prefixes`>>
6565
mapping parameter. If enabled, {es} indexes prefixes between 2 and 5
6666
characters in a separate field. This lets {es} run prefix queries more
67-
efficiently at the cost of a larger index.
67+
efficiently at the cost of a larger index.
68+
69+
[[prefix-query-allow-expensive-queries]]
70+
===== Allow expensive queries
71+
Prefix queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
72+
is set to false. However, if <<index-prefixes, `index_prefixes`>> are enabled, an optimised query is built which
73+
is not considered slow, and will be executed in spite of this setting.

docs/reference/query-dsl/query-string-query.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,9 @@ The example above creates a boolean query:
537537
`(blended(terms:[field2:this, field1:this]) blended(terms:[field2:that, field1:that]) blended(terms:[field2:thus, field1:thus]))~2`
538538

539539
that matches documents with at least two of the three per-term blended queries.
540+
541+
==== Notes
542+
===== Allow expensive queries
543+
Query string query can be internally be transformed to a <<query-dsl-prefix-query, `prefix query`>> which means
544+
that if the prefix queries are disabled as explained <<prefix-query-allow-expensive-queries, here>> the query will not be
545+
executed and an exception will be thrown.

docs/reference/query-dsl/range-query.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ increases the relevance score.
134134
[[range-query-notes]]
135135
==== Notes
136136

137+
[[ranges-on-text-and-keyword]]
138+
===== Using the `range` query with `text` and `keyword` fields
139+
Range queries on <<text, `text`>> or <<keyword, `keyword`>> files will not be executed if
140+
<<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>> is set to false.
141+
137142
[[ranges-on-dates]]
138143
===== Using the `range` query with `date` fields
139144

docs/reference/query-dsl/regexp-query.asciidoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ regular expressions.
8686
`rewrite`::
8787
(Optional, string) Method used to rewrite the query. For valid values and more
8888
information, see the <<query-dsl-multi-term-rewrite, `rewrite` parameter>>.
89+
90+
==== Notes
91+
===== Allow expensive queries
92+
Regexp queries will not be executed if <<query-dsl-allow-expensive-queries, `search.allow_expensive_queries`>>
93+
is set to false.

0 commit comments

Comments
 (0)