From d9a14a6e90d1a4c65910e287c9c2984ef96ef5a2 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Fri, 24 Jan 2020 17:59:29 +0100 Subject: [PATCH 01/25] Add a cluster setting to disallow slow queries Add a new cluster setting `search.disallow_slow_queries` which by default is `false`. If set to `true` then certain queries (prefix, fuzzy, regexp and wildcard) that have usually slow performance cannot be executed and an exception is thrown. Closes: #29050 --- docs/reference/query-dsl.asciidoc | 10 +- docs/reference/query-dsl/fuzzy-query.asciidoc | 6 +- .../reference/query-dsl/prefix-query.asciidoc | 10 +- .../query-dsl/query-string-query.asciidoc | 6 + .../reference/query-dsl/regexp-query.asciidoc | 5 + .../query-dsl/wildcard-query.asciidoc | 7 +- .../mapper/SearchAsYouTypeFieldTypeTests.java | 10 +- .../ICUCollationKeywordFieldMapper.java | 2 +- .../index/mapper/CollationFieldTypeTests.java | 8 +- .../test/search/320_disallow_queries.yml | 213 ++++++++++++++++++ .../common/settings/ClusterSettings.java | 1 + .../org/elasticsearch/index/IndexModule.java | 16 +- .../org/elasticsearch/index/IndexService.java | 11 +- .../index/mapper/MappedFieldType.java | 5 +- .../index/mapper/StringFieldType.java | 22 +- .../index/query/FuzzyQueryBuilder.java | 2 +- .../index/query/QueryShardContext.java | 36 ++- .../index/search/MatchQuery.java | 3 +- .../index/search/QueryStringQueryParser.java | 2 +- .../search/SimpleQueryStringQueryParser.java | 6 +- .../elasticsearch/indices/IndicesService.java | 15 +- .../elasticsearch/search/SearchService.java | 2 + .../index/mapper/IgnoredFieldTypeTests.java | 22 +- .../index/mapper/KeywordFieldTypeTests.java | 20 +- .../index/mapper/RoutingFieldTypeTests.java | 23 +- .../index/mapper/TextFieldTypeTests.java | 32 ++- .../index/mapper/FieldTypeTestCase.java | 15 ++ .../mapper/FlatObjectFieldMapper.java | 2 +- .../mapper/KeyedFlatObjectFieldTypeTests.java | 14 +- .../mapper/RootFlatObjectFieldTypeTests.java | 23 +- .../xpack/watcher/WatcherPluginTests.java | 3 +- 31 files changed, 496 insertions(+), 56 deletions(-) create mode 100644 rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml diff --git a/docs/reference/query-dsl.asciidoc b/docs/reference/query-dsl.asciidoc index 58ebe3190a352..cbc016b8fb8f4 100644 --- a/docs/reference/query-dsl.asciidoc +++ b/docs/reference/query-dsl.asciidoc @@ -25,6 +25,14 @@ or to alter their behaviour (such as the Query clauses behave differently depending on whether they are used in <>. + +[[query-dsl-disallow-slow]] +== Disallow slow queries +Certain queries like <>, <>, +<> and <> , +that are usually slow performance can affect the cluster performance. +The execution of such queries can be prevented by setting the value of the `search.disallow_slow_queries` +setting to `true` (defaults tp `false`). -- include::query-dsl/query_filter_context.asciidoc[] @@ -51,4 +59,4 @@ include::query-dsl/minimum-should-match.asciidoc[] include::query-dsl/multi-term-rewrite.asciidoc[] -include::query-dsl/regexp-syntax.asciidoc[] \ No newline at end of file +include::query-dsl/regexp-syntax.asciidoc[] diff --git a/docs/reference/query-dsl/fuzzy-query.asciidoc b/docs/reference/query-dsl/fuzzy-query.asciidoc index bb20e0bd7e720..e145b8a3c81d8 100644 --- a/docs/reference/query-dsl/fuzzy-query.asciidoc +++ b/docs/reference/query-dsl/fuzzy-query.asciidoc @@ -97,4 +97,8 @@ adjacent characters (ab → ba). Defaults to `true`. `rewrite`:: (Optional, string) Method used to rewrite the query. For valid values and more -information, see the <>. \ No newline at end of file +information, see the <>. + +==== Notes +Fuzzy queries will not be executed if <> +is set to true. diff --git a/docs/reference/query-dsl/prefix-query.asciidoc b/docs/reference/query-dsl/prefix-query.asciidoc index 780de433aabc0..e6802c428c257 100644 --- a/docs/reference/query-dsl/prefix-query.asciidoc +++ b/docs/reference/query-dsl/prefix-query.asciidoc @@ -64,4 +64,12 @@ GET /_search You can speed up prefix queries using the <> mapping parameter. If enabled, {es} indexes prefixes between 2 and 5 characters in a separate field. This lets {es} run prefix queries more -efficiently at the cost of a larger index. \ No newline at end of file +efficiently at the cost of a larger index. + +[[prefix-query-disallow-slow]] +===== Disallow slow queries +Prefix queries will not be executed if <> +is set to true. If <> are enabled though, an optimised query is +built, which is not considered slow and is executed despite of the fact that the +<> set to true. + diff --git a/docs/reference/query-dsl/query-string-query.asciidoc b/docs/reference/query-dsl/query-string-query.asciidoc index 56eb3b6efb5e5..f1b07c680f0e4 100644 --- a/docs/reference/query-dsl/query-string-query.asciidoc +++ b/docs/reference/query-dsl/query-string-query.asciidoc @@ -537,3 +537,9 @@ The example above creates a boolean query: `(blended(terms:[field2:this, field1:this]) blended(terms:[field2:that, field1:that]) blended(terms:[field2:thus, field1:thus]))~2` that matches documents with at least two of the three per-term blended queries. + +==== Notes +===== Disallow slow queries +Query string query can be internally be transformed to a <> which means +that if the prefix queries are disabled as explained <> the query will not be +executed and an exception will be thrown. diff --git a/docs/reference/query-dsl/regexp-query.asciidoc b/docs/reference/query-dsl/regexp-query.asciidoc index e92424afbc2a5..329259d008ba5 100644 --- a/docs/reference/query-dsl/regexp-query.asciidoc +++ b/docs/reference/query-dsl/regexp-query.asciidoc @@ -86,3 +86,8 @@ regular expressions. `rewrite`:: (Optional, string) Method used to rewrite the query. For valid values and more information, see the <>. + +==== Notes +===== Disallow slow queries +Regexp queries will not be executed if <> +is set to true. diff --git a/docs/reference/query-dsl/wildcard-query.asciidoc b/docs/reference/query-dsl/wildcard-query.asciidoc index 5cc1dacfb6efb..604f19692e4c7 100644 --- a/docs/reference/query-dsl/wildcard-query.asciidoc +++ b/docs/reference/query-dsl/wildcard-query.asciidoc @@ -67,4 +67,9 @@ increases the relevance score. `rewrite`:: (Optional, string) Method used to rewrite the query. For valid values and more information, see the -<>. \ No newline at end of file +<>. + +==== Notes +===== Disallow slow queries +Wildcard queries will not be executed if <> +is set to true. diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index 523de91809145..609b3144fa826 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -26,6 +26,7 @@ import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.Defaults; import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.PrefixFieldType; import org.elasticsearch.index.mapper.SearchAsYouTypeFieldMapper.SearchAsYouTypeFieldType; @@ -100,14 +101,19 @@ public void testPrefixQuery() { // this term should be a length that can be rewriteable to a term query on the prefix field final String withinBoundsTerm = "foo"; - assertThat(fieldType.prefixQuery(withinBoundsTerm, CONSTANT_SCORE_REWRITE, null), + assertThat(fieldType.prefixQuery(withinBoundsTerm, CONSTANT_SCORE_REWRITE, randomMockShardContext()), equalTo(new ConstantScoreQuery(new TermQuery(new Term(PREFIX_NAME, withinBoundsTerm))))); // our defaults don't allow a situation where a term can be too small // this term should be too long to be rewriteable to a term query on the prefix field final String longTerm = "toolongforourprefixfieldthistermis"; - assertThat(fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, null), + assertThat(fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC), equalTo(new PrefixQuery(new Term(NAME, longTerm)))); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } } diff --git a/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java b/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java index 4b29d314356df..883468941a5d8 100644 --- a/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java +++ b/plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java @@ -158,7 +158,7 @@ protected BytesRef indexedValueForSearch(Object value) { @Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, - boolean transpositions) { + boolean transpositions, QueryShardContext context) { throw new UnsupportedOperationException("[fuzzy] queries are not supported on [" + CONTENT_TYPE + "] fields."); } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java index a261e8b3b7e9a..df82de52b140b 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java @@ -102,7 +102,7 @@ public void testRegexpQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); expectThrows(UnsupportedOperationException.class, - () -> ft.regexpQuery("foo.*", 0, 10, null, null)); + () -> ft.regexpQuery("foo.*", 0, 10, null, randomMockShardContext())); } public void testFuzzyQuery() { @@ -110,7 +110,7 @@ public void testFuzzyQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); expectThrows(UnsupportedOperationException.class, - () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true)); + () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, randomMockShardContext())); } public void testPrefixQuery() { @@ -118,7 +118,7 @@ public void testPrefixQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); expectThrows(UnsupportedOperationException.class, - () -> ft.prefixQuery("prefix", null, null)); + () -> ft.prefixQuery("prefix", null, randomMockShardContext())); } public void testWildcardQuery() { @@ -126,7 +126,7 @@ public void testWildcardQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); expectThrows(UnsupportedOperationException.class, - () -> ft.wildcardQuery("foo*", null, null)); + () -> ft.wildcardQuery("foo*", null, randomMockShardContext())); } public void testRangeQuery() { diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml new file mode 100644 index 0000000000000..04c674bdababf --- /dev/null +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -0,0 +1,213 @@ +--- +setup: + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + - do: + indices.create: + index: test + body: + mappings: + properties: + text: + type: text + analyzer: standard + - do: + bulk: + refresh: true + body: + - '{"index": {"_index": "test", "_id": "1"}}' + - '{"text" : "Some like it hot, some like it cold"}' + - '{"index": {"_index": "test", "_id": "2"}}' + - '{"text" : "Its cold outside, theres no kind of atmosphere"}' + - '{"index": {"_index": "test", "_id": "3"}}' + - '{"text" : "Baby its cold there outside"}' + - '{"index": {"_index": "test", "_id": "4"}}' + - '{"text" : "Outside it is cold and wet"}' + +--- +teardown: + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: null + +--- +"Test disallow slow queries": + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + ### Check for initial setting = null -> false + - do: + cluster.get_settings: + flat_settings: true + + - match: {search.disallow_slow_queries: null} + + ### Prefix + - do: + search: + index: test + body: + query: + prefix: + text: + value: out + + - match: { hits.total.value: 3 } + + ### Fuzzy + - do: + search: + index: test + body: + query: + fuzzy: + text: + value: outwide + + - match: { hits.total.value: 3 } + + + ### Regexp + - do: + search: + index: test + body: + query: + regexp: + text: + value: .*ou.*id.* + + - match: { hits.total.value: 3 } + + ### Wildcard + - do: + search: + index: test + body: + query: + wildcard: + text: + value: out?ide + + - match: { hits.total.value: 3 } + + ### Update setting to true + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: "true" + flat_settings: true + + - match: {transient: {search.disallow_slow_queries: "true"}} + + ### Prefix + - do: + catch: /prefix queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + index: test + body: + query: + prefix: + text: + value: out + + ### Fuzzy + - do: + catch: /fuzzy queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + index: test + body: + query: + fuzzy: + text: + value: outwide + + ### Regexp + - do: + catch: /regexp queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + index: test + body: + query: + regexp: + text: + value: .*ou.*id.* + + ### Wildcard + - do: + catch: /wildcard queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + index: test + body: + query: + wildcard: + text: + value: out?ide + + ### Revert setting to false + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: "false" + flat_settings: true + + - match: {transient: {search.disallow_slow_queries: "false"}} + + ### Prefix + - do: + search: + index: test + body: + query: + prefix: + text: + value: out + + - match: { hits.total.value: 3 } + + ### Fuzzy + - do: + search: + index: test + body: + query: + fuzzy: + text: + value: outwide + + - match: { hits.total.value: 3 } + + ### Regexp + - do: + search: + index: test + body: + query: + regexp: + text: + value: .*ou.*id.* + + - match: { hits.total.value: 3 } + + ### Wildcard + - do: + search: + index: test + body: + query: + wildcard: + text: + value: out?ide + + - match: { hits.total.value: 3 } diff --git a/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java b/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java index fcf0c0fe4e61b..88eb4538cb983 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java @@ -388,6 +388,7 @@ public void apply(Settings value, Settings current, Settings previous) { SearchService.DEFAULT_KEEPALIVE_SETTING, SearchService.KEEPALIVE_INTERVAL_SETTING, SearchService.MAX_KEEPALIVE_SETTING, + SearchService.DISALLOW_SLOW_QUERIES, MultiBucketConsumerService.MAX_BUCKET_SETTING, SearchService.LOW_LEVEL_CANCELLATION_SETTING, SearchService.MAX_OPEN_SCROLL_CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/index/IndexModule.java b/server/src/main/java/org/elasticsearch/index/IndexModule.java index f1ce91b212b43..6e10316e0ff96 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexModule.java +++ b/server/src/main/java/org/elasticsearch/index/IndexModule.java @@ -130,6 +130,7 @@ public final class IndexModule { private final List searchOperationListeners = new ArrayList<>(); private final List indexOperationListeners = new ArrayList<>(); private final AtomicBoolean frozen = new AtomicBoolean(false); + private final BooleanSupplier isDisallowSlowQueries; /** * Construct the index module for the index with the specified index settings. The index module contains extension points for plugins @@ -144,13 +145,24 @@ public IndexModule( final IndexSettings indexSettings, final AnalysisRegistry analysisRegistry, final EngineFactory engineFactory, - final Map directoryFactories) { + final Map directoryFactories, + final BooleanSupplier isDisallowSlowQueries) { this.indexSettings = indexSettings; this.analysisRegistry = analysisRegistry; this.engineFactory = Objects.requireNonNull(engineFactory); this.searchOperationListeners.add(new SearchSlowLog(indexSettings)); this.indexOperationListeners.add(new IndexingSlowLog(indexSettings)); this.directoryFactories = Collections.unmodifiableMap(directoryFactories); + this.isDisallowSlowQueries = isDisallowSlowQueries; + } + + // For testing + IndexModule( + final IndexSettings indexSettings, + final AnalysisRegistry analysisRegistry, + final EngineFactory engineFactory, + final Map directoryFactories) { + this(indexSettings, analysisRegistry, engineFactory, directoryFactories, () -> false); } /** @@ -424,7 +436,7 @@ public IndexService newIndexService( new SimilarityService(indexSettings, scriptService, similarities), shardStoreDeleter, indexAnalyzers, engineFactory, circuitBreakerService, bigArrays, threadPool, scriptService, clusterService, client, queryCache, directoryFactory, eventListener, readerWrapperFactory, mapperRegistry, indicesFieldDataCache, searchOperationListeners, - indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled); + indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled, isDisallowSlowQueries); success = true; return indexService; } finally { diff --git a/server/src/main/java/org/elasticsearch/index/IndexService.java b/server/src/main/java/org/elasticsearch/index/IndexService.java index d7cf228e526a7..40db224998f34 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexService.java +++ b/server/src/main/java/org/elasticsearch/index/IndexService.java @@ -59,8 +59,8 @@ import org.elasticsearch.index.fielddata.IndexFieldDataCache; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.index.query.SearchIndexNameMatcher; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.index.query.SearchIndexNameMatcher; import org.elasticsearch.index.seqno.RetentionLeaseSyncer; import org.elasticsearch.index.shard.IndexEventListener; import org.elasticsearch.index.shard.IndexShard; @@ -126,6 +126,7 @@ public class IndexService extends AbstractIndexComponent implements IndicesClust private final IndexSettings indexSettings; private final List searchOperationListeners; private final List indexingOperationListeners; + private final BooleanSupplier isDisallowSlowQueries; private volatile AsyncRefreshTask refreshTask; private volatile AsyncTranslogFSync fsyncTask; private volatile AsyncGlobalCheckpointTask globalCheckpointTask; @@ -166,8 +167,10 @@ public IndexService( List searchOperationListeners, List indexingOperationListeners, NamedWriteableRegistry namedWriteableRegistry, - BooleanSupplier idFieldDataEnabled) { + BooleanSupplier idFieldDataEnabled, + BooleanSupplier isDisallowSlowQueries) { super(indexSettings); + this.isDisallowSlowQueries = isDisallowSlowQueries; this.indexSettings = indexSettings; this.xContentRegistry = xContentRegistry; this.similarityService = similarityService; @@ -223,6 +226,8 @@ public IndexService( this.globalCheckpointTask = new AsyncGlobalCheckpointTask(this); this.retentionLeaseSyncTask = new AsyncRetentionLeaseSyncTask(this); updateFsyncTaskIfNecessary(); + + } static boolean needsMapperService(IndexSettings indexSettings, IndexCreationContext indexCreationContext) { @@ -568,7 +573,7 @@ public QueryShardContext newQueryShardContext(int shardId, IndexSearcher searche return new QueryShardContext( shardId, indexSettings, bigArrays, indexCache.bitsetFilterCache(), indexFieldData::getForField, mapperService(), similarityService(), scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, clusterAlias, - indexNameMatcher); + indexNameMatcher, isDisallowSlowQueries); } /** diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java index 86dad273e71b0..e2de09efcb588 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java @@ -26,6 +26,7 @@ import org.apache.lucene.index.PrefixCodedTerms; import org.apache.lucene.index.PrefixCodedTerms.TermIterator; import org.apache.lucene.index.Term; +import org.apache.lucene.queries.intervals.IntervalsSource; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BoostQuery; @@ -34,7 +35,6 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; -import org.apache.lucene.queries.intervals.IntervalsSource; import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper; import org.apache.lucene.search.spans.SpanQuery; import org.apache.lucene.util.BytesRef; @@ -350,7 +350,8 @@ public Query rangeQuery( throw new IllegalArgumentException("Field [" + name + "] of type [" + typeName() + "] does not support range queries"); } - public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions) { + public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions, + QueryShardContext context) { throw new IllegalArgumentException("Can only use fuzzy queries on keyword and text fields - not on [" + name + "] which is of type [" + typeName() + "]"); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index cde8e392dabb8..e06fe9556df00 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -31,6 +31,7 @@ import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.query.QueryShardContext; @@ -38,6 +39,8 @@ import java.util.List; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; + /** Base class for {@link MappedFieldType} implementations that use the same * representation for internal index terms as the external representation so * that partial matching queries such as prefix, wildcard and fuzzy queries @@ -62,7 +65,11 @@ public Query termsQuery(List values, QueryShardContext context) { @Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, - boolean transpositions) { + boolean transpositions, QueryShardContext context) { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("fuzzy queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } failIfNotIndexed(); return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)), fuzziness.asDistance(BytesRefs.toString(value)), prefixLength, maxExpansions, transpositions); @@ -70,6 +77,10 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int @Override public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("prefix queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); if (method != null) { @@ -84,6 +95,11 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu if (termQuery instanceof MatchNoDocsQuery || termQuery instanceof MatchAllDocsQuery) { return termQuery; } + + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("wildcard queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } Term term = MappedFieldType.extractTerm(termQuery); WildcardQuery query = new WildcardQuery(term); @@ -94,6 +110,10 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu @Override public Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("regexp queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } failIfNotIndexed(); RegexpQuery query = new RegexpQuery(new Term(name(), indexedValueForSearch(value)), flags, maxDeterminizedStates); if (method != null) { diff --git a/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java index 954107c656086..8df0fec044124 100644 --- a/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java @@ -328,7 +328,7 @@ protected Query doToQuery(QueryShardContext context) throws IOException { String rewrite = this.rewrite; MappedFieldType fieldType = context.fieldMapper(fieldName); if (fieldType != null) { - query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions); + query = fieldType.fuzzyQuery(value, fuzziness, prefixLength, maxExpansions, transpositions, context); } if (query == null) { int maxEdits = fuzziness.asDistance(BytesRefs.toString(value)); diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 12b2da120f63e..50b8aee218b2a 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -66,6 +66,7 @@ import java.util.Set; import java.util.function.BiConsumer; import java.util.function.BiFunction; +import java.util.function.BooleanSupplier; import java.util.function.LongSupplier; import java.util.function.Predicate; @@ -92,12 +93,35 @@ public class QueryShardContext extends QueryRewriteContext { private final Index fullyQualifiedIndex; private final Predicate indexNameMatcher; + private final BooleanSupplier isDisallowSlowQueries; private final Map namedQueries = new HashMap<>(); private boolean allowUnmappedFields; private boolean mapUnmappedFieldAsString; private NestedScope nestedScope; + public QueryShardContext(int shardId, + IndexSettings indexSettings, + BigArrays bigArrays, + BitsetFilterCache bitsetFilterCache, + BiFunction> indexFieldDataLookup, + MapperService mapperService, + SimilarityService similarityService, + ScriptService scriptService, + NamedXContentRegistry xContentRegistry, + NamedWriteableRegistry namedWriteableRegistry, + Client client, + IndexSearcher searcher, + LongSupplier nowInMillis, + String clusterAlias, + Predicate indexNameMatcher, + BooleanSupplier isDisallowSlowQueries) { + this(shardId, indexSettings, bigArrays, bitsetFilterCache, indexFieldDataLookup, mapperService, similarityService, + scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, indexNameMatcher, + new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), + indexSettings.getIndex().getUUID()), isDisallowSlowQueries); + } + public QueryShardContext(int shardId, IndexSettings indexSettings, BigArrays bigArrays, @@ -116,14 +140,14 @@ public QueryShardContext(int shardId, this(shardId, indexSettings, bigArrays, bitsetFilterCache, indexFieldDataLookup, mapperService, similarityService, scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, indexNameMatcher, new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), - indexSettings.getIndex().getUUID())); + indexSettings.getIndex().getUUID()), () -> false); } public QueryShardContext(QueryShardContext source) { this(source.shardId, source.indexSettings, source.bigArrays, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.getXContentRegistry(), source.getWriteableRegistry(), source.client, source.searcher, source.nowInMillis, source.indexNameMatcher, - source.fullyQualifiedIndex); + source.fullyQualifiedIndex, source.isDisallowSlowQueries); } private QueryShardContext(int shardId, @@ -140,7 +164,8 @@ private QueryShardContext(int shardId, IndexSearcher searcher, LongSupplier nowInMillis, Predicate indexNameMatcher, - Index fullyQualifiedIndex) { + Index fullyQualifiedIndex, + BooleanSupplier isDisallowSlowQueries) { super(xContentRegistry, namedWriteableRegistry, client, nowInMillis); this.shardId = shardId; this.similarityService = similarityService; @@ -155,6 +180,7 @@ private QueryShardContext(int shardId, this.searcher = searcher; this.indexNameMatcher = indexNameMatcher; this.fullyQualifiedIndex = fullyQualifiedIndex; + this.isDisallowSlowQueries = isDisallowSlowQueries; } private void reset() { @@ -192,6 +218,10 @@ public BitSetProducer bitsetFilter(Query filter) { return bitsetFilterCache.getBitSetProducer(filter); } + public boolean isDisallowSlowQueries() { + return isDisallowSlowQueries.getAsBoolean(); + } + @SuppressWarnings("unchecked") public > IFD getForField(MappedFieldType fieldType) { return (IFD) indexFieldDataService.apply(fieldType, fullyQualifiedIndex.getName()); diff --git a/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java b/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java index d42466b5ebe66..abec51d0039f9 100644 --- a/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java +++ b/server/src/main/java/org/elasticsearch/index/search/MatchQuery.java @@ -525,7 +525,8 @@ protected Query newTermQuery(Term term) { Supplier querySupplier; if (fuzziness != null) { querySupplier = () -> { - Query query = fieldType.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions, transpositions); + Query query = fieldType.fuzzyQuery(term.text(), fuzziness, fuzzyPrefixLength, maxExpansions, + transpositions, context); if (query instanceof FuzzyQuery) { QueryParsers.setRewriteMethod((FuzzyQuery) query, fuzzyRewriteMethod); } diff --git a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java index 22be2131e3347..0fd2681b0e114 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryStringQueryParser.java @@ -463,7 +463,7 @@ private Query getFuzzyQuerySingle(String field, String termStr, int minSimilarit Analyzer normalizer = forceAnalyzer == null ? queryBuilder.context.getSearchAnalyzer(currentFieldType) : forceAnalyzer; BytesRef term = termStr == null ? null : normalizer.normalize(field, termStr); return currentFieldType.fuzzyQuery(term, Fuzziness.fromEdits(minSimilarity), - getFuzzyPrefixLength(), fuzzyMaxExpansions, fuzzyTranspositions); + getFuzzyPrefixLength(), fuzzyMaxExpansions, fuzzyTranspositions, context); } catch (RuntimeException e) { if (lenient) { return newLenientFieldQuery(field, e); diff --git a/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java b/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java index 912e03ca7996c..b8509ca2c112c 100644 --- a/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java +++ b/server/src/main/java/org/elasticsearch/index/search/SimpleQueryStringQueryParser.java @@ -42,10 +42,10 @@ import org.elasticsearch.index.query.SimpleQueryStringBuilder; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.List; -import java.util.ArrayList; import static org.elasticsearch.common.lucene.search.Queries.newUnmappedFieldQuery; @@ -134,7 +134,7 @@ public Query newFuzzyQuery(String text, int fuzziness) { try { final BytesRef term = getAnalyzer(ft).normalize(fieldName, text); Query query = ft.fuzzyQuery(term, Fuzziness.fromEdits(fuzziness), settings.fuzzyPrefixLength, - settings.fuzzyMaxExpansions, settings.fuzzyTranspositions); + settings.fuzzyMaxExpansions, settings.fuzzyTranspositions, context); disjuncts.add(wrapWithBoost(query, entry.getValue())); } catch (RuntimeException e) { disjuncts.add(rethrowUnlessLenient(e)); diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesService.java b/server/src/main/java/org/elasticsearch/indices/IndicesService.java index 62be919485669..26744f08a4012 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -168,6 +168,7 @@ import static org.elasticsearch.index.IndexService.IndexCreationContext.CREATE_INDEX; import static org.elasticsearch.index.IndexService.IndexCreationContext.META_DATA_VERIFICATION; import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; public class IndicesService extends AbstractLifecycleComponent implements IndicesClusterStateService.AllocatedIndices, IndexService.ShardStoreDeleter { @@ -220,6 +221,7 @@ public class IndicesService extends AbstractLifecycleComponent final AbstractRefCounted indicesRefCount; // pkg-private for testing private final CountDownLatch closeLatch = new CountDownLatch(1); private volatile boolean idFieldDataEnabled; + private volatile boolean disallowSlowQueries; @Nullable private final EsThreadPoolExecutor danglingIndicesThreadPoolExecutor; @@ -316,6 +318,9 @@ protected void closeInternal() { 0, TimeUnit.MILLISECONDS, daemonThreadFactory(nodeName, DANGLING_INDICES_UPDATE_THREAD_NAME), threadPool.getThreadContext()) : null; + + this.disallowSlowQueries = DISALLOW_SLOW_QUERIES.get(clusterService.getSettings()); + clusterService.getClusterSettings().addSettingsUpdateConsumer(DISALLOW_SLOW_QUERIES, this::setDisallowSlowQueries); } private static final String DANGLING_INDICES_UPDATE_THREAD_NAME = "DanglingIndices#updateTask"; @@ -586,7 +591,8 @@ private synchronized IndexService createIndexService(IndexService.IndexCreationC idxSettings.getNumberOfReplicas(), indexCreationContext); - final IndexModule indexModule = new IndexModule(idxSettings, analysisRegistry, getEngineFactory(idxSettings), directoryFactories); + final IndexModule indexModule = new IndexModule(idxSettings, analysisRegistry, getEngineFactory(idxSettings), + directoryFactories, () -> disallowSlowQueries); for (IndexingOperationListener operationListener : indexingOperationListeners) { indexModule.addIndexOperationListener(operationListener); } @@ -655,7 +661,8 @@ private EngineFactory getEngineFactory(final IndexSettings idxSettings) { */ public synchronized MapperService createIndexMapperService(IndexMetaData indexMetaData) throws IOException { final IndexSettings idxSettings = new IndexSettings(indexMetaData, this.settings, indexScopedSettings); - final IndexModule indexModule = new IndexModule(idxSettings, analysisRegistry, getEngineFactory(idxSettings), directoryFactories); + final IndexModule indexModule = new IndexModule(idxSettings, analysisRegistry, getEngineFactory(idxSettings), + directoryFactories, () -> disallowSlowQueries); pluginsService.onIndexModule(indexModule); return indexModule.newIndexMapperService(xContentRegistry, mapperRegistry, scriptService); } @@ -1572,6 +1579,10 @@ protected void doRun() { } } + private void setDisallowSlowQueries(Boolean disallowSlowQueries) { + this.disallowSlowQueries = disallowSlowQueries; + } + // visible for testing public boolean allPendingDanglingIndicesWritten() { return nodeWriteDanglingIndicesInfo == false || diff --git a/server/src/main/java/org/elasticsearch/search/SearchService.java b/server/src/main/java/org/elasticsearch/search/SearchService.java index 84c4903dfd838..c223feae90531 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchService.java +++ b/server/src/main/java/org/elasticsearch/search/SearchService.java @@ -134,6 +134,8 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv Setting.positiveTimeSetting("search.max_keep_alive", timeValueHours(24), Property.NodeScope, Property.Dynamic); public static final Setting KEEPALIVE_INTERVAL_SETTING = Setting.positiveTimeSetting("search.keep_alive_interval", timeValueMinutes(1), Property.NodeScope); + public static final Setting DISALLOW_SLOW_QUERIES = + Setting.boolSetting("search.disallow_slow_queries", false, Property.NodeScope, Property.Dynamic); /** * Enables low-level, frequent search cancellation checks. Enabling low-level checks will make long running searches to react diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index e0cd3b1d153fd..fe18f75b0dc18 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -26,6 +26,7 @@ import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; public class IgnoredFieldTypeTests extends FieldTypeTestCase { @@ -40,7 +41,12 @@ public void testPrefixQuery() { ft.setIndexOptions(IndexOptions.DOCS); Query expected = new PrefixQuery(new Term("field", new BytesRef("foo*"))); - assertEquals(expected, ft.prefixQuery("foo*", null, null)); + assertEquals(expected, ft.prefixQuery("foo*", null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testRegexpQuery() { @@ -49,7 +55,12 @@ public void testRegexpQuery() { ft.setIndexOptions(IndexOptions.DOCS); Query expected = new RegexpQuery(new Term("field", new BytesRef("foo?"))); - assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, null)); + assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testWildcardQuery() { @@ -58,6 +69,11 @@ public void testWildcardQuery() { ft.setIndexOptions(IndexOptions.DOCS); Query expected = new WildcardQuery(new Term("field", new BytesRef("foo*"))); - assertEquals(expected, ft.wildcardQuery("foo*", null, null)); + assertEquals(expected, ft.wildcardQuery("foo*", null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("wildcard queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java index eae5b4ac7d2ab..5dc517be175aa 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java @@ -34,6 +34,7 @@ import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.analysis.AnalyzerScope; @@ -155,12 +156,17 @@ public void testRegexpQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); assertEquals(new RegexpQuery(new Term("field","foo.*")), - ft.regexpQuery("foo.*", 0, 10, null, null)); + ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.regexpQuery("foo.*", 0, 10, null, null)); + () -> ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testFuzzyQuery() { @@ -168,12 +174,18 @@ public void testFuzzyQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); assertEquals(new FuzzyQuery(new Term("field","foo"), 2, 1, 50, true), - ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true)); + ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true)); + () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, + randomBoolean(), MOCK_QSC_DISALLOW_SLOW)); + assertEquals("fuzzy queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testNormalizeQueries() { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index 6f68d28c0176a..e0144abf6bf91 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -25,8 +25,10 @@ import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; public class RoutingFieldTypeTests extends FieldTypeTestCase { + @Override protected MappedFieldType createDefaultFieldType() { return new RoutingFieldMapper.RoutingFieldType(); @@ -38,7 +40,12 @@ public void testPrefixQuery() { ft.setIndexOptions(IndexOptions.DOCS); Query expected = new PrefixQuery(new Term("field", new BytesRef("foo*"))); - assertEquals(expected, ft.prefixQuery("foo*", null, null)); + assertEquals(expected, ft.prefixQuery("foo*", null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testRegexpQuery() { @@ -47,7 +54,12 @@ public void testRegexpQuery() { ft.setIndexOptions(IndexOptions.DOCS); Query expected = new RegexpQuery(new Term("field", new BytesRef("foo?"))); - assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, null)); + assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testWildcardQuery() { @@ -56,6 +68,11 @@ public void testWildcardQuery() { ft.setIndexOptions(IndexOptions.DOCS); Query expected = new WildcardQuery(new Term("field", new BytesRef("foo*"))); - assertEquals(expected, ft.wildcardQuery("foo*", null, null)); + assertEquals(expected, ft.wildcardQuery("foo*", null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("wildcard queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index da58907355202..670ebcac60be1 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -34,6 +34,7 @@ import org.apache.lucene.util.automaton.Automata; import org.apache.lucene.util.automaton.Automaton; import org.apache.lucene.util.automaton.Operations; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.unit.Fuzziness; import org.junit.Before; @@ -45,6 +46,7 @@ import static org.hamcrest.Matchers.equalTo; public class TextFieldTypeTests extends FieldTypeTestCase { + @Override protected MappedFieldType createDefaultFieldType() { return new TextFieldMapper.TextFieldType(); @@ -135,12 +137,17 @@ public void testRegexpQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); assertEquals(new RegexpQuery(new Term("field","foo.*")), - ft.regexpQuery("foo.*", 0, 10, null, null)); + ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.regexpQuery("foo.*", 0, 10, null, null)); + () -> ft.regexpQuery("foo.*", 0, 10, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testFuzzyQuery() { @@ -148,12 +155,18 @@ public void testFuzzyQuery() { ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); assertEquals(new FuzzyQuery(new Term("field","foo"), 2, 1, 50, true), - ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true)); + ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true)); + () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, + randomBoolean(), MOCK_QSC_DISALLOW_SLOW)); + assertEquals("fuzzy queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testIndexPrefixes() { @@ -161,13 +174,18 @@ public void testIndexPrefixes() { ft.setName("field"); ft.setPrefixFieldType(new TextFieldMapper.PrefixFieldType("field", "field._index_prefix", 2, 10)); - Query q = ft.prefixQuery("goin", CONSTANT_SCORE_REWRITE, null); + Query q = ft.prefixQuery("goin", CONSTANT_SCORE_REWRITE, randomMockShardContext()); assertEquals(new ConstantScoreQuery(new TermQuery(new Term("field._index_prefix", "goin"))), q); - q = ft.prefixQuery("internationalisatio", CONSTANT_SCORE_REWRITE, null); + q = ft.prefixQuery("internationalisatio", CONSTANT_SCORE_REWRITE, MOCK_QSC); assertEquals(new PrefixQuery(new Term("field", "internationalisatio")), q); - q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, null); + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); + + q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, randomMockShardContext()); Automaton automaton = Operations.concatenate(Arrays.asList(Automata.makeChar('g'), Automata.makeAnyChar())); diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java index 8914bad5c4102..baa13c01baa9d 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -31,9 +31,15 @@ import java.util.Arrays; import java.util.List; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** Base test case for subclasses of MappedFieldType */ public abstract class FieldTypeTestCase extends ESTestCase { + public static final QueryShardContext MOCK_QSC = createMockQueryShardContext(false); + public static final QueryShardContext MOCK_QSC_DISALLOW_SLOW = createMockQueryShardContext(true); + /** Abstraction for mutating a property of a MappedFieldType */ public abstract static class Modifier { /** The name of the property that is being modified. Used in test failure messages. */ @@ -243,6 +249,15 @@ protected String toString(MappedFieldType ft) { "} " + super.toString(); } + protected QueryShardContext randomMockShardContext() { + return randomFrom(MOCK_QSC, MOCK_QSC_DISALLOW_SLOW); + } + static QueryShardContext createMockQueryShardContext(boolean disallowSlowQueries) { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(disallowSlowQueries); + return queryShardContext; + } + public void testClone() { MappedFieldType fieldType = createNamedDefaultFieldType(); MappedFieldType clone = fieldType.clone(); diff --git a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java index 5928d9889c8c8..5c182284e0c20 100644 --- a/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java +++ b/x-pack/plugin/mapper-flattened/src/main/java/org/elasticsearch/xpack/flattened/mapper/FlatObjectFieldMapper.java @@ -306,7 +306,7 @@ public Query rangeQuery(Object lowerTerm, @Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, - boolean transpositions) { + boolean transpositions, QueryShardContext context) { throw new UnsupportedOperationException("[fuzzy] queries are not currently supported on keyed " + "[" + CONTENT_TYPE + "] fields."); } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index 46901035c8a96..fce6de477704e 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -15,6 +15,7 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.mapper.FieldTypeTestCase; import org.elasticsearch.index.mapper.MappedFieldType; @@ -98,7 +99,12 @@ public void testPrefixQuery() { ft.setName("field"); Query expected = new PrefixQuery(new Term("field", "key\0val")); - assertEquals(expected, ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, null)); + assertEquals(expected, ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testFuzzyQuery() { @@ -106,7 +112,7 @@ public void testFuzzyQuery() { ft.setName("field"); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.fuzzyQuery("valuee", Fuzziness.fromEdits(2), 1, 50, true)); + () -> ft.fuzzyQuery("value", Fuzziness.fromEdits(2), 1, 50, true, randomMockShardContext())); assertEquals("[fuzzy] queries are not currently supported on keyed [flattened] fields.", e.getMessage()); } @@ -140,7 +146,7 @@ public void testRegexpQuery() { ft.setName("field"); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.regexpQuery("valu*", 0, 10, null, null)); + () -> ft.regexpQuery("valu*", 0, 10, null, randomMockShardContext())); assertEquals("[regexp] queries are not currently supported on keyed [flattened] fields.", e.getMessage()); } @@ -149,7 +155,7 @@ public void testWildcardQuery() { ft.setName("field"); UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, - () -> ft.wildcardQuery("valu*", null, null)); + () -> ft.wildcardQuery("valu*", null, randomMockShardContext())); assertEquals("[wildcard] queries are not currently supported on keyed [flattened] fields.", e.getMessage()); } } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java index be297663c6e74..2b856ebd01dc5 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java @@ -16,6 +16,7 @@ import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.mapper.FieldNamesFieldMapper; import org.elasticsearch.index.mapper.FieldTypeTestCase; @@ -78,8 +79,14 @@ public void testFuzzyQuery() { ft.setName("field"); Query expected = new FuzzyQuery(new Term("field", "value"), 2, 1, 50, true); - Query actual = ft.fuzzyQuery("value", Fuzziness.fromEdits(2), 1, 50, true); + Query actual = ft.fuzzyQuery("value", Fuzziness.fromEdits(2), 1, 50, true, MOCK_QSC); assertEquals(expected, actual); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.fuzzyQuery("value", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, + randomBoolean(), MOCK_QSC_DISALLOW_SLOW)); + assertEquals("fuzzy queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testRangeQuery() { @@ -102,8 +109,13 @@ public void testRegexpQuery() { ft.setName("field"); Query expected = new RegexpQuery(new Term("field", "val.*")); - Query actual = ft.regexpQuery("val.*", 0, 10, null, null); + Query actual = ft.regexpQuery("val.*", 0, 10, null, MOCK_QSC); assertEquals(expected, actual); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.regexpQuery("val.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } public void testWildcardQuery() { @@ -111,6 +123,11 @@ public void testWildcardQuery() { ft.setName("field"); Query expected = new WildcardQuery(new Term("field", new BytesRef("valu*"))); - assertEquals(expected, ft.wildcardQuery("valu*", null, null)); + assertEquals(expected, ft.wildcardQuery("valu*", null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_SLOW)); + assertEquals("wildcard queries cannot be executed when 'search.disallow_slow_queries' is set to true", + ee.getMessage()); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java index 5575915188351..8141bb6ad45b9 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java @@ -74,7 +74,8 @@ public void testWatcherDisabledTests() throws Exception { IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(Watch.INDEX, settings); AnalysisRegistry registry = new AnalysisRegistry(TestEnvironment.newEnvironment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()); - IndexModule indexModule = new IndexModule(indexSettings, registry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule indexModule = new IndexModule(indexSettings, registry, new InternalEngineFactory(), Collections.emptyMap(), + () -> false); // this will trip an assertion if the watcher indexing operation listener is null (which it is) but we try to add it watcher.onIndexModule(indexModule); From 181d838fa68b788c3c09d96b5e419078ff836cd4 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Mon, 27 Jan 2020 16:27:08 +0100 Subject: [PATCH 02/25] address comments --- docs/reference/query-dsl.asciidoc | 2 +- server/src/main/java/org/elasticsearch/index/IndexService.java | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/docs/reference/query-dsl.asciidoc b/docs/reference/query-dsl.asciidoc index cbc016b8fb8f4..ad1ec2ae58303 100644 --- a/docs/reference/query-dsl.asciidoc +++ b/docs/reference/query-dsl.asciidoc @@ -32,7 +32,7 @@ Certain queries like <>, <> and <> , that are usually slow performance can affect the cluster performance. The execution of such queries can be prevented by setting the value of the `search.disallow_slow_queries` -setting to `true` (defaults tp `false`). +setting to `true` (defaults to `false`). -- include::query-dsl/query_filter_context.asciidoc[] diff --git a/server/src/main/java/org/elasticsearch/index/IndexService.java b/server/src/main/java/org/elasticsearch/index/IndexService.java index 40db224998f34..43cc71f7ecd8c 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexService.java +++ b/server/src/main/java/org/elasticsearch/index/IndexService.java @@ -226,8 +226,6 @@ public IndexService( this.globalCheckpointTask = new AsyncGlobalCheckpointTask(this); this.retentionLeaseSyncTask = new AsyncRetentionLeaseSyncTask(this); updateFsyncTaskIfNecessary(); - - } static boolean needsMapperService(IndexSettings indexSettings, IndexCreationContext indexCreationContext) { From 4ffc00f0947977ab00e8a47b97b70eb361876dfd Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Mon, 27 Jan 2020 20:22:25 +0100 Subject: [PATCH 03/25] Add script and script_score queries to the disallowed ones --- .../reference/query-dsl/script-query.asciidoc | 4 ++ .../query-dsl/script-score-query.asciidoc | 4 ++ .../index/query/ScriptQueryBuilder.java | 7 +++ .../ScriptScoreQueryBuilder.java | 6 +++ .../index/query/ScriptQueryBuilderTests.java | 18 ++++++- .../query/ScriptScoreQueryBuilderTests.java | 20 ++++++- .../search/query/ScriptScoreQueryIT.java | 52 +++++++++++++++++++ .../scriptfilter/ScriptQuerySearchIT.java | 51 ++++++++++++++++++ .../index/mapper/FieldTypeTestCase.java | 1 + 9 files changed, 160 insertions(+), 3 deletions(-) diff --git a/docs/reference/query-dsl/script-query.asciidoc b/docs/reference/query-dsl/script-query.asciidoc index 5ed6d4e91f602..509f1df9e4e69 100644 --- a/docs/reference/query-dsl/script-query.asciidoc +++ b/docs/reference/query-dsl/script-query.asciidoc @@ -69,3 +69,7 @@ GET /_search } } ---- + +===== Disallow slow queries +Script queries will not be executed if <> +is set to true. diff --git a/docs/reference/query-dsl/script-score-query.asciidoc b/docs/reference/query-dsl/script-score-query.asciidoc index 029cc7469e0f3..5bc56c83ce450 100644 --- a/docs/reference/query-dsl/script-score-query.asciidoc +++ b/docs/reference/query-dsl/script-score-query.asciidoc @@ -221,6 +221,10 @@ and default time zone. Also calculations with `now` are not supported. <> are accessible through `script_score` query. +===== Disallow slow queries +Script score queries will not be executed if <> +is set to true. + [[script-score-faster-alt]] ===== Faster alternatives The `script_score` query calculates the score for diff --git a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index e9b18bd0aa1f1..33cd22bc79b38 100644 --- a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -29,6 +29,7 @@ import org.apache.lucene.search.Scorer; import org.apache.lucene.search.TwoPhaseIterator; import org.apache.lucene.search.Weight; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -40,6 +41,8 @@ import java.io.IOException; import java.util.Objects; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; + public class ScriptQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "script"; @@ -130,6 +133,10 @@ public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOEx @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("script queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } FilterScript.Factory factory = context.compile(script, FilterScript.CONTEXT); FilterScript.LeafFactory filterScript = factory.newFactory(script.getParams(), context.lookup()); return new ScriptQuery(script, filterScript); diff --git a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java index 74e51ff09a683..f4746d92d26e5 100644 --- a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query.functionscore; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.io.stream.StreamInput; @@ -42,6 +43,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; /** * A query that computes a document score based on the provided script @@ -170,6 +172,10 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("script score queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } ScoreScript.Factory factory = context.compile(script, ScoreScript.CONTEXT); ScoreScript.LeafFactory scoreScriptFactory = factory.newFactory(script.getParams(), context.lookup()); Query query = this.query.toQuery(context); diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index fbf67860a2d85..a714dfe894a5e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParsingException; import org.elasticsearch.script.MockScriptEngine; import org.elasticsearch.script.Script; @@ -33,6 +34,8 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.instanceOf; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ScriptQueryBuilderTests extends AbstractQueryTestCase { @Override @@ -53,7 +56,9 @@ protected void doAssertLuceneQuery(ScriptQueryBuilder queryBuilder, Query query, } public void testIllegalConstructorArg() { - expectThrows(IllegalArgumentException.class, () -> new ScriptQueryBuilder((Script) null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, + () -> new ScriptQueryBuilder((Script) null)); + assertEquals("script cannot be null", e.getMessage()); } public void testFromJsonVerbose() throws IOException { @@ -126,4 +131,15 @@ public void testCacheability() throws IOException { assertNotNull(rewriteQuery.toQuery(context)); assertFalse("query should not be cacheable: " + queryBuilder.toString(), context.isCacheable()); } + + public void testDisallowSlowQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + + ScriptQueryBuilder queryBuilder = doCreateTestQueryBuilder(); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("script queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getMessage()); + } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java index 690a7d6ae757a..1e29cf3453082 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.query; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.lucene.search.function.ScriptScoreQuery; import org.elasticsearch.index.query.functionscore.ScriptScoreQueryBuilder; import org.elasticsearch.script.MockScriptEngine; @@ -32,6 +33,8 @@ import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.hamcrest.CoreMatchers.instanceOf; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ScriptScoreQueryBuilderTests extends AbstractQueryTestCase { @@ -71,15 +74,17 @@ public void testIllegalArguments() { String scriptStr = "1"; Script script = new Script(ScriptType.INLINE, MockScriptEngine.NAME, scriptStr, Collections.emptyMap()); - expectThrows( + IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> new ScriptScoreQueryBuilder(matchAllQuery(), null) ); + assertEquals("script_score: script must not be null" , e.getMessage()); - expectThrows( + e = expectThrows( IllegalArgumentException.class, () -> new ScriptScoreQueryBuilder(null, script) ); + assertEquals("script_score: query must not be null" , e.getMessage()); } /** @@ -93,4 +98,15 @@ public void testCacheability() throws IOException { assertNotNull(rewriteQuery.toQuery(context)); assertFalse("query should not be cacheable: " + queryBuilder.toString(), context.isCacheable()); } + + public void testDisallowSlowQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + + ScriptScoreQueryBuilder queryBuilder = doCreateTestQueryBuilder(); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("script score queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getMessage()); + } } diff --git a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java index 48e8e9abfbb6b..961581bb0d6cb 100644 --- a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java @@ -19,6 +19,8 @@ package org.elasticsearch.search.query; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.fielddata.ScriptDocValues; @@ -29,6 +31,7 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESIntegTestCase; +import org.junit.After; import java.util.Collection; import java.util.Collections; @@ -68,6 +71,13 @@ protected Map, Object>> pluginScripts() { } } + @After + public void resetSettings() { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } + // test that script_score works as expected: // 1) only matched docs retrieved // 2) score is calculated based on a script with params @@ -154,4 +164,46 @@ public void testRewrittenQuery() { assertNoFailures(resp); assertOrderedSearchHits(resp, "3", "2", "1"); } + + public void testDisallowSlowQueries() { + assertAcked( + prepareCreate("test-index").setMapping("field1", "type=text", "field2", "type=double") + ); + int docCount = 10; + for (int i = 1; i <= docCount; i++) { + client().prepareIndex("test-index").setId("" + i) + .setSource("field1", "text" + (i % 2), "field2", i ) + .get(); + } + refresh(); + + Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['field2'].value * param1", + Map.of("param1", 0.1)); + SearchResponse resp = client() + .prepareSearch("test-index") + .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) + .get(); + assertNoFailures(resp); + + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client() + .prepareSearch("test-index") + .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) + .get()); + assertEquals("script score queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getCause().getMessage()); + + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + resp = client() + .prepareSearch("test-index") + .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) + .get(); + assertNoFailures(resp); + } } diff --git a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java index 7456706a8b81d..98f9f774679d6 100644 --- a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java @@ -19,6 +19,8 @@ package org.elasticsearch.search.scriptfilter; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -32,6 +34,7 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalSettingsPlugin; +import org.junit.After; import java.io.IOException; import java.util.Arrays; @@ -47,11 +50,19 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.QueryBuilders.scriptQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; import static org.hamcrest.Matchers.equalTo; @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE) public class ScriptQuerySearchIT extends ESIntegTestCase { + @After + public void resetSettings() { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } + @Override protected Collection> nodePlugins() { return Arrays.asList(CustomScriptPlugin.class, InternalSettingsPlugin.class); @@ -222,6 +233,46 @@ public void testCustomScriptBoost() throws Exception { assertThat(response.getHits().getAt(2).getFields().get("sNum1").getValues().get(0), equalTo(3.0)); } + public void testDisallowSlowQueries() { + assertAcked( + prepareCreate("test-index").setMapping("num1", "type=double") + ); + int docCount = 10; + for (int i = 1; i <= docCount; i++) { + client().prepareIndex("test-index").setId("" + i) + .setSource("num1", i ) + .get(); + } + refresh(); + + Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['num1'].value > 1", + Collections.emptyMap()); + SearchResponse resp = client().prepareSearch("test-index") + .setQuery(scriptQuery(script)) + .get(); + assertNoFailures(resp); + + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client() + .prepareSearch("test-index") + .setQuery(scriptQuery(script)) + .get()); + assertEquals("script queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getCause().getMessage()); + + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + resp = client().prepareSearch("test-index") + .setQuery(scriptQuery(script)) + .get(); + assertNoFailures(resp); + } + private static AtomicInteger scriptCounter = new AtomicInteger(0); public static int incrementScriptCounter() { diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java index baa13c01baa9d..405a5b8817f1a 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -252,6 +252,7 @@ protected String toString(MappedFieldType ft) { protected QueryShardContext randomMockShardContext() { return randomFrom(MOCK_QSC, MOCK_QSC_DISALLOW_SLOW); } + static QueryShardContext createMockQueryShardContext(boolean disallowSlowQueries) { QueryShardContext queryShardContext = mock(QueryShardContext.class); when(queryShardContext.isDisallowSlowQueries()).thenReturn(disallowSlowQueries); From 55200c70cb79a9b68231da3fb744b756272ddd88 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Tue, 28 Jan 2020 16:15:19 +0100 Subject: [PATCH 04/25] Disallow joining queries --- .../query-dsl/joining-queries.asciidoc | 5 +- .../join/query/HasChildQueryBuilder.java | 8 + .../join/query/HasParentQueryBuilder.java | 8 + .../join/query/ParentIdQueryBuilder.java | 8 + .../join/query/HasChildQueryBuilderTests.java | 17 +- .../query/HasParentQueryBuilderTests.java | 16 ++ .../join/query/ParentIdQueryBuilderTests.java | 13 ++ .../rest-api-spec/test/11_parent_child.yml | 33 ++++ .../rest-api-spec/test/20_parent_join.yml | 38 ++++ .../rest-api-spec/test/30_inner_hits.yml | 182 +++++++++++------- .../test/search/320_disallow_queries.yml | 52 ++++- .../index/query/NestedQueryBuilder.java | 7 + .../index/query/NestedQueryBuilderTests.java | 13 ++ 13 files changed, 328 insertions(+), 72 deletions(-) diff --git a/docs/reference/query-dsl/joining-queries.asciidoc b/docs/reference/query-dsl/joining-queries.asciidoc index 69fcca8690079..1f47c616c5eae 100644 --- a/docs/reference/query-dsl/joining-queries.asciidoc +++ b/docs/reference/query-dsl/joining-queries.asciidoc @@ -29,4 +29,7 @@ include::has-parent-query.asciidoc[] include::parent-id-query.asciidoc[] - +=== Notes +==== Disallow slow queries +Joining queries will not be executed if <> +is set to true. diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index 14362afd2e9c6..d58cfa758c723 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -27,6 +27,7 @@ import org.apache.lucene.search.join.JoinUtil; import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.similarities.Similarity; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; @@ -53,6 +54,8 @@ import java.util.Map; import java.util.Objects; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; + /** * A query builder for {@code has_child} query. */ @@ -295,6 +298,11 @@ public String getWriteableName() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } + ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); if (joinFieldMapper == null) { if (ignoreUnmapped) { diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java index 30a2718aab054..7067185d25216 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java @@ -21,6 +21,7 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; @@ -45,6 +46,8 @@ import java.util.Map; import java.util.Objects; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; + /** * Builder for the 'has_parent' query. */ @@ -158,6 +161,11 @@ public boolean ignoreUnmapped() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } + ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); if (joinFieldMapper == null) { if (ignoreUnmapped) { diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java index bc8820c597790..a30c10b1d282f 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java @@ -23,6 +23,7 @@ import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.io.stream.StreamInput; @@ -38,6 +39,8 @@ import java.io.IOException; import java.util.Objects; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; + public final class ParentIdQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "parent_id"; @@ -153,6 +156,11 @@ public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IO @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } + ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); if (joinFieldMapper == null) { if (ignoreUnmapped) { diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index f40b14d5d95ad..ebdacdab913ef 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.join.query; import com.carrotsearch.randomizedtesting.generators.RandomPicks; - import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; @@ -32,6 +31,7 @@ import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.similarities.PerFieldSimilarityWrapper; import org.apache.lucene.search.similarities.Similarity; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Strings; @@ -70,6 +70,8 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class HasChildQueryBuilderTests extends AbstractQueryTestCase { @@ -361,5 +363,18 @@ public void testExtractInnerHitBuildersWithDuplicate() { queryBuilder.innerHit(new InnerHitBuilder("some_name")); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null))); + assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); + } + + public void testDisallowSlowQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + + HasChildQueryBuilder queryBuilder = + hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 1af3a056d2fb8..77e7f78b9e1d0 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -22,6 +22,7 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Strings; @@ -57,6 +58,8 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class HasParentQueryBuilderTests extends AbstractQueryTestCase { private static final String TYPE = "_doc"; @@ -261,5 +264,18 @@ public void testExtractInnerHitBuildersWithDuplicate() { queryBuilder.innerHit(new InnerHitBuilder("some_name")); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> InnerHitContextBuilder.extractInnerHits(queryBuilder, Collections.singletonMap("some_name", null))); + assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); + } + + public void testDisallowSlowQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + + HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder( + CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index f43214515be0d..51012e6b1aefa 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -25,6 +25,7 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.common.Strings; @@ -48,6 +49,8 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class ParentIdQueryBuilderTests extends AbstractQueryTestCase { @@ -154,4 +157,14 @@ public void testIgnoreUnmapped() throws IOException { assertThat(e.getMessage(), containsString("[" + ParentIdQueryBuilder.NAME + "] no relation found for child [unmapped]")); } + public void testDisallowSlowQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + + ParentIdQueryBuilder queryBuilder = doCreateTestQueryBuilder(); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getMessage()); + } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml index d85f89e768db8..2ecc9ab4c803f 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml @@ -26,6 +26,18 @@ setup: - do: indices.refresh: {} +--- +teardown: + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: null + --- "Parent/child inner hits": - do: @@ -53,3 +65,24 @@ setup: - is_false: hits.hits.0.inner_hits.child.hits.hits.0._nested - gte: { hits.hits.0.inner_hits.child.hits.hits.0._seq_no: 0 } - gte: { hits.hits.0.inner_hits.child.hits.hits.0._primary_term: 1 } + +--- +"HasChild disallow slow queries": + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + ### Update setting to true + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: "true" + flat_settings: true + + - match: {transient: {search.disallow_slow_queries: "true"}} + + - do: + catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + body: { "query": { "has_child": { "type": "child", "query": { "match_all": {} }, "inner_hits": {} } } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml index 0b65c744ec0b2..c46116d998c59 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml @@ -51,6 +51,18 @@ setup: - do: indices.refresh: {} +--- +teardown: + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: null + --- "Test basic": - do: @@ -108,3 +120,29 @@ setup: - match: { hits.hits.1._id: "4" } - match: { hits.hits.1._source.join_field.name: "child" } - match: { hits.hits.1._source.join_field.parent: "1" } + +--- +"HasChild disallow slow queries": + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + ### Update setting to true + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: "true" + flat_settings: true + + - match: {transient: {search.disallow_slow_queries: "true"}} + + - do: + catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + body: + sort: [ "id" ] + query: + parent_id: + type: child + id: 1 diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml index 44b1897485f16..2791a20970db3 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml @@ -1,68 +1,118 @@ +--- +setup: + - skip: + version: " - 7.99.99" + reason: "The bug was corrected from 8.0" + + - do: + indices.create: + index: test + body: + mappings: + properties: + entity_type: { "type": "keyword" } + join_field: { "type": "join", "relations": { "question": "answer", "person": "address" } } + settings: + number_of_shards: 1 + + - do: + index: + index: test + id: 1 + body: { "join_field": { "name": "question" }, "entity_type": "question" } + + - do: + index: + index: test + id: 2 + routing: 1 + body: { "join_field": { "name": "answer", "parent": 1} , "entity_type": "answer" } + + - do: + index: + index: test + id: 3 + body: { "join_field": { "name": "person" }, "entity_type": "person" } + + - do: + index: + index: test + routing: 3 + id: 4 + body: { "join_field": { "name": "address", "parent": 3 }, "entity_type": "address" } + + - do: + indices.refresh: {} + +--- +teardown: + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: null + --- "Test two sub-queries with only one having inner_hits": - - skip: - version: " - 7.99.99" - reason: "The bug was corrected from 8.0" - - - do: - indices.create: - index: test - body: - mappings: - properties: - entity_type: { "type": "keyword" } - join_field: { "type": "join", "relations": { "question": "answer", "person" : "address" } } - settings: - number_of_shards: 1 - - - do: - index: - index: test - id: 1 - body: { "join_field": { "name": "question" }, "entity_type": "question" } - - - do: - index: - index: test - id: 2 - routing: 1 - body: { "join_field": { "name": "answer", "parent": 1} , "entity_type": "answer" } - - - do: - index: - index: test - id: 3 - body: { "join_field": { "name": "person" }, "entity_type": "person" } - - - do: - index: - index: test - routing: 3 - id: 4 - body: { "join_field": { "name": "address", "parent": 3 }, "entity_type": "address" } - - - do: - indices.refresh: {} - - - do: - search: - index: test - body: - query: - bool: - should: - - term: - entity_type: person - - has_parent: - parent_type: question - query: - match_all: {} - inner_hits: {} - - - - match: { hits.total.value: 2 } - - match: { hits.hits.0._id: "3" } - - match: { hits.hits.0.inner_hits.question.hits.total.value: 0} - - match: { hits.hits.1._id: "2" } - - match: { hits.hits.1.inner_hits.question.hits.total.value: 1} - - match: { hits.hits.1.inner_hits.question.hits.hits.0._id: "1"} + - skip: + version: " - 7.99.99" + reason: "The bug was corrected from 8.0" + + - do: + search: + index: test + body: + query: + bool: + should: + - term: + entity_type: person + - has_parent: + parent_type: question + query: + match_all: {} + inner_hits: {} + + + - match: { hits.total.value: 2 } + - match: { hits.hits.0._id: "3" } + - match: { hits.hits.0.inner_hits.question.hits.total.value: 0} + - match: { hits.hits.1._id: "2" } + - match: { hits.hits.1.inner_hits.question.hits.total.value: 1} + - match: { hits.hits.1.inner_hits.question.hits.hits.0._id: "1"} + +--- +"HasParent disallow slow queries": + - skip: + version: " - 7.99.99" + reason: "implemented in 8.0.0" + + ### Update setting to true + - do: + cluster.put_settings: + body: + transient: + search.disallow_slow_queries: "true" + flat_settings: true + + - match: {transient: {search.disallow_slow_queries: "true"}} + + - do: + catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + index: test + body: + query: + bool: + should: + - term: + entity_type: person + - has_parent: + parent_type: question + query: + match_all: {} + inner_hits: {} diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml index 04c674bdababf..294f9bf44debb 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -13,18 +13,21 @@ setup: text: type: text analyzer: standard + nested1: + type: nested + - do: bulk: refresh: true body: - '{"index": {"_index": "test", "_id": "1"}}' - - '{"text" : "Some like it hot, some like it cold"}' + - '{"text" : "Some like it hot, some like it cold", "nested1": [{"foo": "bar1"}]}' - '{"index": {"_index": "test", "_id": "2"}}' - - '{"text" : "Its cold outside, theres no kind of atmosphere"}' + - '{"text" : "Its cold outside, theres no kind of atmosphere", "nested1": [{"foo": "bar2"}]}' - '{"index": {"_index": "test", "_id": "3"}}' - - '{"text" : "Baby its cold there outside"}' + - '{"text" : "Baby its cold there outside", "nested1": [{"foo": "bar3"}]}' - '{"index": {"_index": "test", "_id": "4"}}' - - '{"text" : "Outside it is cold and wet"}' + - '{"text" : "Outside it is cold and wet", "nested1": [{"foo": "bar4"}]}' --- teardown: @@ -100,6 +103,20 @@ teardown: - match: { hits.total.value: 3 } + ### Nested + - do: + search: + index: test + body: + query: + nested: + path: "nested1" + query: + bool: + must: [{"match": {"nested1.foo": "bar2"}}] + + - match: { hits.total.value: 1 } + ### Update setting to true - do: cluster.put_settings: @@ -154,6 +171,19 @@ teardown: text: value: out?ide + ### Nested + - do: + catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + search: + index: test + body: + query: + nested: + path: "nested1" + query: + bool: + must: [{"match" : {"nested1.foo" : "bar2"}}] + ### Revert setting to false - do: cluster.put_settings: @@ -211,3 +241,17 @@ teardown: value: out?ide - match: { hits.total.value: 3 } + + ### Nested + - do: + search: + index: test + body: + query: + nested: + path: "nested1" + query: + bool: + must: [{"match": {"nested1.foo": "bar2"}}] + + - match: { hits.total.value: 1 } diff --git a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index fecf5c8407e98..4beeab236d01b 100644 --- a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -34,6 +34,7 @@ import org.apache.lucene.search.join.BitSetProducer; import org.apache.lucene.search.join.ParentChildrenBlockJoinQuery; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.search.MaxScoreCollector; import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParsingException; @@ -57,6 +58,7 @@ import java.util.Map; import java.util.Objects; +import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; import static org.elasticsearch.search.fetch.subphase.InnerHitsContext.intersect; public class NestedQueryBuilder extends AbstractQueryBuilder { @@ -266,6 +268,11 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isDisallowSlowQueries() == true) { + throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + + "' is set to true"); + } + ObjectMapper nestedObjectMapper = context.getObjectMapper(path); if (nestedObjectMapper == null) { if (ignoreUnmapped) { diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 4f4aff6d10cdf..7e0fd7dd7fb0c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -23,6 +23,7 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.Version; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.Strings; @@ -354,5 +355,17 @@ public void testExtractInnerHitBuildersWithDuplicate() { queryBuilder.innerHit(new InnerHitBuilder("some_name")); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> InnerHitContextBuilder.extractInnerHits(queryBuilder,Collections.singletonMap("some_name", null))); + assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); + } + + public void testDisallowSlowQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + + NestedQueryBuilder queryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + e.getMessage()); } } From 16f6c8b6ab13a53d8149af53534fb4bd94ed0d01 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Tue, 28 Jan 2020 22:42:26 +0100 Subject: [PATCH 05/25] Rename setting to positive semantics --- docs/reference/query-dsl.asciidoc | 8 +++--- docs/reference/query-dsl/fuzzy-query.asciidoc | 4 +-- .../query-dsl/joining-queries.asciidoc | 6 ++-- .../reference/query-dsl/prefix-query.asciidoc | 10 +++---- .../query-dsl/query-string-query.asciidoc | 4 +-- .../reference/query-dsl/regexp-query.asciidoc | 6 ++-- .../reference/query-dsl/script-query.asciidoc | 6 ++-- .../query-dsl/script-score-query.asciidoc | 6 ++-- .../query-dsl/wildcard-query.asciidoc | 6 ++-- .../mapper/SearchAsYouTypeFieldTypeTests.java | 4 +-- .../join/query/HasChildQueryBuilder.java | 8 +++--- .../join/query/HasParentQueryBuilder.java | 8 +++--- .../join/query/ParentIdQueryBuilder.java | 8 +++--- .../join/query/HasChildQueryBuilderTests.java | 6 ++-- .../query/HasParentQueryBuilderTests.java | 6 ++-- .../join/query/ParentIdQueryBuilderTests.java | 6 ++-- .../rest-api-spec/test/11_parent_child.yml | 12 ++++---- .../rest-api-spec/test/20_parent_join.yml | 12 ++++---- .../rest-api-spec/test/30_inner_hits.yml | 12 ++++---- .../test/search/320_disallow_queries.yml | 28 +++++++++---------- .../common/settings/ClusterSettings.java | 2 +- .../org/elasticsearch/index/IndexModule.java | 10 +++---- .../org/elasticsearch/index/IndexService.java | 8 +++--- .../index/mapper/StringFieldType.java | 26 ++++++++--------- .../index/query/NestedQueryBuilder.java | 8 +++--- .../index/query/QueryShardContext.java | 18 ++++++------ .../index/query/ScriptQueryBuilder.java | 8 +++--- .../ScriptScoreQueryBuilder.java | 8 +++--- .../elasticsearch/indices/IndicesService.java | 16 +++++------ .../elasticsearch/search/SearchService.java | 4 +-- .../index/mapper/IgnoredFieldTypeTests.java | 12 ++++---- .../index/mapper/KeywordFieldTypeTests.java | 8 +++--- .../index/mapper/RoutingFieldTypeTests.java | 12 ++++---- .../index/mapper/TextFieldTypeTests.java | 12 ++++---- .../index/query/NestedQueryBuilderTests.java | 6 ++-- .../index/query/ScriptQueryBuilderTests.java | 6 ++-- .../query/ScriptScoreQueryBuilderTests.java | 6 ++-- .../search/query/ScriptScoreQueryIT.java | 10 +++---- .../scriptfilter/ScriptQuerySearchIT.java | 10 +++---- .../index/mapper/FieldTypeTestCase.java | 10 +++---- .../mapper/KeyedFlatObjectFieldTypeTests.java | 4 +-- .../mapper/RootFlatObjectFieldTypeTests.java | 12 ++++---- .../xpack/watcher/WatcherPluginTests.java | 2 +- 43 files changed, 192 insertions(+), 192 deletions(-) diff --git a/docs/reference/query-dsl.asciidoc b/docs/reference/query-dsl.asciidoc index ad1ec2ae58303..bb80bfd4d9337 100644 --- a/docs/reference/query-dsl.asciidoc +++ b/docs/reference/query-dsl.asciidoc @@ -26,13 +26,13 @@ or to alter their behaviour (such as the Query clauses behave differently depending on whether they are used in <>. -[[query-dsl-disallow-slow]] -== Disallow slow queries +[[query-dsl-allow-expensive-queries]] +== Allow expensive queries Certain queries like <>, <>, <> and <> , that are usually slow performance can affect the cluster performance. -The execution of such queries can be prevented by setting the value of the `search.disallow_slow_queries` -setting to `true` (defaults to `false`). +The execution of such queries can be prevented by setting the value of the `search.allow_expensive_queries` +setting to `false` (defaults to `true`). -- include::query-dsl/query_filter_context.asciidoc[] diff --git a/docs/reference/query-dsl/fuzzy-query.asciidoc b/docs/reference/query-dsl/fuzzy-query.asciidoc index e145b8a3c81d8..3a6d4bf786681 100644 --- a/docs/reference/query-dsl/fuzzy-query.asciidoc +++ b/docs/reference/query-dsl/fuzzy-query.asciidoc @@ -100,5 +100,5 @@ adjacent characters (ab → ba). Defaults to `true`. information, see the <>. ==== Notes -Fuzzy queries will not be executed if <> -is set to true. +Fuzzy queries will not be executed if <> +is set to false. diff --git a/docs/reference/query-dsl/joining-queries.asciidoc b/docs/reference/query-dsl/joining-queries.asciidoc index 1f47c616c5eae..140fe111fed79 100644 --- a/docs/reference/query-dsl/joining-queries.asciidoc +++ b/docs/reference/query-dsl/joining-queries.asciidoc @@ -30,6 +30,6 @@ include::has-parent-query.asciidoc[] include::parent-id-query.asciidoc[] === Notes -==== Disallow slow queries -Joining queries will not be executed if <> -is set to true. +==== Allow expensive queries +Joining queries will not be executed if <> +is set to false. diff --git a/docs/reference/query-dsl/prefix-query.asciidoc b/docs/reference/query-dsl/prefix-query.asciidoc index e6802c428c257..cd5f2e624b4a4 100644 --- a/docs/reference/query-dsl/prefix-query.asciidoc +++ b/docs/reference/query-dsl/prefix-query.asciidoc @@ -66,10 +66,10 @@ mapping parameter. If enabled, {es} indexes prefixes between 2 and 5 characters in a separate field. This lets {es} run prefix queries more efficiently at the cost of a larger index. -[[prefix-query-disallow-slow]] -===== Disallow slow queries -Prefix queries will not be executed if <> -is set to true. If <> are enabled though, an optimised query is +[[prefix-query-allow-expensive-queries]] +===== Allow expensive queries +Prefix queries will not be executed if <> +is set to false. If <> are enabled though, an optimised query is built, which is not considered slow and is executed despite of the fact that the -<> set to true. +<> set to false. diff --git a/docs/reference/query-dsl/query-string-query.asciidoc b/docs/reference/query-dsl/query-string-query.asciidoc index f1b07c680f0e4..47523a079341d 100644 --- a/docs/reference/query-dsl/query-string-query.asciidoc +++ b/docs/reference/query-dsl/query-string-query.asciidoc @@ -539,7 +539,7 @@ The example above creates a boolean query: that matches documents with at least two of the three per-term blended queries. ==== Notes -===== Disallow slow queries +===== Allow expensive queries Query string query can be internally be transformed to a <> which means -that if the prefix queries are disabled as explained <> the query will not be +that if the prefix queries are disabled as explained <> the query will not be executed and an exception will be thrown. diff --git a/docs/reference/query-dsl/regexp-query.asciidoc b/docs/reference/query-dsl/regexp-query.asciidoc index 329259d008ba5..522bc68adf8d7 100644 --- a/docs/reference/query-dsl/regexp-query.asciidoc +++ b/docs/reference/query-dsl/regexp-query.asciidoc @@ -88,6 +88,6 @@ regular expressions. information, see the <>. ==== Notes -===== Disallow slow queries -Regexp queries will not be executed if <> -is set to true. +===== Allow expensive queries +Regexp queries will not be executed if <> +is set to false. diff --git a/docs/reference/query-dsl/script-query.asciidoc b/docs/reference/query-dsl/script-query.asciidoc index 509f1df9e4e69..cadf3c080df65 100644 --- a/docs/reference/query-dsl/script-query.asciidoc +++ b/docs/reference/query-dsl/script-query.asciidoc @@ -70,6 +70,6 @@ GET /_search } ---- -===== Disallow slow queries -Script queries will not be executed if <> -is set to true. +===== Allow expensive queries +Script queries will not be executed if <> +is set to false. diff --git a/docs/reference/query-dsl/script-score-query.asciidoc b/docs/reference/query-dsl/script-score-query.asciidoc index 5bc56c83ce450..d38021119d9c3 100644 --- a/docs/reference/query-dsl/script-score-query.asciidoc +++ b/docs/reference/query-dsl/script-score-query.asciidoc @@ -221,9 +221,9 @@ and default time zone. Also calculations with `now` are not supported. <> are accessible through `script_score` query. -===== Disallow slow queries -Script score queries will not be executed if <> -is set to true. +===== Allow expensive queries +Script score queries will not be executed if <> +is set to false. [[script-score-faster-alt]] ===== Faster alternatives diff --git a/docs/reference/query-dsl/wildcard-query.asciidoc b/docs/reference/query-dsl/wildcard-query.asciidoc index 604f19692e4c7..3df6570897d6c 100644 --- a/docs/reference/query-dsl/wildcard-query.asciidoc +++ b/docs/reference/query-dsl/wildcard-query.asciidoc @@ -70,6 +70,6 @@ increases the relevance score. <>. ==== Notes -===== Disallow slow queries -Wildcard queries will not be executed if <> -is set to true. +===== Allow expensive queries +Wildcard queries will not be executed if <> +is set to false. diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index 609b3144fa826..144221d6013dd 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -112,8 +112,8 @@ public void testPrefixQuery() { equalTo(new PrefixQuery(new Term(NAME, longTerm)))); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index d58cfa758c723..0efd25ca1a657 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -54,7 +54,7 @@ import java.util.Map; import java.util.Objects; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; /** * A query builder for {@code has_child} query. @@ -298,9 +298,9 @@ public String getWriteableName() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java index 7067185d25216..d05fb8ea18021 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java @@ -46,7 +46,7 @@ import java.util.Map; import java.util.Objects; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; /** * Builder for the 'has_parent' query. @@ -161,9 +161,9 @@ public boolean ignoreUnmapped() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java index a30c10b1d282f..a13c3912d1796 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java @@ -39,7 +39,7 @@ import java.io.IOException; import java.util.Objects; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; public final class ParentIdQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "parent_id"; @@ -156,9 +156,9 @@ public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IO @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index ebdacdab913ef..647bb2c061291 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -366,15 +366,15 @@ public void testExtractInnerHitBuildersWithDuplicate() { assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); HasChildQueryBuilder queryBuilder = hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 77e7f78b9e1d0..5f8c791307f97 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -267,15 +267,15 @@ public void testExtractInnerHitBuildersWithDuplicate() { assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder( CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index 51012e6b1aefa..f81038845b9ff 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -157,14 +157,14 @@ public void testIgnoreUnmapped() throws IOException { assertThat(e.getMessage(), containsString("[" + ParentIdQueryBuilder.NAME + "] no relation found for child [unmapped]")); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); ParentIdQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml index 2ecc9ab4c803f..6839082cedc5a 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml @@ -36,7 +36,7 @@ teardown: cluster.put_settings: body: transient: - search.disallow_slow_queries: null + search.allow_expensive_queries: null --- "Parent/child inner hits": @@ -67,22 +67,22 @@ teardown: - gte: { hits.hits.0.inner_hits.child.hits.hits.0._primary_term: 1 } --- -"HasChild disallow slow queries": +"HasChild disallow expensive queries": - skip: version: " - 7.99.99" reason: "implemented in 8.0.0" - ### Update setting to true + ### Update setting to false - do: cluster.put_settings: body: transient: - search.disallow_slow_queries: "true" + search.allow_expensive_queries: "false" flat_settings: true - - match: {transient: {search.disallow_slow_queries: "true"}} + - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: body: { "query": { "has_child": { "type": "child", "query": { "match_all": {} }, "inner_hits": {} } } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml index c46116d998c59..4ac15897dbb33 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml @@ -61,7 +61,7 @@ teardown: cluster.put_settings: body: transient: - search.disallow_slow_queries: null + search.allow_expensive_queries: null --- "Test basic": @@ -122,23 +122,23 @@ teardown: - match: { hits.hits.1._source.join_field.parent: "1" } --- -"HasChild disallow slow queries": +"HasChild disallow expensive queries": - skip: version: " - 7.99.99" reason: "implemented in 8.0.0" - ### Update setting to true + ### Update setting to false - do: cluster.put_settings: body: transient: - search.disallow_slow_queries: "true" + search.allow_expensive_queries: "false" flat_settings: true - - match: {transient: {search.disallow_slow_queries: "true"}} + - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: body: sort: [ "id" ] diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml index 2791a20970db3..12493b1fc1c11 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml @@ -54,7 +54,7 @@ teardown: cluster.put_settings: body: transient: - search.disallow_slow_queries: null + search.allow_expensive_queries: null --- "Test two sub-queries with only one having inner_hits": @@ -86,23 +86,23 @@ teardown: - match: { hits.hits.1.inner_hits.question.hits.hits.0._id: "1"} --- -"HasParent disallow slow queries": +"HasParent disallow expensive queries": - skip: version: " - 7.99.99" reason: "implemented in 8.0.0" - ### Update setting to true + ### Update setting to false - do: cluster.put_settings: body: transient: - search.disallow_slow_queries: "true" + search.allow_expensive_queries: "false" flat_settings: true - - match: {transient: {search.disallow_slow_queries: "true"}} + - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml index 294f9bf44debb..39a1203e24190 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -39,10 +39,10 @@ teardown: cluster.put_settings: body: transient: - search.disallow_slow_queries: null + search.allow_expensive_queries: null --- -"Test disallow slow queries": +"Test disallow expensive queries": - skip: version: " - 7.99.99" reason: "implemented in 8.0.0" @@ -52,7 +52,7 @@ teardown: cluster.get_settings: flat_settings: true - - match: {search.disallow_slow_queries: null} + - match: {search.allow_expensive_queries: null} ### Prefix - do: @@ -117,19 +117,19 @@ teardown: - match: { hits.total.value: 1 } - ### Update setting to true + ### Update setting to false - do: cluster.put_settings: body: transient: - search.disallow_slow_queries: "true" + search.allow_expensive_queries: "false" flat_settings: true - - match: {transient: {search.disallow_slow_queries: "true"}} + - match: {transient: {search.allow_expensive_queries: "false"}} ### Prefix - do: - catch: /prefix queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -140,7 +140,7 @@ teardown: ### Fuzzy - do: - catch: /fuzzy queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /fuzzy queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -151,7 +151,7 @@ teardown: ### Regexp - do: - catch: /regexp queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /regexp queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -162,7 +162,7 @@ teardown: ### Wildcard - do: - catch: /wildcard queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /wildcard queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -173,7 +173,7 @@ teardown: ### Nested - do: - catch: /joining queries cannot be executed when \'search.disallow_slow_queries\' is set to true/ + catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -184,15 +184,15 @@ teardown: bool: must: [{"match" : {"nested1.foo" : "bar2"}}] - ### Revert setting to false + ### Revert setting to true - do: cluster.put_settings: body: transient: - search.disallow_slow_queries: "false" + search.allow_expensive_queries: "true" flat_settings: true - - match: {transient: {search.disallow_slow_queries: "false"}} + - match: {transient: {search.allow_expensive_queries: "true"}} ### Prefix - do: diff --git a/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java b/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java index 88eb4538cb983..864abeb8fcc6c 100644 --- a/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java +++ b/server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java @@ -388,7 +388,7 @@ public void apply(Settings value, Settings current, Settings previous) { SearchService.DEFAULT_KEEPALIVE_SETTING, SearchService.KEEPALIVE_INTERVAL_SETTING, SearchService.MAX_KEEPALIVE_SETTING, - SearchService.DISALLOW_SLOW_QUERIES, + SearchService.ALLOW_EXPENSIVE_QUERIES, MultiBucketConsumerService.MAX_BUCKET_SETTING, SearchService.LOW_LEVEL_CANCELLATION_SETTING, SearchService.MAX_OPEN_SCROLL_CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/index/IndexModule.java b/server/src/main/java/org/elasticsearch/index/IndexModule.java index 6e10316e0ff96..ee020595dbda5 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexModule.java +++ b/server/src/main/java/org/elasticsearch/index/IndexModule.java @@ -130,7 +130,7 @@ public final class IndexModule { private final List searchOperationListeners = new ArrayList<>(); private final List indexOperationListeners = new ArrayList<>(); private final AtomicBoolean frozen = new AtomicBoolean(false); - private final BooleanSupplier isDisallowSlowQueries; + private final BooleanSupplier isAllowExpensiveQueries; /** * Construct the index module for the index with the specified index settings. The index module contains extension points for plugins @@ -146,14 +146,14 @@ public IndexModule( final AnalysisRegistry analysisRegistry, final EngineFactory engineFactory, final Map directoryFactories, - final BooleanSupplier isDisallowSlowQueries) { + final BooleanSupplier isAllowExpensiveQueries) { this.indexSettings = indexSettings; this.analysisRegistry = analysisRegistry; this.engineFactory = Objects.requireNonNull(engineFactory); this.searchOperationListeners.add(new SearchSlowLog(indexSettings)); this.indexOperationListeners.add(new IndexingSlowLog(indexSettings)); this.directoryFactories = Collections.unmodifiableMap(directoryFactories); - this.isDisallowSlowQueries = isDisallowSlowQueries; + this.isAllowExpensiveQueries = isAllowExpensiveQueries; } // For testing @@ -162,7 +162,7 @@ public IndexModule( final AnalysisRegistry analysisRegistry, final EngineFactory engineFactory, final Map directoryFactories) { - this(indexSettings, analysisRegistry, engineFactory, directoryFactories, () -> false); + this(indexSettings, analysisRegistry, engineFactory, directoryFactories, () -> true); } /** @@ -436,7 +436,7 @@ public IndexService newIndexService( new SimilarityService(indexSettings, scriptService, similarities), shardStoreDeleter, indexAnalyzers, engineFactory, circuitBreakerService, bigArrays, threadPool, scriptService, clusterService, client, queryCache, directoryFactory, eventListener, readerWrapperFactory, mapperRegistry, indicesFieldDataCache, searchOperationListeners, - indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled, isDisallowSlowQueries); + indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled, isAllowExpensiveQueries); success = true; return indexService; } finally { diff --git a/server/src/main/java/org/elasticsearch/index/IndexService.java b/server/src/main/java/org/elasticsearch/index/IndexService.java index 43cc71f7ecd8c..fe85ff260fe0c 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexService.java +++ b/server/src/main/java/org/elasticsearch/index/IndexService.java @@ -126,7 +126,7 @@ public class IndexService extends AbstractIndexComponent implements IndicesClust private final IndexSettings indexSettings; private final List searchOperationListeners; private final List indexingOperationListeners; - private final BooleanSupplier isDisallowSlowQueries; + private final BooleanSupplier isAllowExpensiveQueries; private volatile AsyncRefreshTask refreshTask; private volatile AsyncTranslogFSync fsyncTask; private volatile AsyncGlobalCheckpointTask globalCheckpointTask; @@ -168,9 +168,9 @@ public IndexService( List indexingOperationListeners, NamedWriteableRegistry namedWriteableRegistry, BooleanSupplier idFieldDataEnabled, - BooleanSupplier isDisallowSlowQueries) { + BooleanSupplier isAllowExpensiveQueries) { super(indexSettings); - this.isDisallowSlowQueries = isDisallowSlowQueries; + this.isAllowExpensiveQueries = isAllowExpensiveQueries; this.indexSettings = indexSettings; this.xContentRegistry = xContentRegistry; this.similarityService = similarityService; @@ -571,7 +571,7 @@ public QueryShardContext newQueryShardContext(int shardId, IndexSearcher searche return new QueryShardContext( shardId, indexSettings, bigArrays, indexCache.bitsetFilterCache(), indexFieldData::getForField, mapperService(), similarityService(), scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, clusterAlias, - indexNameMatcher, isDisallowSlowQueries); + indexNameMatcher, isAllowExpensiveQueries); } /** diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index e06fe9556df00..17d2a57e01824 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -39,7 +39,7 @@ import java.util.List; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; /** Base class for {@link MappedFieldType} implementations that use the same * representation for internal index terms as the external representation so @@ -66,9 +66,9 @@ public Query termsQuery(List values, QueryShardContext context) { @Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions, QueryShardContext context) { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("fuzzy queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("fuzzy queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } failIfNotIndexed(); return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)), @@ -77,9 +77,9 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int @Override public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("prefix queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("prefix queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); @@ -96,9 +96,9 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu return termQuery; } - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("wildcard queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("wildcard queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } Term term = MappedFieldType.extractTerm(termQuery); @@ -110,9 +110,9 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu @Override public Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("regexp queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("regexp queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } failIfNotIndexed(); RegexpQuery query = new RegexpQuery(new Term(name(), indexedValueForSearch(value)), flags, maxDeterminizedStates); diff --git a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index 4beeab236d01b..e85b722c5c960 100644 --- a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -58,7 +58,7 @@ import java.util.Map; import java.util.Objects; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; import static org.elasticsearch.search.fetch.subphase.InnerHitsContext.intersect; public class NestedQueryBuilder extends AbstractQueryBuilder { @@ -268,9 +268,9 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("joining queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } ObjectMapper nestedObjectMapper = context.getObjectMapper(path); diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 50b8aee218b2a..085df7b765478 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -93,7 +93,7 @@ public class QueryShardContext extends QueryRewriteContext { private final Index fullyQualifiedIndex; private final Predicate indexNameMatcher; - private final BooleanSupplier isDisallowSlowQueries; + private final BooleanSupplier isAllowExpensiveQueries; private final Map namedQueries = new HashMap<>(); private boolean allowUnmappedFields; @@ -115,11 +115,11 @@ public QueryShardContext(int shardId, LongSupplier nowInMillis, String clusterAlias, Predicate indexNameMatcher, - BooleanSupplier isDisallowSlowQueries) { + BooleanSupplier isAllowExpensiveQueries) { this(shardId, indexSettings, bigArrays, bitsetFilterCache, indexFieldDataLookup, mapperService, similarityService, scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, indexNameMatcher, new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), - indexSettings.getIndex().getUUID()), isDisallowSlowQueries); + indexSettings.getIndex().getUUID()), isAllowExpensiveQueries); } public QueryShardContext(int shardId, @@ -140,14 +140,14 @@ public QueryShardContext(int shardId, this(shardId, indexSettings, bigArrays, bitsetFilterCache, indexFieldDataLookup, mapperService, similarityService, scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, indexNameMatcher, new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), - indexSettings.getIndex().getUUID()), () -> false); + indexSettings.getIndex().getUUID()), () -> true); } public QueryShardContext(QueryShardContext source) { this(source.shardId, source.indexSettings, source.bigArrays, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.getXContentRegistry(), source.getWriteableRegistry(), source.client, source.searcher, source.nowInMillis, source.indexNameMatcher, - source.fullyQualifiedIndex, source.isDisallowSlowQueries); + source.fullyQualifiedIndex, source.isAllowExpensiveQueries); } private QueryShardContext(int shardId, @@ -165,7 +165,7 @@ private QueryShardContext(int shardId, LongSupplier nowInMillis, Predicate indexNameMatcher, Index fullyQualifiedIndex, - BooleanSupplier isDisallowSlowQueries) { + BooleanSupplier isAllowExpensiveQueries) { super(xContentRegistry, namedWriteableRegistry, client, nowInMillis); this.shardId = shardId; this.similarityService = similarityService; @@ -180,7 +180,7 @@ private QueryShardContext(int shardId, this.searcher = searcher; this.indexNameMatcher = indexNameMatcher; this.fullyQualifiedIndex = fullyQualifiedIndex; - this.isDisallowSlowQueries = isDisallowSlowQueries; + this.isAllowExpensiveQueries = isAllowExpensiveQueries; } private void reset() { @@ -218,8 +218,8 @@ public BitSetProducer bitsetFilter(Query filter) { return bitsetFilterCache.getBitSetProducer(filter); } - public boolean isDisallowSlowQueries() { - return isDisallowSlowQueries.getAsBoolean(); + public boolean isAllowExpensiveQueries() { + return isAllowExpensiveQueries.getAsBoolean(); } @SuppressWarnings("unchecked") diff --git a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 33cd22bc79b38..857c9f0129de7 100644 --- a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -41,7 +41,7 @@ import java.io.IOException; import java.util.Objects; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; public class ScriptQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "script"; @@ -133,9 +133,9 @@ public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOEx @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("script queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("script queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } FilterScript.Factory factory = context.compile(script, FilterScript.CONTEXT); FilterScript.LeafFactory filterScript = factory.newFactory(script.getParams(), context.lookup()); diff --git a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java index f4746d92d26e5..7f73bc5f3399d 100644 --- a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java @@ -43,7 +43,7 @@ import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; /** * A query that computes a document score based on the provided script @@ -172,9 +172,9 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isDisallowSlowQueries() == true) { - throw new ElasticsearchException("script score queries cannot be executed when '" + DISALLOW_SLOW_QUERIES.getKey() + - "' is set to true"); + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("script score queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + + "' is set to false"); } ScoreScript.Factory factory = context.compile(script, ScoreScript.CONTEXT); ScoreScript.LeafFactory scoreScriptFactory = factory.newFactory(script.getParams(), context.lookup()); diff --git a/server/src/main/java/org/elasticsearch/indices/IndicesService.java b/server/src/main/java/org/elasticsearch/indices/IndicesService.java index 26744f08a4012..2ba68d698c072 100644 --- a/server/src/main/java/org/elasticsearch/indices/IndicesService.java +++ b/server/src/main/java/org/elasticsearch/indices/IndicesService.java @@ -168,7 +168,7 @@ import static org.elasticsearch.index.IndexService.IndexCreationContext.CREATE_INDEX; import static org.elasticsearch.index.IndexService.IndexCreationContext.META_DATA_VERIFICATION; import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; -import static org.elasticsearch.search.SearchService.DISALLOW_SLOW_QUERIES; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; public class IndicesService extends AbstractLifecycleComponent implements IndicesClusterStateService.AllocatedIndices, IndexService.ShardStoreDeleter { @@ -221,7 +221,7 @@ public class IndicesService extends AbstractLifecycleComponent final AbstractRefCounted indicesRefCount; // pkg-private for testing private final CountDownLatch closeLatch = new CountDownLatch(1); private volatile boolean idFieldDataEnabled; - private volatile boolean disallowSlowQueries; + private volatile boolean allowExpensiveQueries; @Nullable private final EsThreadPoolExecutor danglingIndicesThreadPoolExecutor; @@ -319,8 +319,8 @@ protected void closeInternal() { daemonThreadFactory(nodeName, DANGLING_INDICES_UPDATE_THREAD_NAME), threadPool.getThreadContext()) : null; - this.disallowSlowQueries = DISALLOW_SLOW_QUERIES.get(clusterService.getSettings()); - clusterService.getClusterSettings().addSettingsUpdateConsumer(DISALLOW_SLOW_QUERIES, this::setDisallowSlowQueries); + this.allowExpensiveQueries = ALLOW_EXPENSIVE_QUERIES.get(clusterService.getSettings()); + clusterService.getClusterSettings().addSettingsUpdateConsumer(ALLOW_EXPENSIVE_QUERIES, this::setAllowExpensiveQueries); } private static final String DANGLING_INDICES_UPDATE_THREAD_NAME = "DanglingIndices#updateTask"; @@ -592,7 +592,7 @@ private synchronized IndexService createIndexService(IndexService.IndexCreationC indexCreationContext); final IndexModule indexModule = new IndexModule(idxSettings, analysisRegistry, getEngineFactory(idxSettings), - directoryFactories, () -> disallowSlowQueries); + directoryFactories, () -> allowExpensiveQueries); for (IndexingOperationListener operationListener : indexingOperationListeners) { indexModule.addIndexOperationListener(operationListener); } @@ -662,7 +662,7 @@ private EngineFactory getEngineFactory(final IndexSettings idxSettings) { public synchronized MapperService createIndexMapperService(IndexMetaData indexMetaData) throws IOException { final IndexSettings idxSettings = new IndexSettings(indexMetaData, this.settings, indexScopedSettings); final IndexModule indexModule = new IndexModule(idxSettings, analysisRegistry, getEngineFactory(idxSettings), - directoryFactories, () -> disallowSlowQueries); + directoryFactories, () -> allowExpensiveQueries); pluginsService.onIndexModule(indexModule); return indexModule.newIndexMapperService(xContentRegistry, mapperRegistry, scriptService); } @@ -1579,8 +1579,8 @@ protected void doRun() { } } - private void setDisallowSlowQueries(Boolean disallowSlowQueries) { - this.disallowSlowQueries = disallowSlowQueries; + private void setAllowExpensiveQueries(Boolean allowExpensiveQueries) { + this.allowExpensiveQueries = allowExpensiveQueries; } // visible for testing diff --git a/server/src/main/java/org/elasticsearch/search/SearchService.java b/server/src/main/java/org/elasticsearch/search/SearchService.java index c223feae90531..51b0e340c04ae 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchService.java +++ b/server/src/main/java/org/elasticsearch/search/SearchService.java @@ -134,8 +134,8 @@ public class SearchService extends AbstractLifecycleComponent implements IndexEv Setting.positiveTimeSetting("search.max_keep_alive", timeValueHours(24), Property.NodeScope, Property.Dynamic); public static final Setting KEEPALIVE_INTERVAL_SETTING = Setting.positiveTimeSetting("search.keep_alive_interval", timeValueMinutes(1), Property.NodeScope); - public static final Setting DISALLOW_SLOW_QUERIES = - Setting.boolSetting("search.disallow_slow_queries", false, Property.NodeScope, Property.Dynamic); + public static final Setting ALLOW_EXPENSIVE_QUERIES = + Setting.boolSetting("search.allow_expensive_queries", true, Property.NodeScope, Property.Dynamic); /** * Enables low-level, frequent search cancellation checks. Enabling low-level checks will make long running searches to react diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index fe18f75b0dc18..8c49fe8490e69 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -44,8 +44,8 @@ public void testPrefixQuery() { assertEquals(expected, ft.prefixQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -58,8 +58,8 @@ public void testRegexpQuery() { assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -72,8 +72,8 @@ public void testWildcardQuery() { assertEquals(expected, ft.wildcardQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("wildcard queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java index 5dc517be175aa..d0267b791d955 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java @@ -164,8 +164,8 @@ public void testRegexpQuery() { assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -183,8 +183,8 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, - randomBoolean(), MOCK_QSC_DISALLOW_SLOW)); - assertEquals("fuzzy queries cannot be executed when 'search.disallow_slow_queries' is set to true", + randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index e0144abf6bf91..8a628cc1f9e7c 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -43,8 +43,8 @@ public void testPrefixQuery() { assertEquals(expected, ft.prefixQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -57,8 +57,8 @@ public void testRegexpQuery() { assertEquals(expected, ft.regexpQuery("foo?", 0, 10, null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -71,8 +71,8 @@ public void testWildcardQuery() { assertEquals(expected, ft.wildcardQuery("foo*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("wildcard queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index 670ebcac60be1..8b46a022f512c 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -145,8 +145,8 @@ public void testRegexpQuery() { assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -164,8 +164,8 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, - randomBoolean(), MOCK_QSC_DISALLOW_SLOW)); - assertEquals("fuzzy queries cannot be executed when 'search.disallow_slow_queries' is set to true", + randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -181,8 +181,8 @@ public void testIndexPrefixes() { assertEquals(new PrefixQuery(new Term("field", "internationalisatio")), q); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, randomMockShardContext()); diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 7e0fd7dd7fb0c..3410cd44ade1f 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -358,14 +358,14 @@ public void testExtractInnerHitBuildersWithDuplicate() { assertEquals("[inner_hits] already contains an entry for key [some_name]", e.getMessage()); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); NestedQueryBuilder queryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index a714dfe894a5e..6241c50b3e1bc 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -132,14 +132,14 @@ public void testCacheability() throws IOException { assertFalse("query should not be cacheable: " + queryBuilder.toString(), context.isCacheable()); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); ScriptQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("script queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java index 1e29cf3453082..2075424c2f90c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java @@ -99,14 +99,14 @@ public void testCacheability() throws IOException { assertFalse("query should not be cacheable: " + queryBuilder.toString(), context.isCacheable()); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(true); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); ScriptScoreQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("script score queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java index 961581bb0d6cb..9e4c9e4cc1124 100644 --- a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java @@ -74,7 +74,7 @@ protected Map, Object>> pluginScripts() { @After public void resetSettings() { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", (String) null)); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } @@ -165,7 +165,7 @@ public void testRewrittenQuery() { assertOrderedSearchHits(resp, "3", "2", "1"); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { assertAcked( prepareCreate("test-index").setMapping("field1", "type=text", "field2", "type=double") ); @@ -186,7 +186,7 @@ public void testDisallowSlowQueries() { assertNoFailures(resp); ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", true)); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); ElasticsearchException e = expectThrows(ElasticsearchException.class, @@ -194,11 +194,11 @@ public void testDisallowSlowQueries() { .prepareSearch("test-index") .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) .get()); - assertEquals("script score queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", false)); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); resp = client() .prepareSearch("test-index") diff --git a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java index 98f9f774679d6..f7520e5677a04 100644 --- a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java @@ -59,7 +59,7 @@ public class ScriptQuerySearchIT extends ESIntegTestCase { @After public void resetSettings() { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", (String) null)); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } @@ -233,7 +233,7 @@ public void testCustomScriptBoost() throws Exception { assertThat(response.getHits().getAt(2).getFields().get("sNum1").getValues().get(0), equalTo(3.0)); } - public void testDisallowSlowQueries() { + public void testDisallowExpensiveQueries() { assertAcked( prepareCreate("test-index").setMapping("num1", "type=double") ); @@ -253,7 +253,7 @@ public void testDisallowSlowQueries() { assertNoFailures(resp); ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", true)); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); ElasticsearchException e = expectThrows(ElasticsearchException.class, @@ -261,11 +261,11 @@ public void testDisallowSlowQueries() { .prepareSearch("test-index") .setQuery(scriptQuery(script)) .get()); - assertEquals("script queries cannot be executed when 'search.disallow_slow_queries' is set to true", + assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.disallow_slow_queries", false)); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); resp = client().prepareSearch("test-index") .setQuery(scriptQuery(script)) diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java index 405a5b8817f1a..1a509a37b2cfc 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -37,8 +37,8 @@ /** Base test case for subclasses of MappedFieldType */ public abstract class FieldTypeTestCase extends ESTestCase { - public static final QueryShardContext MOCK_QSC = createMockQueryShardContext(false); - public static final QueryShardContext MOCK_QSC_DISALLOW_SLOW = createMockQueryShardContext(true); + public static final QueryShardContext MOCK_QSC = createMockQueryShardContext(true); + public static final QueryShardContext MOCK_QSC_DISALLOW_EXPENSIVE = createMockQueryShardContext(false); /** Abstraction for mutating a property of a MappedFieldType */ public abstract static class Modifier { @@ -250,12 +250,12 @@ protected String toString(MappedFieldType ft) { } protected QueryShardContext randomMockShardContext() { - return randomFrom(MOCK_QSC, MOCK_QSC_DISALLOW_SLOW); + return randomFrom(MOCK_QSC, MOCK_QSC_DISALLOW_EXPENSIVE); } - static QueryShardContext createMockQueryShardContext(boolean disallowSlowQueries) { + static QueryShardContext createMockQueryShardContext(boolean allowExpensiveQueries) { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isDisallowSlowQueries()).thenReturn(disallowSlowQueries); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(allowExpensiveQueries); return queryShardContext; } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index fce6de477704e..78ac8f181fe60 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -102,8 +102,8 @@ public void testPrefixQuery() { assertEquals(expected, ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("prefix queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java index 2b856ebd01dc5..6a658e50bed43 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java @@ -84,8 +84,8 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("value", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, - randomBoolean(), MOCK_QSC_DISALLOW_SLOW)); - assertEquals("fuzzy queries cannot be executed when 'search.disallow_slow_queries' is set to true", + randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -113,8 +113,8 @@ public void testRegexpQuery() { assertEquals(expected, actual); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.regexpQuery("val.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("regexp queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.regexpQuery("val.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -126,8 +126,8 @@ public void testWildcardQuery() { assertEquals(expected, ft.wildcardQuery("valu*", null, MOCK_QSC)); ElasticsearchException ee = expectThrows(ElasticsearchException.class, - () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_SLOW)); - assertEquals("wildcard queries cannot be executed when 'search.disallow_slow_queries' is set to true", + () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java index 8141bb6ad45b9..b54f3c78c3e6a 100644 --- a/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java +++ b/x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/WatcherPluginTests.java @@ -75,7 +75,7 @@ public void testWatcherDisabledTests() throws Exception { AnalysisRegistry registry = new AnalysisRegistry(TestEnvironment.newEnvironment(settings), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()); IndexModule indexModule = new IndexModule(indexSettings, registry, new InternalEngineFactory(), Collections.emptyMap(), - () -> false); + () -> true); // this will trip an assertion if the watcher indexing operation listener is null (which it is) but we try to add it watcher.onIndexModule(indexModule); From 2dd5d9af57763c2a183aca1a63b95c90c519c40d Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Wed, 5 Feb 2020 21:26:19 +0100 Subject: [PATCH 06/25] Add percolator queries to the expensive ones --- .../query-dsl/percolate-query.asciidoc | 5 ++ .../percolator/PercolateQueryBuilder.java | 7 +++ .../PercolateQueryBuilderTests.java | 13 +++++ .../percolator/PercolatorQuerySearchIT.java | 57 +++++++++++++++++++ .../search/query/ScriptScoreQueryIT.java | 3 + .../scriptfilter/ScriptQuerySearchIT.java | 3 + 6 files changed, 88 insertions(+) diff --git a/docs/reference/query-dsl/percolate-query.asciidoc b/docs/reference/query-dsl/percolate-query.asciidoc index 2094be0ccd996..e9d5bcf096270 100644 --- a/docs/reference/query-dsl/percolate-query.asciidoc +++ b/docs/reference/query-dsl/percolate-query.asciidoc @@ -686,3 +686,8 @@ being percolated, as opposed to a single index as we do in examples. There are a allows for fields to be stored in a denser, more efficient way. - Percolate queries do not scale in the same way as other queries, so percolation performance may benefit from using a different index configuration, like the number of primary shards. + +=== Notes +==== Allow expensive queries +Percolate queries will not be executed if <> +is set to false. diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index 1f083aa207613..52410d5099e0a 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -87,6 +87,8 @@ import java.util.Objects; import java.util.function.Supplier; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; + public class PercolateQueryBuilder extends AbstractQueryBuilder { public static final String NAME = "percolate"; @@ -491,6 +493,11 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) { @Override protected Query doToQuery(QueryShardContext context) throws IOException { + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("percolate queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + } + // Call nowInMillis() so that this query becomes un-cacheable since we // can't be sure that it doesn't use now or scripts context.nowInMillis(); diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java index 0c914295f9fc8..df33fc89fb52a 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.percolator; import org.apache.lucene.search.Query; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.get.GetRequest; @@ -57,6 +58,8 @@ import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO; import static org.hamcrest.Matchers.equalTo; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class PercolateQueryBuilderTests extends AbstractQueryTestCase { @@ -341,4 +344,14 @@ public void testSettingNameWhileRewritingWhenDocumentSupplierAndSourceNotNull() assertNotEquals(rewrittenQueryBuilder, percolateQueryBuilder); } + public void testDisallowExpensiveQueries() { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + + PercolateQueryBuilder queryBuilder = doCreateTestQueryBuilder(true); + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> queryBuilder.toQuery(queryShardContext)); + assertEquals("percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + e.getMessage()); + } } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index c726d203087ed..2ba0e5514e8ff 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -19,11 +19,14 @@ package org.elasticsearch.percolator; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -36,7 +39,9 @@ import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; +import org.junit.After; +import java.io.IOException; import java.util.Arrays; import java.util.Collections; @@ -66,6 +71,13 @@ public class PercolatorQuerySearchIT extends ESIntegTestCase { + @After + public void resetSettings() { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } + public void testPercolatorQuery() throws Exception { assertAcked(client().admin().indices().prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=keyword", "field2", "type=keyword", "query", "type=percolator") @@ -875,4 +887,49 @@ public void testPercolatorQueryViaMultiSearch() throws Exception { assertThat(item.getFailureMessage(), containsString("[test/6] couldn't be found")); } + public void testDisallowExpensiveQueries() throws IOException { + assertAcked(client().admin().indices().prepareCreate("test") + .setMapping("id", "type=keyword", "field1", "type=keyword", "query", "type=percolator") + ); + + client().prepareIndex("test").setId("1") + .setSource(jsonBuilder().startObject() + .field("id", "1") + .field("query", matchQuery("field1", "value")).endObject()) + .get(); + refresh(); + + // Execute with search.allow_expensive_queries = null => default value = false => success + BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()); + SearchResponse response = client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get(); + assertHitCount(response, 1); + assertThat(response.getHits().getAt(0).getId(), equalTo("1")); + assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); + + // Set search.allow_expensive_queries to "false" => assert failure + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get()); + assertEquals("percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + e.getCause().getMessage()); + + // Set search.allow_expensive_queries setting to "true" ==> success + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + response = client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get(); + assertHitCount(response, 1); + assertThat(response.getHits().getAt(0).getId(), equalTo("1")); + assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); + } } diff --git a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java index 9e4c9e4cc1124..a1a824b6d0624 100644 --- a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java @@ -177,6 +177,7 @@ public void testDisallowExpensiveQueries() { } refresh(); + // Execute with search.allow_expensive_queries = null => default value = true => success Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['field2'].value * param1", Map.of("param1", 0.1)); SearchResponse resp = client() @@ -185,6 +186,7 @@ public void testDisallowExpensiveQueries() { .get(); assertNoFailures(resp); + // Set search.allow_expensive_queries to "false" => assert failure ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); @@ -197,6 +199,7 @@ public void testDisallowExpensiveQueries() { assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); + // Set search.allow_expensive_queries to "true" => success updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); diff --git a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java index f7520e5677a04..683905e0e22be 100644 --- a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java @@ -245,6 +245,7 @@ public void testDisallowExpensiveQueries() { } refresh(); + // Execute with search.allow_expensive_queries = null => default value = false => success Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['num1'].value > 1", Collections.emptyMap()); SearchResponse resp = client().prepareSearch("test-index") @@ -256,6 +257,7 @@ public void testDisallowExpensiveQueries() { updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + // Set search.allow_expensive_queries to "false" => assert failure ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client() .prepareSearch("test-index") @@ -264,6 +266,7 @@ public void testDisallowExpensiveQueries() { assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); + // Set search.allow_expensive_queries to "true" => success updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); From 65ff23ea4e3e4fd4011af1339cb73e0172f805e4 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Wed, 5 Feb 2020 23:49:18 +0100 Subject: [PATCH 07/25] Add legacy geo-shape queries to the disallowed ones --- .../mapping/types/geo-shape.asciidoc | 4 + .../query-dsl/geo-shape-query.asciidoc | 4 + .../percolator/PercolatorQuerySearchIT.java | 94 ++++++++++--------- .../query/LegacyGeoShapeQueryProcessor.java | 8 ++ .../ScriptScoreQueryBuilder.java | 4 +- .../LegacyGeoShapeFieldMapperTests.java | 33 ++++++- .../geo/LegacyGeoShapeIntegrationIT.java | 52 ++++++++++ .../search/query/ScriptScoreQueryIT.java | 94 +++++++++---------- .../scriptfilter/ScriptQuerySearchIT.java | 90 +++++++++--------- 9 files changed, 241 insertions(+), 142 deletions(-) diff --git a/docs/reference/mapping/types/geo-shape.asciidoc b/docs/reference/mapping/types/geo-shape.asciidoc index 5ff464da9b995..d2aaa14651279 100644 --- a/docs/reference/mapping/types/geo-shape.asciidoc +++ b/docs/reference/mapping/types/geo-shape.asciidoc @@ -252,6 +252,10 @@ between index size and a reasonable level of precision of 50m at the equator. This allows for indexing tens of millions of shapes without overly bloating the resulting index too much relative to the input size. +[NOTE] +Geo-shape queries on geo-shapes implemented with PrefixTrees will not be executed if +<> is set to false. + [[input-structure]] [float] ==== Input Structure diff --git a/docs/reference/query-dsl/geo-shape-query.asciidoc b/docs/reference/query-dsl/geo-shape-query.asciidoc index 19a22ee103d91..9706b90d82845 100644 --- a/docs/reference/query-dsl/geo-shape-query.asciidoc +++ b/docs/reference/query-dsl/geo-shape-query.asciidoc @@ -161,3 +161,7 @@ and will not match any documents for this query. This can be useful when querying multiple indexes which might have different mappings. When set to `false` (the default value) the query will throw an exception if the field is not mapped. + +==== Notes +Geo-shape queries on geo-shapes implemented with <> will not be executed if +<> is set to false. diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index 2ba0e5514e8ff..fb6c4f1dfdeb8 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -73,9 +73,7 @@ public class PercolatorQuerySearchIT extends ESIntegTestCase { @After public void resetSettings() { - ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } public void testPercolatorQuery() throws Exception { @@ -888,48 +886,54 @@ public void testPercolatorQueryViaMultiSearch() throws Exception { } public void testDisallowExpensiveQueries() throws IOException { - assertAcked(client().admin().indices().prepareCreate("test") - .setMapping("id", "type=keyword", "field1", "type=keyword", "query", "type=percolator") - ); - - client().prepareIndex("test").setId("1") - .setSource(jsonBuilder().startObject() - .field("id", "1") - .field("query", matchQuery("field1", "value")).endObject()) - .get(); - refresh(); - - // Execute with search.allow_expensive_queries = null => default value = false => success - BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()); - SearchResponse response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); - assertHitCount(response, 1); - assertThat(response.getHits().getAt(0).getId(), equalTo("1")); - assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); - - // Set search.allow_expensive_queries to "false" => assert failure - ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get()); - assertEquals("percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", - e.getCause().getMessage()); - - // Set search.allow_expensive_queries setting to "true" ==> success - updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + try { + assertAcked(client().admin().indices().prepareCreate("test") + .setMapping("id", "type=keyword", "field1", "type=keyword", "query", "type=percolator") + ); + + client().prepareIndex("test").setId("1") + .setSource(jsonBuilder().startObject() + .field("id", "1") + .field("query", matchQuery("field1", "value")).endObject()) + .get(); + refresh(); - response = client().prepareSearch() - .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) - .get(); - assertHitCount(response, 1); - assertThat(response.getHits().getAt(0).getId(), equalTo("1")); - assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); + // Execute with search.allow_expensive_queries = null => default value = false => success + BytesReference source = BytesReference.bytes(jsonBuilder().startObject().field("field1", "value").endObject()); + SearchResponse response = client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get(); + assertHitCount(response, 1); + assertThat(response.getHits().getAt(0).getId(), equalTo("1")); + assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); + + // Set search.allow_expensive_queries to "false" => assert failure + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get()); + assertEquals("percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + e.getCause().getMessage()); + + // Set search.allow_expensive_queries setting to "true" ==> success + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + response = client().prepareSearch() + .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) + .get(); + assertHitCount(response, 1); + assertThat(response.getHits().getAt(0).getId(), equalTo("1")); + assertThat(response.getHits().getAt(0).getFields().get("_percolator_document_slot").getValue(), equalTo(0)); + } finally { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } } } diff --git a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java index 14307d846b67e..7af0b7b90a47e 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -26,6 +26,7 @@ import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy; import org.apache.lucene.spatial.query.SpatialArgs; import org.apache.lucene.spatial.query.SpatialOperation; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.SpatialStrategy; import org.elasticsearch.common.geo.builders.CircleBuilder; @@ -59,6 +60,8 @@ import java.util.ArrayList; import java.util.List; +import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES; + public class LegacyGeoShapeQueryProcessor implements AbstractGeometryFieldMapper.QueryProcessor { private AbstractGeometryFieldMapper.AbstractGeometryFieldType ft; @@ -74,6 +77,11 @@ public Query process(Geometry shape, String fieldName, ShapeRelation relation, Q @Override public Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context) { + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("geo shape queries on PrefixTree geo shapes cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + } + LegacyGeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (LegacyGeoShapeFieldMapper.GeoShapeFieldType) ft; SpatialStrategy spatialStrategy = shapeFieldType.strategy(); if (strategy != null) { diff --git a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java index 7f73bc5f3399d..a0158f2c59ae1 100644 --- a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java @@ -173,8 +173,8 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("script score queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("script score queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } ScoreScript.Factory factory = context.compile(script, ScoreScript.CONTEXT); ScoreScript.LeafFactory scoreScriptFactory = factory.newFactory(script.getParams(), context.lookup()); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java index d53eb0bcac134..330b4c192d4a0 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java @@ -22,15 +22,20 @@ import org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy; import org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree; import org.apache.lucene.spatial.prefix.tree.QuadPrefixTree; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Explicit; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.geo.GeoUtils; +import org.elasticsearch.common.geo.ShapeRelation; +import org.elasticsearch.common.geo.SpatialStrategy; import org.elasticsearch.common.geo.builders.ShapeBuilder; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.geometry.Point; +import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.test.InternalSettingsPlugin; @@ -44,6 +49,8 @@ import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.not; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class LegacyGeoShapeFieldMapperTests extends ESSingleNodeTestCase { @@ -695,6 +702,31 @@ public void testPointsOnlyFalseWithTermStrategy() throws Exception { assertFieldWarnings("tree", "precision", "strategy", "points_only"); } + public void testDisallowExpensiveQueries() throws IOException { + QueryShardContext queryShardContext = mock(QueryShardContext.class); + when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("location") + .field("type", "geo_shape") + .field("tree", "quadtree") + .endObject().endObject() + .endObject().endObject()); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser() + .parse("type1", new CompressedXContent(mapping)); + Mapper fieldMapper = defaultMapper.mappers().getMapper("location"); + assertThat(fieldMapper, instanceOf(LegacyGeoShapeFieldMapper.class)); + LegacyGeoShapeFieldMapper geoShapeFieldMapper = (LegacyGeoShapeFieldMapper) fieldMapper; + + + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> geoShapeFieldMapper.fieldType().geometryQueryBuilder().process( + new Point(-10, 10), "location", SpatialStrategy.TERM, ShapeRelation.INTERSECTS, queryShardContext)); + assertEquals("geo shape queries on PrefixTree geo shapes cannot be executed when " + + "'search.allow_expensive_queries' is set to false", e.getMessage()); + assertFieldWarnings("tree"); + } + public String toXContentString(LegacyGeoShapeFieldMapper mapper, boolean includeDefaults) throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder().startObject(); ToXContent.Params params; @@ -710,5 +742,4 @@ public String toXContentString(LegacyGeoShapeFieldMapper mapper, boolean include public String toXContentString(LegacyGeoShapeFieldMapper mapper) throws IOException { return toXContentString(mapper, true); } - } diff --git a/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java b/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java index 421eb36380551..401e9541f65ec 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java +++ b/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java @@ -18,11 +18,14 @@ */ package org.elasticsearch.search.geo; +import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.builders.ShapeBuilder; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; @@ -33,6 +36,8 @@ import org.elasticsearch.indices.IndicesService; import org.elasticsearch.test.ESIntegTestCase; +import java.io.IOException; + import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; @@ -186,6 +191,53 @@ public void testLegacyCircle() throws Exception { assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); } + public void testDisallowExpensiveQueries() throws InterruptedException, IOException { + try { + // create index + assertAcked(client().admin().indices().prepareCreate("test") + .setMapping("shape", "type=geo_shape,strategy=recursive,tree=geohash").get()); + ensureGreen(); + + indexRandom(true, client().prepareIndex("test").setId("0").setSource( + "shape", (ToXContent) (builder, params) -> { + builder.startObject().field("type", "circle") + .startArray("coordinates").value(30).value(50).endArray() + .field("radius", "77km") + .endObject(); + return builder; + })); + refresh(); + + // Execute with search.allow_expensive_queries = null => default value = false => success + SearchResponse searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", + new Circle(30, 50, 77000))).get(); + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + // Set search.allow_expensive_queries to "false" => assert failure + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client().prepareSearch("test").setQuery(geoShapeQuery("shape", + new Circle(30, 50, 77000))).get()); + assertEquals("geo shape queries on PrefixTree geo shapes cannot be executed when " + + "'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); + + // Set search.allow_expensive_queries to "true" => success + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", + new Circle(30, 50, 77000))).get(); + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + } finally { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } + } + private String findNodeName(String index) { ClusterState state = client().admin().cluster().prepareState().get().getState(); IndexShardRoutingTable shard = state.getRoutingTable().index(index).shard(0); diff --git a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java index a1a824b6d0624..f8e4f9c7e69f4 100644 --- a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java @@ -31,7 +31,6 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptType; import org.elasticsearch.test.ESIntegTestCase; -import org.junit.After; import java.util.Collection; import java.util.Collections; @@ -71,13 +70,6 @@ protected Map, Object>> pluginScripts() { } } - @After - public void resetSettings() { - ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - } - // test that script_score works as expected: // 1) only matched docs retrieved // 2) score is calculated based on a script with params @@ -166,47 +158,53 @@ public void testRewrittenQuery() { } public void testDisallowExpensiveQueries() { - assertAcked( - prepareCreate("test-index").setMapping("field1", "type=text", "field2", "type=double") - ); - int docCount = 10; - for (int i = 1; i <= docCount; i++) { - client().prepareIndex("test-index").setId("" + i) - .setSource("field1", "text" + (i % 2), "field2", i ) + try { + assertAcked( + prepareCreate("test-index").setMapping("field1", "type=text", "field2", "type=double") + ); + int docCount = 10; + for (int i = 1; i <= docCount; i++) { + client().prepareIndex("test-index").setId("" + i) + .setSource("field1", "text" + (i % 2), "field2", i) + .get(); + } + refresh(); + + // Execute with search.allow_expensive_queries = null => default value = true => success + Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['field2'].value * param1", + Map.of("param1", 0.1)); + SearchResponse resp = client() + .prepareSearch("test-index") + .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) .get(); + assertNoFailures(resp); + + // Set search.allow_expensive_queries to "false" => assert failure + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client() + .prepareSearch("test-index") + .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) + .get()); + assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", + e.getCause().getMessage()); + + // Set search.allow_expensive_queries to "true" => success + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + resp = client() + .prepareSearch("test-index") + .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) + .get(); + assertNoFailures(resp); + } finally { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } - refresh(); - - // Execute with search.allow_expensive_queries = null => default value = true => success - Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['field2'].value * param1", - Map.of("param1", 0.1)); - SearchResponse resp = client() - .prepareSearch("test-index") - .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) - .get(); - assertNoFailures(resp); - - // Set search.allow_expensive_queries to "false" => assert failure - ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> client() - .prepareSearch("test-index") - .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) - .get()); - assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", - e.getCause().getMessage()); - - // Set search.allow_expensive_queries to "true" => success - updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - resp = client() - .prepareSearch("test-index") - .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) - .get(); - assertNoFailures(resp); } } diff --git a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java index 683905e0e22be..eaf31285ffc97 100644 --- a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java @@ -34,7 +34,6 @@ import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.InternalSettingsPlugin; -import org.junit.After; import java.io.IOException; import java.util.Arrays; @@ -56,13 +55,6 @@ @ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.SUITE) public class ScriptQuerySearchIT extends ESIntegTestCase { - @After - public void resetSettings() { - ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - } - @Override protected Collection> nodePlugins() { return Arrays.asList(CustomScriptPlugin.class, InternalSettingsPlugin.class); @@ -234,46 +226,52 @@ public void testCustomScriptBoost() throws Exception { } public void testDisallowExpensiveQueries() { - assertAcked( - prepareCreate("test-index").setMapping("num1", "type=double") - ); - int docCount = 10; - for (int i = 1; i <= docCount; i++) { - client().prepareIndex("test-index").setId("" + i) - .setSource("num1", i ) + try { + assertAcked( + prepareCreate("test-index").setMapping("num1", "type=double") + ); + int docCount = 10; + for (int i = 1; i <= docCount; i++) { + client().prepareIndex("test-index").setId("" + i) + .setSource("num1", i) + .get(); + } + refresh(); + + // Execute with search.allow_expensive_queries = null => default value = false => success + Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['num1'].value > 1", + Collections.emptyMap()); + SearchResponse resp = client().prepareSearch("test-index") + .setQuery(scriptQuery(script)) + .get(); + assertNoFailures(resp); + + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + // Set search.allow_expensive_queries to "false" => assert failure + ElasticsearchException e = expectThrows(ElasticsearchException.class, + () -> client() + .prepareSearch("test-index") + .setQuery(scriptQuery(script)) + .get()); + assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", + e.getCause().getMessage()); + + // Set search.allow_expensive_queries to "true" => success + updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + resp = client().prepareSearch("test-index") + .setQuery(scriptQuery(script)) .get(); + assertNoFailures(resp); + } finally { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } - refresh(); - - // Execute with search.allow_expensive_queries = null => default value = false => success - Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['num1'].value > 1", - Collections.emptyMap()); - SearchResponse resp = client().prepareSearch("test-index") - .setQuery(scriptQuery(script)) - .get(); - assertNoFailures(resp); - - ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", false)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - - // Set search.allow_expensive_queries to "false" => assert failure - ElasticsearchException e = expectThrows(ElasticsearchException.class, - () -> client() - .prepareSearch("test-index") - .setQuery(scriptQuery(script)) - .get()); - assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", - e.getCause().getMessage()); - - // Set search.allow_expensive_queries to "true" => success - updateSettingsRequest = new ClusterUpdateSettingsRequest(); - updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); - assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - resp = client().prepareSearch("test-index") - .setQuery(scriptQuery(script)) - .get(); - assertNoFailures(resp); } private static AtomicInteger scriptCounter = new AtomicInteger(0); From 14ef948c0fb6a7edf82241fe3e6da929e84576eb Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 14:12:36 +0100 Subject: [PATCH 08/25] capitalize the first letter of error messages --- .../mapper/SearchAsYouTypeFieldTypeTests.java | 2 +- .../join/query/HasChildQueryBuilder.java | 4 ++-- .../join/query/HasParentQueryBuilder.java | 4 ++-- .../join/query/ParentIdQueryBuilder.java | 4 ++-- .../join/query/HasChildQueryBuilderTests.java | 2 +- .../join/query/HasParentQueryBuilderTests.java | 2 +- .../join/query/ParentIdQueryBuilderTests.java | 2 +- .../rest-api-spec/test/11_parent_child.yml | 2 +- .../rest-api-spec/test/20_parent_join.yml | 2 +- .../rest-api-spec/test/30_inner_hits.yml | 2 +- .../percolator/PercolateQueryBuilder.java | 2 +- .../percolator/PercolateQueryBuilderTests.java | 2 +- .../percolator/PercolatorQuerySearchIT.java | 2 +- .../index/mapper/StringFieldType.java | 16 ++++++++-------- .../query/LegacyGeoShapeQueryProcessor.java | 2 +- .../index/query/NestedQueryBuilder.java | 4 ++-- .../index/query/ScriptQueryBuilder.java | 4 ++-- .../functionscore/ScriptScoreQueryBuilder.java | 2 +- .../index/mapper/IgnoredFieldTypeTests.java | 6 +++--- .../index/mapper/KeywordFieldTypeTests.java | 4 ++-- .../mapper/LegacyGeoShapeFieldMapperTests.java | 2 +- .../index/mapper/RoutingFieldTypeTests.java | 6 +++--- .../index/mapper/TextFieldTypeTests.java | 6 +++--- .../index/query/NestedQueryBuilderTests.java | 2 +- .../index/query/ScriptQueryBuilderTests.java | 2 +- .../query/ScriptScoreQueryBuilderTests.java | 2 +- .../search/geo/LegacyGeoShapeIntegrationIT.java | 2 +- .../search/query/ScriptScoreQueryIT.java | 2 +- .../search/scriptfilter/ScriptQuerySearchIT.java | 2 +- .../mapper/KeyedFlatObjectFieldTypeTests.java | 2 +- .../mapper/RootFlatObjectFieldTypeTests.java | 6 +++--- 31 files changed, 52 insertions(+), 52 deletions(-) diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index 144221d6013dd..63e0ee27a9f4e 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -113,7 +113,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index 0efd25ca1a657..aed8ea9f70b82 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -299,8 +299,8 @@ public String getWriteableName() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Joining queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java index d05fb8ea18021..0f18f769742db 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java @@ -162,8 +162,8 @@ public boolean ignoreUnmapped() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Joining queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java index a13c3912d1796..88d8eda50d514 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java @@ -157,8 +157,8 @@ public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IO @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Joining queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index 647bb2c061291..8c83658e98ea1 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -374,7 +374,7 @@ public void testDisallowExpensiveQueries() { hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 5f8c791307f97..9541c5563595f 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -275,7 +275,7 @@ public void testDisallowExpensiveQueries() { CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index f81038845b9ff..05f4cd3ae25d1 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -164,7 +164,7 @@ public void testDisallowExpensiveQueries() { ParentIdQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml index 6839082cedc5a..c052749dcd9ba 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml @@ -83,6 +83,6 @@ teardown: - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: body: { "query": { "has_child": { "type": "child", "query": { "match_all": {} }, "inner_hits": {} } } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml index 4ac15897dbb33..6386dda5d3773 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml @@ -138,7 +138,7 @@ teardown: - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: body: sort: [ "id" ] diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml index 12493b1fc1c11..5c74bad0ad520 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml @@ -102,7 +102,7 @@ teardown: - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index 52410d5099e0a..ed39ad706730b 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -494,7 +494,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("percolate queries cannot be executed when '" + + throw new ElasticsearchException("Percolate queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java index df33fc89fb52a..c8cd158dd8320 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java @@ -351,7 +351,7 @@ public void testDisallowExpensiveQueries() { PercolateQueryBuilder queryBuilder = doCreateTestQueryBuilder(true); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index fb6c4f1dfdeb8..11fb284cbca3b 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -916,7 +916,7 @@ public void testDisallowExpensiveQueries() throws IOException { () -> client().prepareSearch() .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) .get()); - assertEquals("percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); // Set search.allow_expensive_queries setting to "true" ==> success diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index 17d2a57e01824..16c341e8af11d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -67,8 +67,8 @@ public Query termsQuery(List values, QueryShardContext context) { public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("fuzzy queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Fuzzy queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } failIfNotIndexed(); return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)), @@ -78,8 +78,8 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int @Override public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("prefix queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Prefix queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); @@ -97,8 +97,8 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu } if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("wildcard queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Wildcard queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } Term term = MappedFieldType.extractTerm(termQuery); @@ -111,8 +111,8 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu public Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("regexp queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Regexp queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } failIfNotIndexed(); RegexpQuery query = new RegexpQuery(new Term(name(), indexedValueForSearch(value)), flags, maxDeterminizedStates); diff --git a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java index 7af0b7b90a47e..27a820072484f 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -78,7 +78,7 @@ public Query process(Geometry shape, String fieldName, ShapeRelation relation, Q @Override public Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("geo shape queries on PrefixTree geo shapes cannot be executed when '" + throw new ElasticsearchException("Geo-shape queries on PrefixTree geo shapes cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } diff --git a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index e85b722c5c960..811f5014863c1 100644 --- a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -269,8 +269,8 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("joining queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Joining queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } ObjectMapper nestedObjectMapper = context.getObjectMapper(path); diff --git a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 857c9f0129de7..3304cbe0c09cc 100644 --- a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -134,8 +134,8 @@ public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOEx @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("script queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + - "' is set to false"); + throw new ElasticsearchException("Script queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } FilterScript.Factory factory = context.compile(script, FilterScript.CONTEXT); FilterScript.LeafFactory filterScript = factory.newFactory(script.getParams(), context.lookup()); diff --git a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java index a0158f2c59ae1..0cb68104846e0 100644 --- a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java @@ -173,7 +173,7 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("script score queries cannot be executed when '" + throw new ElasticsearchException("Script score queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); } ScoreScript.Factory factory = context.compile(script, ScoreScript.CONTEXT); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index 8c49fe8490e69..ded2138d62608 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -45,7 +45,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -59,7 +59,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -73,7 +73,7 @@ public void testWildcardQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java index d0267b791d955..63675d672c4f0 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java @@ -165,7 +165,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -184,7 +184,7 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java index 330b4c192d4a0..2cb7f7ad7014b 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java @@ -722,7 +722,7 @@ public void testDisallowExpensiveQueries() throws IOException { ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> geoShapeFieldMapper.fieldType().geometryQueryBuilder().process( new Point(-10, 10), "location", SpatialStrategy.TERM, ShapeRelation.INTERSECTS, queryShardContext)); - assertEquals("geo shape queries on PrefixTree geo shapes cannot be executed when " + + assertEquals("Geo-shape queries on PrefixTree geo shapes cannot be executed when " + "'search.allow_expensive_queries' is set to false", e.getMessage()); assertFieldWarnings("tree"); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index 8a628cc1f9e7c..400181fafbee2 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -44,7 +44,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -58,7 +58,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -72,7 +72,7 @@ public void testWildcardQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index 8b46a022f512c..a456843ea038b 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -146,7 +146,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -165,7 +165,7 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -182,7 +182,7 @@ public void testIndexPrefixes() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, randomMockShardContext()); diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 3410cd44ade1f..2bd4056455243 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -365,7 +365,7 @@ public void testDisallowExpensiveQueries() { NestedQueryBuilder queryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index 6241c50b3e1bc..7f9ff8e0fd20e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -139,7 +139,7 @@ public void testDisallowExpensiveQueries() { ScriptQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Script queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java index 2075424c2f90c..4de89c6ba9538 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java @@ -106,7 +106,7 @@ public void testDisallowExpensiveQueries() { ScriptScoreQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java b/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java index 401e9541f65ec..796dc3dfd1461 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java +++ b/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java @@ -221,7 +221,7 @@ public void testDisallowExpensiveQueries() throws InterruptedException, IOExcept ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get()); - assertEquals("geo shape queries on PrefixTree geo shapes cannot be executed when " + + assertEquals("Geo-shape queries on PrefixTree geo shapes cannot be executed when " + "'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); // Set search.allow_expensive_queries to "true" => success diff --git a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java index f8e4f9c7e69f4..0fe2710270bd3 100644 --- a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java @@ -189,7 +189,7 @@ public void testDisallowExpensiveQueries() { .prepareSearch("test-index") .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) .get()); - assertEquals("script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); // Set search.allow_expensive_queries to "true" => success diff --git a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java index eaf31285ffc97..e9650228ab73a 100644 --- a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java @@ -256,7 +256,7 @@ public void testDisallowExpensiveQueries() { .prepareSearch("test-index") .setQuery(scriptQuery(script)) .get()); - assertEquals("script queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Script queries cannot be executed when 'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); // Set search.allow_expensive_queries to "true" => success diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index 78ac8f181fe60..2ccbad176725f 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -103,7 +103,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java index 6a658e50bed43..ee2ecf2ba4e4f 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java @@ -85,7 +85,7 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("value", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -114,7 +114,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("val.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } @@ -127,7 +127,7 @@ public void testWildcardQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("Wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", ee.getMessage()); } } From 77a0eff17390ec5c82046754c4c00dbc3ea2b437 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 14:13:07 +0100 Subject: [PATCH 09/25] Add range queries on text/keyword to the expensive ones --- docs/reference/query-dsl/range-query.asciidoc | 5 + .../test/search/320_disallow_queries.yml | 91 +++++++++++++++++-- .../index/mapper/StringFieldType.java | 4 + .../index/mapper/KeywordFieldTypeTests.java | 15 +++ .../index/mapper/TextFieldTypeTests.java | 15 +++ 5 files changed, 121 insertions(+), 9 deletions(-) diff --git a/docs/reference/query-dsl/range-query.asciidoc b/docs/reference/query-dsl/range-query.asciidoc index 73fda308a0b29..ea7e7e9529435 100644 --- a/docs/reference/query-dsl/range-query.asciidoc +++ b/docs/reference/query-dsl/range-query.asciidoc @@ -134,6 +134,11 @@ increases the relevance score. [[range-query-notes]] ==== Notes +[[ranges-on-text-and-keyword]] +===== Using the `range` query with `text` and `keyword` fields +Range queries on <> or <> files will not be executed if +<> is set to false. + [[ranges-on-dates]] ===== Using the `range` query with `date` fields diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml index 39a1203e24190..4598df4e56440 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -6,13 +6,16 @@ setup: - do: indices.create: - index: test + index: test body: mappings: properties: text: type: text analyzer: standard + fields: + raw: + type: keyword nested1: type: nested @@ -103,10 +106,34 @@ teardown: - match: { hits.total.value: 3 } + ### Range on text + - do: + search: + index: test + body: + query: + range: + text: + gte: "theres" + + - match: { hits.total.value: 2 } + + ### Range on keyword + - do: + search: + index: test + body: + query: + range: + text.raw: + gte : "Outside it is cold and wet" + + - match: { hits.total.value: 2 } + ### Nested - do: search: - index: test + index: test body: query: nested: @@ -129,7 +156,7 @@ teardown: ### Prefix - do: - catch: /prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -140,7 +167,7 @@ teardown: ### Fuzzy - do: - catch: /fuzzy queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Fuzzy queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -151,7 +178,7 @@ teardown: ### Regexp - do: - catch: /regexp queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Regexp queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -162,7 +189,7 @@ teardown: ### Wildcard - do: - catch: /wildcard queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Wildcard queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: index: test body: @@ -171,11 +198,33 @@ teardown: text: value: out?ide + ### Range on text + - do: + catch: /Range queries on text or keyword fields cannot be executed when \'search.allow_expensive_queries\' is set to false/ + search: + index: test + body: + query: + range: + text: + gte: "theres" + + ### Range on keyword + - do: + catch: /Range queries on text or keyword fields cannot be executed when \'search.allow_expensive_queries\' is set to false/ + search: + index: test + body: + query: + range: + text.raw: + gte : "Outside it is cold and wet" + ### Nested - do: - catch: /joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ search: - index: test + index: test body: query: nested: @@ -242,10 +291,34 @@ teardown: - match: { hits.total.value: 3 } + ### Range on text + - do: + search: + index: test + body: + query: + range: + text: + gte: "theres" + + - match: { hits.total.value: 2 } + + ### Range on keyword + - do: + search: + index: test + body: + query: + range: + text.raw: + gte: "Outside it is cold and wet" + + - match: { hits.total.value: 2 } + ### Nested - do: search: - index: test + index: test body: query: nested: diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index 16c341e8af11d..cc9b4ed463283 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -124,6 +124,10 @@ public Query regexpQuery(String value, int flags, int maxDeterminizedStates, @Override public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { + if (context.isAllowExpensiveQueries() == false) { + throw new ElasticsearchException("Range queries on text or keyword fields cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + } failIfNotIndexed(); return new TermRangeQuery(name(), lowerTerm == null ? null : indexedValueForSearch(lowerTerm), diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java index 63675d672c4f0..41516d1ca630d 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java @@ -33,8 +33,10 @@ import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.analysis.AnalyzerScope; @@ -151,6 +153,19 @@ public void testExistsQuery() { assertEquals(new TermQuery(new Term(FieldNamesFieldMapper.NAME, "field")), ft.existsQuery(null)); } + public void testRangeQuery() { + MappedFieldType ft = createDefaultFieldType(); + ft.setName("field"); + ft.setIndexOptions(IndexOptions.DOCS); + assertEquals(new TermRangeQuery("field", BytesRefs.toBytesRef("foo"), BytesRefs.toBytesRef("bar"), true, false), + ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + + "is set to false", ee.getMessage()); + } + public void testRegexpQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index a456843ea038b..014e89a4b3ecc 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -30,11 +30,13 @@ import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.TermInSetQuery; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.automaton.Automata; import org.apache.lucene.util.automaton.Automaton; import org.apache.lucene.util.automaton.Operations; import org.elasticsearch.ElasticsearchException; +import org.elasticsearch.common.lucene.BytesRefs; import org.elasticsearch.common.unit.Fuzziness; import org.junit.Before; @@ -132,6 +134,19 @@ public void testTermsQuery() { assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } + public void testRangeQuery() { + MappedFieldType ft = createDefaultFieldType(); + ft.setName("field"); + ft.setIndexOptions(IndexOptions.DOCS); + assertEquals(new TermRangeQuery("field", BytesRefs.toBytesRef("foo"), BytesRefs.toBytesRef("bar"), true, false), + ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + + "is set to false", ee.getMessage()); + } + public void testRegexpQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); From 2b4d45a463fece5c82da90db116309cfb924bfe3 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 14:20:33 +0100 Subject: [PATCH 10/25] Add index_prefixes suggestion to the error message --- docs/reference/query-dsl/prefix-query.asciidoc | 2 +- .../index/mapper/SearchAsYouTypeFieldTypeTests.java | 4 ++-- .../rest-api-spec/test/search/320_disallow_queries.yml | 2 +- .../java/org/elasticsearch/index/mapper/StringFieldType.java | 3 ++- .../org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java | 4 ++-- .../org/elasticsearch/index/mapper/RoutingFieldTypeTests.java | 4 ++-- .../org/elasticsearch/index/mapper/TextFieldTypeTests.java | 4 ++-- .../xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java | 4 ++-- 8 files changed, 14 insertions(+), 13 deletions(-) diff --git a/docs/reference/query-dsl/prefix-query.asciidoc b/docs/reference/query-dsl/prefix-query.asciidoc index cd5f2e624b4a4..5d2015b223ba7 100644 --- a/docs/reference/query-dsl/prefix-query.asciidoc +++ b/docs/reference/query-dsl/prefix-query.asciidoc @@ -69,7 +69,7 @@ efficiently at the cost of a larger index. [[prefix-query-allow-expensive-queries]] ===== Allow expensive queries Prefix queries will not be executed if <> -is set to false. If <> are enabled though, an optimised query is +is set to false. If <> are enabled though, an optimised query is built, which is not considered slow and is executed despite of the fact that the <> set to false. diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index 63e0ee27a9f4e..237408175d08f 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -113,7 +113,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml index 4598df4e56440..9a19ddbe81b61 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -156,7 +156,7 @@ teardown: ### Prefix - do: - catch: /Prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /Prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false. For optimised prefix queries on text fields please enable [index_prefixes]/ search: index: test body: diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index cc9b4ed463283..38c02608e215a 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -79,7 +79,8 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { throw new ElasticsearchException("Prefix queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false. For optimised prefix queries on text fields" + + "please enable [index_prefixes]"); } failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index ded2138d62608..b97a202fef1a8 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -59,8 +59,8 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } public void testWildcardQuery() { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index 400181fafbee2..8ff9cab09fe58 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -58,8 +58,8 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false" + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } public void testWildcardQuery() { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index 014e89a4b3ecc..d5c2bf00b3599 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -197,8 +197,8 @@ public void testIndexPrefixes() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, randomMockShardContext()); Automaton automaton diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index 2ccbad176725f..a859247bcc0ee 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -103,8 +103,8 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } public void testFuzzyQuery() { From f8e24e8d74d946d3b0e1c6174eedc7d11b269f7c Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 14:34:35 +0100 Subject: [PATCH 11/25] fix error msg --- .../java/org/elasticsearch/index/mapper/StringFieldType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index 38c02608e215a..856c0a44ef384 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -79,8 +79,8 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { throw new ElasticsearchException("Prefix queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false. For optimised prefix queries on text fields" + - "please enable [index_prefixes]"); + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false. For optimised prefix queries on text " + + "fields please enable [index_prefixes]"); } failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); From 0587709f24425d5d1115fd1c54e4f1d3fa5da3d6 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 15:03:08 +0100 Subject: [PATCH 12/25] fix assertions of error messages --- .../index/mapper/SearchAsYouTypeFieldTypeTests.java | 2 +- .../rest-api-spec/test/search/320_disallow_queries.yml | 2 +- .../elasticsearch/index/mapper/IgnoredFieldTypeTests.java | 8 ++++---- .../elasticsearch/index/mapper/RoutingFieldTypeTests.java | 8 ++++---- .../elasticsearch/index/mapper/TextFieldTypeTests.java | 2 +- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index 237408175d08f..e93431b89e6fc 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -113,7 +113,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml index 9a19ddbe81b61..63f006b392218 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -156,7 +156,7 @@ teardown: ### Prefix - do: - catch: /Prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false. For optimised prefix queries on text fields please enable [index_prefixes]/ + catch: /Prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false. For optimised prefix queries on text fields please enable \[index_prefixes\]/ search: index: test body: diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index b97a202fef1a8..289228f8096fc 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -45,8 +45,8 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } public void testRegexpQuery() { @@ -59,8 +59,8 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false." + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + ee.getMessage()); } public void testWildcardQuery() { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index 8ff9cab09fe58..7ed4e97018f46 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -44,8 +44,8 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false", - ee.getMessage()); + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } public void testRegexpQuery() { @@ -58,8 +58,8 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false" + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + ee.getMessage()); } public void testWildcardQuery() { diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index d5c2bf00b3599..df57873b27d15 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -197,7 +197,7 @@ public void testIndexPrefixes() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, randomMockShardContext()); From aaaf2195792a2977ba54e7c571239cb2d25ae462 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 15:18:35 +0100 Subject: [PATCH 13/25] Enhance part of allow expensive in docs Query DSL --- docs/reference/query-dsl.asciidoc | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/docs/reference/query-dsl.asciidoc b/docs/reference/query-dsl.asciidoc index bb80bfd4d9337..86181d3960870 100644 --- a/docs/reference/query-dsl.asciidoc +++ b/docs/reference/query-dsl.asciidoc @@ -27,10 +27,23 @@ Query clauses behave differently depending on whether they are used in <>. [[query-dsl-allow-expensive-queries]] -== Allow expensive queries -Certain queries like <>, <>, -<> and <> , -that are usually slow performance can affect the cluster performance. +Allow expensive queries:: +Execution of certain types of queries have usually slow performance, which can affect the cluster performance. +Those queries can be categorised as follows: +* Queries that need to do linear scans to identify matches: +** <> +** <> +* Queries that have a high up-front cost: +** <> +** <> +** <> without <> +** <> +** <> on <> and <> fields +* <> +* Queries on <> +* Queries that may have a high per-document cost: +** <> + The execution of such queries can be prevented by setting the value of the `search.allow_expensive_queries` setting to `false` (defaults to `true`). -- From b24ca0e6345266fe1332e656bb344300f216c757 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 15:30:40 +0100 Subject: [PATCH 14/25] fix tests --- .../index/mapper/CollationFieldTypeTests.java | 22 ++++++++++++++----- .../mapper/KeyedFlatObjectFieldTypeTests.java | 13 +++++++---- .../mapper/RootFlatObjectFieldTypeTests.java | 9 ++++++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java index df82de52b140b..c17241fcae11a 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java @@ -28,6 +28,7 @@ import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.util.BytesRef; +import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.mapper.ICUCollationKeywordFieldMapper.CollationFieldType; import org.elasticsearch.index.mapper.MappedFieldType.Relation; @@ -101,32 +102,36 @@ public void testRegexpQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); - expectThrows(UnsupportedOperationException.class, + UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.regexpQuery("foo.*", 0, 10, null, randomMockShardContext())); + assertEquals("[regexp] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testFuzzyQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); - expectThrows(UnsupportedOperationException.class, + UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.fuzzyQuery("foo", Fuzziness.fromEdits(2), 1, 50, true, randomMockShardContext())); + assertEquals("[fuzzy] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testPrefixQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); - expectThrows(UnsupportedOperationException.class, + UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.prefixQuery("prefix", null, randomMockShardContext())); + assertEquals("[prefix] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testWildcardQuery() { MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); ft.setIndexOptions(IndexOptions.DOCS); - expectThrows(UnsupportedOperationException.class, + UnsupportedOperationException e = expectThrows(UnsupportedOperationException.class, () -> ft.wildcardQuery("foo*", null, randomMockShardContext())); + assertEquals("[wildcard] queries are not supported on [icu_collation_keyword] fields.", e.getMessage()); } public void testRangeQuery() { @@ -143,11 +148,16 @@ public void testRangeQuery() { TermRangeQuery expected = new TermRangeQuery("field", new BytesRef(aKey.bytes, 0, aKey.size), new BytesRef(bKey.bytes, 0, bKey.size), false, false); - assertEquals(expected, ft.rangeQuery("a", "b", false, false, null, null, null, null)); + assertEquals(expected, ft.rangeQuery("a", "b", false, false, null, null, null, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.rangeQuery("a", "b", true, true, null, null, null, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + + "is set to false", ee.getMessage()); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> ft.rangeQuery("a", "b", false, false, null, null, null, null)); + () -> ft.rangeQuery("a", "b", false, false, null, null, null, MOCK_QSC)); assertEquals("Cannot search on field [field] since it is not indexed.", e.getMessage()); } } diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index a859247bcc0ee..7d8c10a79a444 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -103,7 +103,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false." + + assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } @@ -123,12 +123,12 @@ public void testRangeQuery() { TermRangeQuery expected = new TermRangeQuery("field", new BytesRef("key\0lower"), new BytesRef("key\0upper"), false, false); - assertEquals(expected, ft.rangeQuery("lower", "upper", false, false, null)); + assertEquals(expected, ft.rangeQuery("lower", "upper", false, false, MOCK_QSC)); expected = new TermRangeQuery("field", new BytesRef("key\0lower"), new BytesRef("key\0upper"), true, true); - assertEquals(expected, ft.rangeQuery("lower", "upper", true, true, null)); + assertEquals(expected, ft.rangeQuery("lower", "upper", true, true, MOCK_QSC)); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> ft.rangeQuery("lower", null, false, false, null)); @@ -136,9 +136,14 @@ public void testRangeQuery() { e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> - ft.rangeQuery(null, "upper", false, false, null)); + ft.rangeQuery(null, "upper", false, false, MOCK_QSC)); assertEquals("[range] queries on keyed [flattened] fields must include both an upper and a lower bound.", e.getMessage()); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.rangeQuery("lower", "upper", false, false, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + + "is set to false", ee.getMessage()); } public void testRegexpQuery() { diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java index ee2ecf2ba4e4f..2cb5bbd7d10a5 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java @@ -96,12 +96,17 @@ public void testRangeQuery() { TermRangeQuery expected = new TermRangeQuery("field", new BytesRef("lower"), new BytesRef("upper"), false, false); - assertEquals(expected, ft.rangeQuery("lower", "upper", false, false, null)); + assertEquals(expected, ft.rangeQuery("lower", "upper", false, false, MOCK_QSC)); expected = new TermRangeQuery("field", new BytesRef("lower"), new BytesRef("upper"), true, true); - assertEquals(expected, ft.rangeQuery("lower", "upper", true, true, null)); + assertEquals(expected, ft.rangeQuery("lower", "upper", true, true, MOCK_QSC)); + + ElasticsearchException ee = expectThrows(ElasticsearchException.class, + () -> ft.rangeQuery("lower", "upper", true, true, MOCK_QSC_DISALLOW_EXPENSIVE)); + assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + + "is set to false", ee.getMessage()); } public void testRegexpQuery() { From eab1b650b64d2e9e8146fb20cc347b9e390d8e68 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 16:07:43 +0100 Subject: [PATCH 15/25] improve error messages --- .../mapper/SearchAsYouTypeFieldTypeTests.java | 2 +- .../join/query/HasChildQueryBuilder.java | 4 ++-- .../join/query/HasParentQueryBuilder.java | 4 ++-- .../join/query/ParentIdQueryBuilder.java | 4 ++-- .../join/query/HasChildQueryBuilderTests.java | 2 +- .../query/HasParentQueryBuilderTests.java | 2 +- .../join/query/ParentIdQueryBuilderTests.java | 2 +- .../rest-api-spec/test/11_parent_child.yml | 2 +- .../rest-api-spec/test/20_parent_join.yml | 2 +- .../rest-api-spec/test/30_inner_hits.yml | 2 +- .../percolator/PercolateQueryBuilder.java | 4 ++-- .../PercolateQueryBuilderTests.java | 2 +- .../index/mapper/CollationFieldTypeTests.java | 4 ++-- .../test/search/320_disallow_queries.yml | 14 ++++++------- .../index/mapper/StringFieldType.java | 20 +++++++++---------- .../query/LegacyGeoShapeQueryProcessor.java | 4 ++-- .../index/query/NestedQueryBuilder.java | 4 ++-- .../index/query/ScriptQueryBuilder.java | 4 ++-- .../ScriptScoreQueryBuilder.java | 4 ++-- .../index/mapper/IgnoredFieldTypeTests.java | 8 ++++---- .../index/mapper/KeywordFieldTypeTests.java | 8 ++++---- .../LegacyGeoShapeFieldMapperTests.java | 4 ++-- .../index/mapper/RoutingFieldTypeTests.java | 8 ++++---- .../index/mapper/TextFieldTypeTests.java | 12 +++++------ .../index/query/NestedQueryBuilderTests.java | 2 +- .../index/query/ScriptQueryBuilderTests.java | 2 +- .../query/ScriptScoreQueryBuilderTests.java | 2 +- .../geo/LegacyGeoShapeIntegrationIT.java | 4 ++-- .../search/query/ScriptScoreQueryIT.java | 2 +- .../scriptfilter/ScriptQuerySearchIT.java | 2 +- .../mapper/KeyedFlatObjectFieldTypeTests.java | 8 ++++---- .../mapper/RootFlatObjectFieldTypeTests.java | 10 +++++----- 32 files changed, 79 insertions(+), 79 deletions(-) diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index e93431b89e6fc..d8c8706c9df43 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -113,7 +113,7 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); } } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index aed8ea9f70b82..dcb0fcad86f1b 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -299,8 +299,8 @@ public String getWriteableName() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Joining queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[joining] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java index 0f18f769742db..85273a3d397d6 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java @@ -162,8 +162,8 @@ public boolean ignoreUnmapped() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Joining queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[joining] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java index 88d8eda50d514..3b6ab9c99ba52 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java @@ -157,8 +157,8 @@ public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IO @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Joining queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[joining] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } ParentJoinFieldMapper joinFieldMapper = ParentJoinFieldMapper.getMapper(context.getMapperService()); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index 8c83658e98ea1..f51ed448ad50d 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -374,7 +374,7 @@ public void testDisallowExpensiveQueries() { hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 9541c5563595f..0cd2de76559b5 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -275,7 +275,7 @@ public void testDisallowExpensiveQueries() { CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index 05f4cd3ae25d1..5322ff3e162f3 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -164,7 +164,7 @@ public void testDisallowExpensiveQueries() { ParentIdQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml index c052749dcd9ba..51ca76481cb75 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/11_parent_child.yml @@ -83,6 +83,6 @@ teardown: - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[joining\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: body: { "query": { "has_child": { "type": "child", "query": { "match_all": {} }, "inner_hits": {} } } } diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml index 6386dda5d3773..5798c6f7f2252 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/20_parent_join.yml @@ -138,7 +138,7 @@ teardown: - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[joining\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: body: sort: [ "id" ] diff --git a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml index 5c74bad0ad520..53d8a4d8a2a40 100644 --- a/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml +++ b/modules/parent-join/src/test/resources/rest-api-spec/test/30_inner_hits.yml @@ -102,7 +102,7 @@ teardown: - match: {transient: {search.allow_expensive_queries: "false"}} - do: - catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[joining\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index ed39ad706730b..f886ef1e9fe81 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -494,8 +494,8 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Percolate queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[percolate] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } // Call nowInMillis() so that this query becomes un-cacheable since we diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java index c8cd158dd8320..25c78febdfd3e 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java @@ -351,7 +351,7 @@ public void testDisallowExpensiveQueries() { PercolateQueryBuilder queryBuilder = doCreateTestQueryBuilder(true); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java index c17241fcae11a..c44ead42557ae 100644 --- a/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java +++ b/plugins/analysis-icu/src/test/java/org/elasticsearch/index/mapper/CollationFieldTypeTests.java @@ -152,8 +152,8 @@ public void testRangeQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.rangeQuery("a", "b", true, true, null, null, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + - "is set to false", ee.getMessage()); + assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", ee.getMessage()); ft.setIndexOptions(IndexOptions.NONE); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml index 63f006b392218..ceb8bdeb1115c 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/search/320_disallow_queries.yml @@ -156,7 +156,7 @@ teardown: ### Prefix - do: - catch: /Prefix queries cannot be executed when \'search.allow_expensive_queries\' is set to false. For optimised prefix queries on text fields please enable \[index_prefixes\]/ + catch: /\[prefix\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false. For optimised prefix queries on text fields please enable \[index_prefixes\]./ search: index: test body: @@ -167,7 +167,7 @@ teardown: ### Fuzzy - do: - catch: /Fuzzy queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[fuzzy\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: @@ -178,7 +178,7 @@ teardown: ### Regexp - do: - catch: /Regexp queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[regexp\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: @@ -189,7 +189,7 @@ teardown: ### Wildcard - do: - catch: /Wildcard queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[wildcard\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: @@ -200,7 +200,7 @@ teardown: ### Range on text - do: - catch: /Range queries on text or keyword fields cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[range\] queries on \[text\] or \[keyword\] fields cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: @@ -211,7 +211,7 @@ teardown: ### Range on keyword - do: - catch: /Range queries on text or keyword fields cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[range\] queries on \[text\] or \[keyword\] fields cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: @@ -222,7 +222,7 @@ teardown: ### Nested - do: - catch: /Joining queries cannot be executed when \'search.allow_expensive_queries\' is set to false/ + catch: /\[joining\] queries cannot be executed when \'search.allow_expensive_queries\' is set to false./ search: index: test body: diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index 856c0a44ef384..194e4314a7061 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -67,8 +67,8 @@ public Query termsQuery(List values, QueryShardContext context) { public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Fuzzy queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[fuzzy] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } failIfNotIndexed(); return new FuzzyQuery(new Term(name(), indexedValueForSearch(value)), @@ -78,9 +78,9 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int @Override public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Prefix queries cannot be executed when '" + + throw new ElasticsearchException("[prefix] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false. For optimised prefix queries on text " + - "fields please enable [index_prefixes]"); + "fields please enable [index_prefixes]."); } failIfNotIndexed(); PrefixQuery query = new PrefixQuery(new Term(name(), indexedValueForSearch(value))); @@ -98,8 +98,8 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu } if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Wildcard queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[wildcard] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } Term term = MappedFieldType.extractTerm(termQuery); @@ -112,8 +112,8 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu public Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Regexp queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[regexp] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } failIfNotIndexed(); RegexpQuery query = new RegexpQuery(new Term(name(), indexedValueForSearch(value)), flags, maxDeterminizedStates); @@ -126,8 +126,8 @@ public Query regexpQuery(String value, int flags, int maxDeterminizedStates, @Override public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Range queries on text or keyword fields cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[range] queries on [text] or [keyword] fields cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } failIfNotIndexed(); return new TermRangeQuery(name(), diff --git a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java index 27a820072484f..7b1abe71c241e 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -78,8 +78,8 @@ public Query process(Geometry shape, String fieldName, ShapeRelation relation, Q @Override public Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context) { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Geo-shape queries on PrefixTree geo shapes cannot be executed when '" - + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[geo-shape] queries on [PrefixTree geo shapes] cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } LegacyGeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (LegacyGeoShapeFieldMapper.GeoShapeFieldType) ft; diff --git a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index 811f5014863c1..00674fce87605 100644 --- a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -269,8 +269,8 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Joining queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[joining] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } ObjectMapper nestedObjectMapper = context.getObjectMapper(path); diff --git a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 3304cbe0c09cc..990a3841493f7 100644 --- a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -134,8 +134,8 @@ public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOEx @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Script queries cannot be executed when '" + - ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[script] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } FilterScript.Factory factory = context.compile(script, FilterScript.CONTEXT); FilterScript.LeafFactory filterScript = factory.newFactory(script.getParams(), context.lookup()); diff --git a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java index 0cb68104846e0..56b905c12a7d8 100644 --- a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java @@ -173,8 +173,8 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { if (context.isAllowExpensiveQueries() == false) { - throw new ElasticsearchException("Script score queries cannot be executed when '" - + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false"); + throw new ElasticsearchException("[script score] queries cannot be executed when '" + + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } ScoreScript.Factory factory = context.compile(script, ScoreScript.CONTEXT); ScoreScript.LeafFactory scoreScriptFactory = factory.newFactory(script.getParams(), context.lookup()); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java index 289228f8096fc..662b2e331e49e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IgnoredFieldTypeTests.java @@ -45,8 +45,8 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes].", ee.getMessage()); } public void testRegexpQuery() { @@ -59,7 +59,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[regexp] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -73,7 +73,7 @@ public void testWildcardQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[wildcard] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java index 41516d1ca630d..b05c4c779cf6e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldTypeTests.java @@ -162,8 +162,8 @@ public void testRangeQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + - "is set to false", ee.getMessage()); + assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", ee.getMessage()); } public void testRegexpQuery() { @@ -180,7 +180,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[regexp] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -199,7 +199,7 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[fuzzy] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java index 2cb7f7ad7014b..ebe3fe64e3b75 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java @@ -722,8 +722,8 @@ public void testDisallowExpensiveQueries() throws IOException { ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> geoShapeFieldMapper.fieldType().geometryQueryBuilder().process( new Point(-10, 10), "location", SpatialStrategy.TERM, ShapeRelation.INTERSECTS, queryShardContext)); - assertEquals("Geo-shape queries on PrefixTree geo shapes cannot be executed when " + - "'search.allow_expensive_queries' is set to false", e.getMessage()); + assertEquals("[geo-shape] queries on [PrefixTree geo shapes] cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", e.getMessage()); assertFieldWarnings("tree"); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java index 7ed4e97018f46..8bb052efbd467 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RoutingFieldTypeTests.java @@ -44,8 +44,8 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("foo*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes].", ee.getMessage()); } public void testRegexpQuery() { @@ -58,7 +58,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo?", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[regexp] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -72,7 +72,7 @@ public void testWildcardQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[wildcard] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java index df57873b27d15..9b54dcda3100a 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/TextFieldTypeTests.java @@ -143,8 +143,8 @@ public void testRangeQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.rangeQuery("foo", "bar", true, false, null, null, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + - "is set to false", ee.getMessage()); + assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", ee.getMessage()); } public void testRegexpQuery() { @@ -161,7 +161,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("foo.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[regexp] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -180,7 +180,7 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("foo", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[fuzzy] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -197,8 +197,8 @@ public void testIndexPrefixes() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("internationalisatio", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes].", ee.getMessage()); q = ft.prefixQuery("g", CONSTANT_SCORE_REWRITE, randomMockShardContext()); Automaton automaton diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 2bd4056455243..0de7105a9b6ec 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -365,7 +365,7 @@ public void testDisallowExpensiveQueries() { NestedQueryBuilder queryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Joining queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[joining] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index 7f9ff8e0fd20e..d2f867f99728b 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -139,7 +139,7 @@ public void testDisallowExpensiveQueries() { ScriptQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Script queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[script] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java index 4de89c6ba9538..4176057f5fd78 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java @@ -106,7 +106,7 @@ public void testDisallowExpensiveQueries() { ScriptScoreQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> queryBuilder.toQuery(queryShardContext)); - assertEquals("Script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[script score] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getMessage()); } } diff --git a/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java b/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java index 796dc3dfd1461..484f9078d5e83 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java +++ b/server/src/test/java/org/elasticsearch/search/geo/LegacyGeoShapeIntegrationIT.java @@ -221,8 +221,8 @@ public void testDisallowExpensiveQueries() throws InterruptedException, IOExcept ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get()); - assertEquals("Geo-shape queries on PrefixTree geo shapes cannot be executed when " + - "'search.allow_expensive_queries' is set to false", e.getCause().getMessage()); + assertEquals("[geo-shape] queries on [PrefixTree geo shapes] cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", e.getCause().getMessage()); // Set search.allow_expensive_queries to "true" => success updateSettingsRequest = new ClusterUpdateSettingsRequest(); diff --git a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java index 0fe2710270bd3..65449430aa878 100644 --- a/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java @@ -189,7 +189,7 @@ public void testDisallowExpensiveQueries() { .prepareSearch("test-index") .setQuery(scriptScoreQuery(matchQuery("field1", "text0"), script)) .get()); - assertEquals("Script score queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[script score] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getCause().getMessage()); // Set search.allow_expensive_queries to "true" => success diff --git a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java index e9650228ab73a..baeed397204d5 100644 --- a/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/scriptfilter/ScriptQuerySearchIT.java @@ -256,7 +256,7 @@ public void testDisallowExpensiveQueries() { .prepareSearch("test-index") .setQuery(scriptQuery(script)) .get()); - assertEquals("Script queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[script] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getCause().getMessage()); // Set search.allow_expensive_queries to "true" => success diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java index 7d8c10a79a444..2a106d6a96c5f 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/KeyedFlatObjectFieldTypeTests.java @@ -103,8 +103,8 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.prefixQuery("val", MultiTermQuery.CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Prefix queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + + "For optimised prefix queries on text fields please enable [index_prefixes].", ee.getMessage()); } public void testFuzzyQuery() { @@ -142,8 +142,8 @@ public void testRangeQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.rangeQuery("lower", "upper", false, false, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + - "is set to false", ee.getMessage()); + assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", ee.getMessage()); } public void testRegexpQuery() { diff --git a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java index 2cb5bbd7d10a5..e0afaf007a5f9 100644 --- a/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java +++ b/x-pack/plugin/mapper-flattened/src/test/java/org/elasticsearch/xpack/flattened/mapper/RootFlatObjectFieldTypeTests.java @@ -85,7 +85,7 @@ public void testFuzzyQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.fuzzyQuery("value", Fuzziness.AUTO, randomInt(10) + 1, randomInt(10) + 1, randomBoolean(), MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Fuzzy queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[fuzzy] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -105,8 +105,8 @@ public void testRangeQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.rangeQuery("lower", "upper", true, true, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Range queries on text or keyword fields cannot be executed when 'search.allow_expensive_queries' " + - "is set to false", ee.getMessage()); + assertEquals("[range] queries on [text] or [keyword] fields cannot be executed when " + + "'search.allow_expensive_queries' is set to false.", ee.getMessage()); } public void testRegexpQuery() { @@ -119,7 +119,7 @@ public void testRegexpQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.regexpQuery("val.*", randomInt(10), randomInt(10) + 1, null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Regexp queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[regexp] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } @@ -132,7 +132,7 @@ public void testWildcardQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> ft.wildcardQuery("valu*", null, MOCK_QSC_DISALLOW_EXPENSIVE)); - assertEquals("Wildcard queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[wildcard] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", ee.getMessage()); } } From 51a8cdc0e6c920534e58b97b468343aa1b21ff91 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 16:31:29 +0100 Subject: [PATCH 16/25] fix test --- .../index/mapper/SearchAsYouTypeFieldTypeTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java index d8c8706c9df43..b9cdae13ec139 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/SearchAsYouTypeFieldTypeTests.java @@ -114,6 +114,6 @@ public void testPrefixQuery() { ElasticsearchException ee = expectThrows(ElasticsearchException.class, () -> fieldType.prefixQuery(longTerm, CONSTANT_SCORE_REWRITE, MOCK_QSC_DISALLOW_EXPENSIVE)); assertEquals("[prefix] queries cannot be executed when 'search.allow_expensive_queries' is set to false. " + - "For optimised prefix queries on text fields please enable [index_prefixes]", ee.getMessage()); + "For optimised prefix queries on text fields please enable [index_prefixes].", ee.getMessage()); } } From 7eccee2866746a30b839d4475e2c32311bdd7772 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 16:57:45 +0100 Subject: [PATCH 17/25] fix test --- .../org/elasticsearch/percolator/PercolatorQuerySearchIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index 11fb284cbca3b..f3d9fb0216fe9 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -916,7 +916,7 @@ public void testDisallowExpensiveQueries() throws IOException { () -> client().prepareSearch() .setQuery(new PercolateQueryBuilder("query", source, XContentType.JSON)) .get()); - assertEquals("Percolate queries cannot be executed when 'search.allow_expensive_queries' is set to false", + assertEquals("[percolate] queries cannot be executed when 'search.allow_expensive_queries' is set to false.", e.getCause().getMessage()); // Set search.allow_expensive_queries setting to "true" ==> success From 907207ffabc7b5b17ef35de6e85a545dab60b5ac Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 17:03:03 +0100 Subject: [PATCH 18/25] fix test --- .../elasticsearch/percolator/PercolatorQuerySearchIT.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index f3d9fb0216fe9..95d2dfefb1e6b 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -71,11 +71,6 @@ public class PercolatorQuerySearchIT extends ESIntegTestCase { - @After - public void resetSettings() { - - } - public void testPercolatorQuery() throws Exception { assertAcked(client().admin().indices().prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=keyword", "field2", "type=keyword", "query", "type=percolator") From 5aa1a80147e0359feffac41cf3eeac0efa388593 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 17:11:22 +0100 Subject: [PATCH 19/25] Check that allow_expensive_queries setting doesn't affect new geo-shape queries --- .../search/geo/GeoShapeIntegrationIT.java | 109 +++++++++++------- 1 file changed, 70 insertions(+), 39 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java b/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java index a9916734247ef..bcfe0a83f984f 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java +++ b/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java @@ -18,12 +18,14 @@ */ package org.elasticsearch.search.geo; +import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.routing.IndexShardRoutingTable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.geo.builders.PointBuilder; import org.elasticsearch.common.geo.builders.ShapeBuilder; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexService; @@ -31,6 +33,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.test.ESIntegTestCase; +import org.junit.Ignore; import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -41,6 +44,16 @@ public class GeoShapeIntegrationIT extends ESIntegTestCase { + @Override + protected Settings nodeSettings(int nodeOrdinal) { + return Settings.builder() + // Check that only geo-shape queries on legacy PrefixTree based + // geo shapes are disallowed. + .put("search.allow_expensive_queries", false) + .put(super.nodeSettings(nodeOrdinal)) + .build(); + } + /** * Test that orientation parameter correctly persists across cluster restart */ @@ -190,55 +203,17 @@ public void testIndexPolygonDateLine() throws Exception { " }\n" + " }"; - String mappingQuad = "{\n" + - " \"properties\": {\n" + - " \"shape\": {\n" + - " \"type\": \"geo_shape\",\n" + - " \"tree\": \"quadtree\"\n" + - " }\n" + - " }\n" + - " }"; - - // create index assertAcked(client().admin().indices().prepareCreate("vector").setMapping(mappingVector).get()); ensureGreen(); - assertAcked(client().admin().indices().prepareCreate("quad").setMapping(mappingQuad).get()); - ensureGreen(); - String source = "{\n" + " \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\""+ "}"; - indexRandom(true, client().prepareIndex("quad").setId("0").setSource(source, XContentType.JSON)); indexRandom(true, client().prepareIndex("vector").setId("0").setSource(source, XContentType.JSON)); - SearchResponse searchResponse = client().prepareSearch("quad").setQuery( - geoShapeQuery("shape", new PointBuilder(-179.75, 1)) - ).get(); - - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); - - searchResponse = client().prepareSearch("quad").setQuery( - geoShapeQuery("shape", new PointBuilder(90, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); - - searchResponse = client().prepareSearch("quad").setQuery( - geoShapeQuery("shape", new PointBuilder(-180, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); - searchResponse = client().prepareSearch("quad").setQuery( - geoShapeQuery("shape", new PointBuilder(180, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); - - searchResponse = client().prepareSearch("vector").setQuery( + SearchResponse searchResponse = client().prepareSearch("vector").setQuery( geoShapeQuery("shape", new PointBuilder(90, 1)) ).get(); @@ -263,6 +238,62 @@ public void testIndexPolygonDateLine() throws Exception { assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); } + public void testIndexPolygonDateLineOnLegacyQuadTrees() throws Exception { + try { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + + String mappingQuad = "{\n" + + " \"properties\": {\n" + + " \"shape\": {\n" + + " \"type\": \"geo_shape\",\n" + + " \"tree\": \"quadtree\"\n" + + " }\n" + + " }\n" + + " }"; + + + // create index + assertAcked(client().admin().indices().prepareCreate("quad").setMapping(mappingQuad).get()); + ensureGreen(); + + String source = "{\n" + + " \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\"" + + "}"; + + indexRandom(true, client().prepareIndex("quad").setId("0").setSource(source, XContentType.JSON)); + + SearchResponse searchResponse = client().prepareSearch("quad").setQuery( + geoShapeQuery("shape", new PointBuilder(-179.75, 1)) + ).get(); + + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + + searchResponse = client().prepareSearch("quad").setQuery( + geoShapeQuery("shape", new PointBuilder(90, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); + + searchResponse = client().prepareSearch("quad").setQuery( + geoShapeQuery("shape", new PointBuilder(-180, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + searchResponse = client().prepareSearch("quad").setQuery( + geoShapeQuery("shape", new PointBuilder(180, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + } finally { + ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); + updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); + assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); + } + } + private String findNodeName(String index) { ClusterState state = client().admin().cluster().prepareState().get().getState(); IndexShardRoutingTable shard = state.getRoutingTable().index(index).shard(0); From ff9bb5ca1547ccb02f860c08843373c75e89e8cf Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 17:41:16 +0100 Subject: [PATCH 20/25] revert the legacy quad tree test to the same method --- .../search/geo/GeoShapeIntegrationIT.java | 103 ++++++++---------- 1 file changed, 47 insertions(+), 56 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java b/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java index bcfe0a83f984f..6e90da1e815f0 100644 --- a/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java +++ b/server/src/test/java/org/elasticsearch/search/geo/GeoShapeIntegrationIT.java @@ -33,7 +33,6 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.test.ESIntegTestCase; -import org.junit.Ignore; import static org.elasticsearch.index.query.QueryBuilders.geoShapeQuery; import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery; @@ -196,79 +195,46 @@ public void testIndexShapeRouting() throws Exception { public void testIndexPolygonDateLine() throws Exception { String mappingVector = "{\n" + - " \"properties\": {\n" + - " \"shape\": {\n" + - " \"type\": \"geo_shape\"\n" + - " }\n" + - " }\n" + - " }"; + " \"properties\": {\n" + + " \"shape\": {\n" + + " \"type\": \"geo_shape\"\n" + + " }\n" + + " }\n" + + " }"; + + String mappingQuad = "{\n" + + " \"properties\": {\n" + + " \"shape\": {\n" + + " \"type\": \"geo_shape\",\n" + + " \"tree\": \"quadtree\"\n" + + " }\n" + + " }\n" + + " }"; + // create index assertAcked(client().admin().indices().prepareCreate("vector").setMapping(mappingVector).get()); ensureGreen(); + assertAcked(client().admin().indices().prepareCreate("quad").setMapping(mappingQuad).get()); + ensureGreen(); + String source = "{\n" + - " \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\""+ - "}"; + " \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\""+ + "}"; + indexRandom(true, client().prepareIndex("quad").setId("0").setSource(source, XContentType.JSON)); indexRandom(true, client().prepareIndex("vector").setId("0").setSource(source, XContentType.JSON)); - SearchResponse searchResponse = client().prepareSearch("vector").setQuery( - geoShapeQuery("shape", new PointBuilder(90, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); - - searchResponse = client().prepareSearch("vector").setQuery( - geoShapeQuery("shape", new PointBuilder(-179.75, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); - - searchResponse = client().prepareSearch("vector").setQuery( - geoShapeQuery("shape", new PointBuilder(-180, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); - - searchResponse = client().prepareSearch("vector").setQuery( - geoShapeQuery("shape", new PointBuilder(180, 1)) - ).get(); - - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); - } - - public void testIndexPolygonDateLineOnLegacyQuadTrees() throws Exception { try { ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest(); updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", true)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); - String mappingQuad = "{\n" + - " \"properties\": {\n" + - " \"shape\": {\n" + - " \"type\": \"geo_shape\",\n" + - " \"tree\": \"quadtree\"\n" + - " }\n" + - " }\n" + - " }"; - - - // create index - assertAcked(client().admin().indices().prepareCreate("quad").setMapping(mappingQuad).get()); - ensureGreen(); - - String source = "{\n" + - " \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\"" + - "}"; - - indexRandom(true, client().prepareIndex("quad").setId("0").setSource(source, XContentType.JSON)); - SearchResponse searchResponse = client().prepareSearch("quad").setQuery( geoShapeQuery("shape", new PointBuilder(-179.75, 1)) ).get(); - assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); searchResponse = client().prepareSearch("quad").setQuery( @@ -282,6 +248,7 @@ public void testIndexPolygonDateLineOnLegacyQuadTrees() throws Exception { ).get(); assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + searchResponse = client().prepareSearch("quad").setQuery( geoShapeQuery("shape", new PointBuilder(180, 1)) ).get(); @@ -292,6 +259,30 @@ public void testIndexPolygonDateLineOnLegacyQuadTrees() throws Exception { updateSettingsRequest.persistentSettings(Settings.builder().put("search.allow_expensive_queries", (String) null)); assertAcked(client().admin().cluster().updateSettings(updateSettingsRequest).actionGet()); } + + SearchResponse searchResponse = client().prepareSearch("vector").setQuery( + geoShapeQuery("shape", new PointBuilder(90, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L)); + + searchResponse = client().prepareSearch("vector").setQuery( + geoShapeQuery("shape", new PointBuilder(-179.75, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + + searchResponse = client().prepareSearch("vector").setQuery( + geoShapeQuery("shape", new PointBuilder(-180, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); + + searchResponse = client().prepareSearch("vector").setQuery( + geoShapeQuery("shape", new PointBuilder(180, 1)) + ).get(); + + assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L)); } private String findNodeName(String index) { From d1c5e1c755a8696bbb601738b80cca9421161fa3 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 6 Feb 2020 19:14:48 +0100 Subject: [PATCH 21/25] remove unused import --- .../org/elasticsearch/percolator/PercolatorQuerySearchIT.java | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index 95d2dfefb1e6b..cbd319d342c94 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -39,7 +39,6 @@ import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.ESIntegTestCase; -import org.junit.After; import java.io.IOException; import java.util.Arrays; From 78e3c9e813a10cd8413071435b7f66e67627b29f Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Fri, 7 Feb 2020 22:10:58 +0100 Subject: [PATCH 22/25] Remove duplicate constructors to ease up tests --- .../org/elasticsearch/index/IndexModule.java | 9 ---- .../index/query/QueryShardContext.java | 21 --------- .../MetaDataCreateIndexServiceTests.java | 2 +- .../elasticsearch/index/IndexModuleTests.java | 46 +++++++++++-------- .../index/mapper/DateFieldTypeTests.java | 4 +- .../mapper/FieldNamesFieldTypeTests.java | 2 +- .../index/mapper/IndexFieldTypeTests.java | 2 +- .../index/mapper/RangeFieldTypeTests.java | 2 +- .../query/IntervalQueryBuilderTests.java | 2 +- .../index/query/QueryShardContextTests.java | 2 +- .../index/query/RangeQueryRewriteTests.java | 7 +-- .../bucket/histogram/ExtendedBoundsTests.java | 2 +- .../ScriptedMetricAggregatorTests.java | 2 +- .../highlight/HighlightBuilderTests.java | 2 +- .../rescore/QueryRescorerBuilderTests.java | 4 +- .../search/sort/AbstractSortTestCase.java | 2 +- .../AbstractSuggestionBuilderTestCase.java | 2 +- .../aggregations/AggregatorTestCase.java | 2 +- .../DocumentSubsetBitsetCacheTests.java | 4 +- ...ityIndexReaderWrapperIntegrationTests.java | 4 +- .../job/RollupIndexerIndexingTests.java | 2 +- 21 files changed, 52 insertions(+), 73 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/IndexModule.java b/server/src/main/java/org/elasticsearch/index/IndexModule.java index ee020595dbda5..e8bef9e48f465 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexModule.java +++ b/server/src/main/java/org/elasticsearch/index/IndexModule.java @@ -156,15 +156,6 @@ public IndexModule( this.isAllowExpensiveQueries = isAllowExpensiveQueries; } - // For testing - IndexModule( - final IndexSettings indexSettings, - final AnalysisRegistry analysisRegistry, - final EngineFactory engineFactory, - final Map directoryFactories) { - this(indexSettings, analysisRegistry, engineFactory, directoryFactories, () -> true); - } - /** * Adds a Setting and it's consumer for this index. */ diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 7f42219adb8b9..f886f9713d960 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -122,27 +122,6 @@ public QueryShardContext(int shardId, indexSettings.getIndex().getUUID()), isAllowExpensiveQueries); } - public QueryShardContext(int shardId, - IndexSettings indexSettings, - BigArrays bigArrays, - BitsetFilterCache bitsetFilterCache, - BiFunction> indexFieldDataLookup, - MapperService mapperService, - SimilarityService similarityService, - ScriptService scriptService, - NamedXContentRegistry xContentRegistry, - NamedWriteableRegistry namedWriteableRegistry, - Client client, - IndexSearcher searcher, - LongSupplier nowInMillis, - String clusterAlias, - Predicate indexNameMatcher) { - this(shardId, indexSettings, bigArrays, bitsetFilterCache, indexFieldDataLookup, mapperService, similarityService, - scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, indexNameMatcher, - new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), - indexSettings.getIndex().getUUID()), () -> true); - } - public QueryShardContext(QueryShardContext source) { this(source.shardId, source.indexSettings, source.bigArrays, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.getXContentRegistry(), diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java index b7f437e80af70..19a6c5cf12bdf 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexServiceTests.java @@ -128,7 +128,7 @@ public void setupCreateIndexRequestAndAliasValidator() { queryShardContext = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("test").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), - null, null, () -> randomNonNegativeLong(), null, null); + null, null, () -> randomNonNegativeLong(), null, null, () -> true); } private ClusterState createClusterState(String name, int numShards, int numReplicas, Settings settings) { diff --git a/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java b/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java index 2c11b26580f3d..7807f58c7fd6e 100644 --- a/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java +++ b/server/src/test/java/org/elasticsearch/index/IndexModuleTests.java @@ -166,7 +166,8 @@ private IndexService newIndexService(IndexModule module) throws IOException { public void testWrapperIsBound() throws IOException { final MockEngineFactory engineFactory = new MockEngineFactory(AssertingDirectoryReader.class); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, engineFactory, Collections.emptyMap()); + IndexModule module = new IndexModule( + indexSettings, emptyAnalysisRegistry, engineFactory, Collections.emptyMap(), () -> true); module.setReaderWrapper(s -> new Wrapper()); IndexService indexService = newIndexService(module); @@ -186,7 +187,8 @@ public void testRegisterIndexStore() throws IOException { final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); final Map indexStoreFactories = singletonMap( "foo_store", new FooFunction()); - final IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), indexStoreFactories); + final IndexModule module = new IndexModule( + indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), indexStoreFactories, () -> true); final IndexService indexService = newIndexService(module); assertThat(indexService.getDirectoryFactory(), instanceOf(FooFunction.class)); @@ -203,7 +205,7 @@ public void beforeIndexRemoved(IndexService indexService, IndexRemovalReason rea } }; IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); module.addIndexEventListener(eventListener); IndexService indexService = newIndexService(module); IndexSettings x = indexService.getIndexSettings(); @@ -218,7 +220,7 @@ public void beforeIndexRemoved(IndexService indexService, IndexRemovalReason rea public void testListener() throws IOException { Setting booleanSetting = Setting.boolSetting("index.foo.bar", false, Property.Dynamic, Property.IndexScope); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings, booleanSetting); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); Setting booleanSetting2 = Setting.boolSetting("index.foo.bar.baz", false, Property.Dynamic, Property.IndexScope); AtomicBoolean atomicBoolean = new AtomicBoolean(false); module.addSettingsUpdateConsumer(booleanSetting, atomicBoolean::set); @@ -238,7 +240,7 @@ public void testListener() throws IOException { public void testAddIndexOperationListener() throws IOException { final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); AtomicBoolean executed = new AtomicBoolean(false); IndexingOperationListener listener = new IndexingOperationListener() { @Override @@ -269,7 +271,7 @@ public Engine.Index preIndex(ShardId shardId, Engine.Index operation) { public void testAddSearchOperationListener() throws IOException { final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); AtomicBoolean executed = new AtomicBoolean(false); SearchOperationListener listener = new SearchOperationListener() { @@ -304,7 +306,7 @@ public void testAddSimilarity() throws IOException { .build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); IndexModule module = - new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + createIndexModule(indexSettings, emptyAnalysisRegistry); module.addSimilarity("test_similarity", (providerSettings, indexCreatedVersion, scriptService) -> new TestSimilarity(providerSettings.get("key"))); @@ -320,9 +322,11 @@ public void testAddSimilarity() throws IOException { indexService.close("simon says", false); } + + public void testFrozen() { final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(index, settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); module.freeze(); String msg = "Can't modify IndexModule once the index service has been created"; assertEquals(msg, expectThrows(IllegalStateException.class, () -> module.addSearchOperationListener(null)).getMessage()); @@ -333,7 +337,7 @@ public void testFrozen() { assertEquals(msg, expectThrows(IllegalStateException.class, () -> module.forceQueryCacheProvider(null)).getMessage()); } - public void testSetupUnknownSimilarity() throws IOException { + public void testSetupUnknownSimilarity() { Settings settings = Settings.builder() .put("index.similarity.my_similarity.type", "test_similarity") .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) @@ -341,19 +345,19 @@ public void testSetupUnknownSimilarity() throws IOException { .build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); IndexModule module = - new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + createIndexModule(indexSettings, emptyAnalysisRegistry); Exception ex = expectThrows(IllegalArgumentException.class, () -> newIndexService(module)); assertEquals("Unknown Similarity type [test_similarity] for [my_similarity]", ex.getMessage()); } - public void testSetupWithoutType() throws IOException { + public void testSetupWithoutType() { Settings settings = Settings.builder() .put("index.similarity.my_similarity.foo", "bar") .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT) .build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); Exception ex = expectThrows(IllegalArgumentException.class, () -> newIndexService(module)); assertEquals("Similarity [my_similarity] must have an associated type", ex.getMessage()); } @@ -363,7 +367,7 @@ public void testForceCustomQueryCache() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); final Set liveQueryCaches = new HashSet<>(); module.forceQueryCacheProvider((a, b) -> { final CustomQueryCache customQueryCache = new CustomQueryCache(liveQueryCaches); @@ -384,7 +388,7 @@ public void testDefaultQueryCacheImplIsSelected() throws IOException { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); IndexService indexService = newIndexService(module); assertTrue(indexService.cache().query() instanceof IndexQueryCache); indexService.close("simon says", false); @@ -396,7 +400,7 @@ public void testDisableQueryCacheHasPrecedenceOverForceQueryCache() throws IOExc .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); module.forceQueryCacheProvider((a, b) -> new CustomQueryCache(null)); IndexService indexService = newIndexService(module); assertTrue(indexService.cache().query() instanceof DisabledQueryCache); @@ -408,7 +412,7 @@ public void testCustomQueryCacheCleanedUpIfIndexServiceCreationFails() { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()) .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("foo", settings); - IndexModule module = new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, emptyAnalysisRegistry); final Set liveQueryCaches = new HashSet<>(); module.forceQueryCacheProvider((a, b) -> { final CustomQueryCache customQueryCache = new CustomQueryCache(liveQueryCaches); @@ -458,7 +462,7 @@ public void close() { }; final AnalysisRegistry analysisRegistry = new AnalysisRegistry(environment, emptyMap(), emptyMap(), emptyMap(), singletonMap("test", analysisProvider), emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()); - IndexModule module = new IndexModule(indexSettings, analysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + IndexModule module = createIndexModule(indexSettings, analysisRegistry); threadPool.shutdown(); // causes index service creation to fail expectThrows(EsRejectedExecutionException.class, () -> newIndexService(module)); assertThat(openAnalyzers, empty()); @@ -475,11 +479,16 @@ public void testMmapNotAllowed() { .build(); final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(new Index("foo", "_na_"), settings, nodeSettings); final IndexModule module = - new IndexModule(indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap()); + createIndexModule(indexSettings, emptyAnalysisRegistry); final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> newIndexService(module)); assertThat(e, hasToString(containsString("store type [" + storeType + "] is not allowed"))); } + private static IndexModule createIndexModule(IndexSettings indexSettings, AnalysisRegistry emptyAnalysisRegistry) { + return new IndexModule( + indexSettings, emptyAnalysisRegistry, new InternalEngineFactory(), Collections.emptyMap(), () -> true); + } + class CustomQueryCache implements QueryCache { private final Set liveQueryCaches; @@ -545,5 +554,4 @@ public DirectoryReader apply(DirectoryReader reader) { return null; } } - } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java index 6ac59169ad908..45d2aac7a41f2 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java @@ -179,7 +179,7 @@ public void testTermQuery() { QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, - xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null); + xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true); MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); String date = "2015-10-12T14:10:55"; @@ -202,7 +202,7 @@ public void testRangeQuery() throws IOException { QueryShardContext context = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), - null, null, () -> nowInMillis, null, null); + null, null, () -> nowInMillis, null, null, () -> true); MappedFieldType ft = createDefaultFieldType(); ft.setName("field"); String date1 = "2015-10-12T14:10:55"; diff --git a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java index 1a9460115f056..eaa3e533ca359 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldTypeTests.java @@ -68,7 +68,7 @@ public void testTermQuery() { QueryShardContext queryShardContext = new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, - null, null, null, null, null, null, () -> 0L, null, null); + null, null, null, null, null, null, () -> 0L, null, null, () -> true); fieldNamesFieldType.setEnabled(true); Query termQuery = fieldNamesFieldType.termQuery("field_name", queryShardContext); assertEquals(new TermQuery(new Term(FieldNamesFieldMapper.CONTENT_TYPE, "field_name")), termQuery); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java index 1b100fb0872ea..bf0a0dffba743 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/IndexFieldTypeTests.java @@ -78,6 +78,6 @@ private QueryShardContext createContext() { Predicate indexNameMatcher = pattern -> Regex.simpleMatch(pattern, "index"); return new QueryShardContext(0, indexSettings, null, null, null, null, null, null, xContentRegistry(), writableRegistry(), - null, null, System::currentTimeMillis, null, indexNameMatcher); + null, null, System::currentTimeMillis, null, indexNameMatcher, () -> true); } } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java index 893a909ece2c8..d1a7ff06d3276 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java @@ -230,7 +230,7 @@ private QueryShardContext createContext() { .put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build(); IndexSettings idxSettings = IndexSettingsModule.newIndexSettings(randomAlphaOfLengthBetween(1, 10), indexSettings); return new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, - xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null); + xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true); } public void testDateRangeQueryUsingMappingFormat() { diff --git a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java index 1e147e24def7a..8e736b12d6a1f 100644 --- a/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java @@ -425,7 +425,7 @@ public FactoryType compile(Script script, ScriptContext true); String json = "{ \"intervals\" : { \"" + STRING_FIELD_NAME + "\": { " + "\"match\" : { " + diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java index 8e57ad50bd1cd..ba68e1b6a2013 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java @@ -153,6 +153,6 @@ public static QueryShardContext createQueryShardContext(String indexUuid, String (mappedFieldType, idxName) -> mappedFieldType.fielddataBuilder(idxName).build(indexSettings, mappedFieldType, null, null, null), mapperService, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegistry(Collections.emptyList()), - null, null, () -> nowInMillis, clusterAlias, null); + null, null, () -> nowInMillis, clusterAlias, null, () -> true); } } diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java index 83ab9c8e62bb4..c43470ea3b21c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryRewriteTests.java @@ -41,7 +41,7 @@ public void testRewriteMissingField() throws Exception { IndexReader reader = new MultiReader(); QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), BigArrays.NON_RECYCLING_INSTANCE, null, null, indexService.mapperService(), null, null, xContentRegistry(), writableRegistry(), - null, new IndexSearcher(reader), null, null, null); + null, new IndexSearcher(reader), null, null, null, () -> true); RangeQueryBuilder range = new RangeQueryBuilder("foo"); assertEquals(Relation.DISJOINT, range.getRelation(context)); } @@ -58,7 +58,8 @@ public void testRewriteMissingReader() throws Exception { indexService.mapperService().merge("type", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE); QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), null, null, null, - indexService.mapperService(), null, null, xContentRegistry(), writableRegistry(), null, null, null, null, null); + indexService.mapperService(), null, null, xContentRegistry(), writableRegistry(), + null, null, null, null, null, () -> true); RangeQueryBuilder range = new RangeQueryBuilder("foo"); // can't make assumptions on a missing reader, so it must return INTERSECT assertEquals(Relation.INTERSECTS, range.getRelation(context)); @@ -78,7 +79,7 @@ public void testRewriteEmptyReader() throws Exception { IndexReader reader = new MultiReader(); QueryRewriteContext context = new QueryShardContext(0, indexService.getIndexSettings(), BigArrays.NON_RECYCLING_INSTANCE, null, null, indexService.mapperService(), null, null, xContentRegistry(), writableRegistry(), - null, new IndexSearcher(reader), null, null, null); + null, new IndexSearcher(reader), null, null, null, () -> true); RangeQueryBuilder range = new RangeQueryBuilder("foo"); // no values -> DISJOINT assertEquals(Relation.DISJOINT, range.getRelation(context)); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java index 6fc5561f6d6f7..4afce4e5ff247 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/ExtendedBoundsTests.java @@ -97,7 +97,7 @@ public void testParseAndValidate() { QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), writableRegistry(), - null, null, () -> now, null, null); + null, null, () -> now, null, null, () -> true); DateFormatter formatter = DateFormatter.forPattern("dateOptionalTime"); DocValueFormat format = new DocValueFormat.DateTime(formatter, ZoneOffset.UTC, DateFieldMapper.Resolution.MILLISECONDS); diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricAggregatorTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricAggregatorTests.java index 9d0d1d69f023c..4adf16f6012b9 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricAggregatorTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/metrics/ScriptedMetricAggregatorTests.java @@ -426,6 +426,6 @@ protected QueryShardContext queryShardContextMock(IndexSearcher searcher, Map engines = Collections.singletonMap(scriptEngine.getType(), scriptEngine); ScriptService scriptService = new ScriptService(Settings.EMPTY, engines, ScriptModule.CORE_CONTEXTS); return new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, scriptService, - xContentRegistry(), writableRegistry(), null, null, System::currentTimeMillis, null, null); + xContentRegistry(), writableRegistry(), null, null, System::currentTimeMillis, null, null, () -> true); } } diff --git a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java index 9a542fd762ad9..31a2de6b3d821 100644 --- a/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlightBuilderTests.java @@ -280,7 +280,7 @@ public void testBuildSearchContextHighlight() throws IOException { // shard context will only need indicesQueriesRegistry for building Query objects nested in highlighter QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, xContentRegistry(), namedWriteableRegistry, - null, null, System::currentTimeMillis, null, null) { + null, null, System::currentTimeMillis, null, null, () -> true) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); diff --git a/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java b/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java index e22daab2e89cf..341202d44a940 100644 --- a/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/rescore/QueryRescorerBuilderTests.java @@ -144,7 +144,7 @@ public void testBuildRescoreSearchContext() throws ElasticsearchParseException, // shard context will only need indicesQueriesRegistry for building Query objects nested in query rescorer QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, - xContentRegistry(), namedWriteableRegistry, null, null, () -> nowInMillis, null, null) { + xContentRegistry(), namedWriteableRegistry, null, null, () -> nowInMillis, null, null, () -> true) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); @@ -188,7 +188,7 @@ public void testRewritingKeepsSettings() throws IOException { // shard context will only need indicesQueriesRegistry for building Query objects nested in query rescorer QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, - xContentRegistry(), namedWriteableRegistry, null, null, () -> nowInMillis, null, null) { + xContentRegistry(), namedWriteableRegistry, null, null, () -> nowInMillis, null, null, () -> true) { @Override public MappedFieldType fieldMapper(String name) { TextFieldMapper.Builder builder = new TextFieldMapper.Builder(name); diff --git a/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java b/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java index 9cda381df243b..2ec4038529972 100644 --- a/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java +++ b/server/src/test/java/org/elasticsearch/search/sort/AbstractSortTestCase.java @@ -198,7 +198,7 @@ protected final QueryShardContext createMockShardContext(IndexSearcher searcher) }; return new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, bitsetFilterCache, indexFieldDataLookup, null, null, scriptService, xContentRegistry(), namedWriteableRegistry, null, searcher, - () -> randomNonNegativeLong(), null, null) { + () -> randomNonNegativeLong(), null, null, () -> true) { @Override public MappedFieldType fieldMapper(String name) { diff --git a/server/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java b/server/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java index 48936b4d201e3..64c11910c3a62 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/AbstractSuggestionBuilderTestCase.java @@ -181,7 +181,7 @@ public void testBuild() throws IOException { ((Script) invocation.getArguments()[0]).getIdOrCode())); QueryShardContext mockShardContext = new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, scriptService, xContentRegistry(), namedWriteableRegistry, null, null, - System::currentTimeMillis, null, null); + System::currentTimeMillis, null, null, () -> true); SuggestionContext suggestionContext = suggestionBuilder.build(mockShardContext); assertEquals(toBytesRef(suggestionBuilder.text()), suggestionContext.getText()); diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index c671b9c109017..16a1a22c6faf1 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -282,7 +282,7 @@ protected QueryShardContext queryShardContextMock(IndexSearcher searcher, return new QueryShardContext(0, indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, getIndexFieldDataLookup(mapperService, circuitBreakerService), mapperService, null, getMockScriptService(), xContentRegistry(), - writableRegistry(), null, searcher, System::currentTimeMillis, null, null); + writableRegistry(), null, searcher, System::currentTimeMillis, null, null, () -> true); } /** diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java index be83ab37c7628..1299728996eb3 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java @@ -24,8 +24,8 @@ import org.apache.lucene.util.BitSet; import org.elasticsearch.client.Client; import org.elasticsearch.common.CheckedBiConsumer; -import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.CheckedConsumer; +import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.index.IndexSettings; @@ -519,7 +519,7 @@ private TestIndexContext testIndex(MapperService mapperService, Client client) t final QueryShardContext shardContext = new QueryShardContext(shardId.id(), indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, null, xContentRegistry(), writableRegistry(), - client, new IndexSearcher(directoryReader), () -> nowInMillis, null, null); + client, new IndexSearcher(directoryReader), () -> nowInMillis, null, null, () -> true); context = new TestIndexContext(directory, iw, directoryReader, shardContext, leaf); return context; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java index 1f8ba70426366..d2b9844c5c2d9 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/SecurityIndexReaderWrapperIntegrationTests.java @@ -79,7 +79,7 @@ public void testDLS() throws Exception { final long nowInMillis = randomNonNegativeLong(); QueryShardContext realQueryShardContext = new QueryShardContext(shardId.id(), indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, null, xContentRegistry(), writableRegistry(), - client, null, () -> nowInMillis, null, null); + client, null, () -> nowInMillis, null, null, () -> true); QueryShardContext queryShardContext = spy(realQueryShardContext); DocumentSubsetBitsetCache bitsetCache = new DocumentSubsetBitsetCache(Settings.EMPTY, Executors.newSingleThreadExecutor()); XPackLicenseState licenseState = mock(XPackLicenseState.class); @@ -201,7 +201,7 @@ public void testDLSWithLimitedPermissions() throws Exception { final long nowInMillis = randomNonNegativeLong(); QueryShardContext realQueryShardContext = new QueryShardContext(shardId.id(), indexSettings, BigArrays.NON_RECYCLING_INSTANCE, null, null, mapperService, null, null, xContentRegistry(), writableRegistry(), - client, null, () -> nowInMillis, null, null); + client, null, () -> nowInMillis, null, null, () -> true); QueryShardContext queryShardContext = spy(realQueryShardContext); DocumentSubsetBitsetCache bitsetCache = new DocumentSubsetBitsetCache(Settings.EMPTY, Executors.newSingleThreadExecutor()); diff --git a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java index 6de6b66ab7156..baabbfd8d3e94 100644 --- a/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java +++ b/x-pack/plugin/rollup/src/test/java/org/elasticsearch/xpack/rollup/job/RollupIndexerIndexingTests.java @@ -91,7 +91,7 @@ private void setup() { settings = createIndexSettings(); queryShardContext = new QueryShardContext(0, settings, BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, - null, null, null, null, () -> 0L, null, null); + null, null, null, null, () -> 0L, null, null, () -> true); } public void testSimpleDateHisto() throws Exception { From 1c645025c953aaf1cc770992e040f2354331c650 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Fri, 7 Feb 2020 22:36:54 +0100 Subject: [PATCH 23/25] fix tests --- .../java/org/elasticsearch/test/AbstractBuilderTestCase.java | 2 +- .../java/org/elasticsearch/search/MockSearchServiceTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java index a682cf4ea490d..875bb775ba21f 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java @@ -413,7 +413,7 @@ public void close() throws IOException { QueryShardContext createShardContext(IndexSearcher searcher) { return new QueryShardContext(0, idxSettings, BigArrays.NON_RECYCLING_INSTANCE, bitsetFilterCache, indexFieldDataService::getForField, mapperService, similarityService, scriptService, xContentRegistry, - namedWriteableRegistry, this.client, searcher, () -> nowInMillis, null, indexNameMatcher()); + namedWriteableRegistry, this.client, searcher, () -> nowInMillis, null, indexNameMatcher(), () -> true); } ScriptModule createScriptModule(List scriptPlugins) { diff --git a/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java b/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java index 8a8842487f14a..684210f13c57f 100644 --- a/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java +++ b/test/framework/src/test/java/org/elasticsearch/search/MockSearchServiceTests.java @@ -43,7 +43,7 @@ public void testAssertNoInFlightContext() { final long nowInMillis = randomNonNegativeLong(); SearchContext s = new TestSearchContext(new QueryShardContext(0, new IndexSettings(EMPTY_INDEX_METADATA, Settings.EMPTY), BigArrays.NON_RECYCLING_INSTANCE, null, null, null, null, null, - xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null)) { + xContentRegistry(), writableRegistry(), null, null, () -> nowInMillis, null, null, () -> true)) { @Override public SearchShardTarget shardTarget() { From 623846b5a8b4be0800be0105caf7fac92d7c2186 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Mon, 10 Feb 2020 12:33:07 +0100 Subject: [PATCH 24/25] Address comments --- docs/reference/query-dsl.asciidoc | 4 ++-- docs/reference/query-dsl/prefix-query.asciidoc | 6 ++---- .../join/query/HasChildQueryBuilder.java | 2 +- .../join/query/HasParentQueryBuilder.java | 2 +- .../join/query/ParentIdQueryBuilder.java | 2 +- .../join/query/HasChildQueryBuilderTests.java | 2 +- .../join/query/HasParentQueryBuilderTests.java | 2 +- .../join/query/ParentIdQueryBuilderTests.java | 2 +- .../percolator/PercolateQueryBuilder.java | 2 +- .../percolator/PercolateQueryBuilderTests.java | 2 +- .../percolator/PercolatorQuerySearchIT.java | 2 +- .../org/elasticsearch/index/IndexModule.java | 8 ++++---- .../org/elasticsearch/index/IndexService.java | 8 ++++---- .../index/mapper/StringFieldType.java | 10 +++++----- .../query/LegacyGeoShapeQueryProcessor.java | 2 +- .../index/query/NestedQueryBuilder.java | 2 +- .../index/query/QueryShardContext.java | 16 ++++++++-------- .../index/query/ScriptQueryBuilder.java | 2 +- .../functionscore/ScriptScoreQueryBuilder.java | 2 +- .../mapper/LegacyGeoShapeFieldMapperTests.java | 2 +- .../index/query/NestedQueryBuilderTests.java | 2 +- .../index/query/ScriptQueryBuilderTests.java | 2 +- .../query/ScriptScoreQueryBuilderTests.java | 2 +- .../index/mapper/FieldTypeTestCase.java | 2 +- 24 files changed, 43 insertions(+), 45 deletions(-) diff --git a/docs/reference/query-dsl.asciidoc b/docs/reference/query-dsl.asciidoc index 86181d3960870..b69d19b73d3e1 100644 --- a/docs/reference/query-dsl.asciidoc +++ b/docs/reference/query-dsl.asciidoc @@ -28,8 +28,8 @@ Query clauses behave differently depending on whether they are used in [[query-dsl-allow-expensive-queries]] Allow expensive queries:: -Execution of certain types of queries have usually slow performance, which can affect the cluster performance. -Those queries can be categorised as follows: +Certain types of queries will generally execute slowly due to the way they are implemented, which can affect +the stability of the cluster. Those queries can be categorised as follows: * Queries that need to do linear scans to identify matches: ** <> ** <> diff --git a/docs/reference/query-dsl/prefix-query.asciidoc b/docs/reference/query-dsl/prefix-query.asciidoc index 5d2015b223ba7..8501f0f7d032b 100644 --- a/docs/reference/query-dsl/prefix-query.asciidoc +++ b/docs/reference/query-dsl/prefix-query.asciidoc @@ -69,7 +69,5 @@ efficiently at the cost of a larger index. [[prefix-query-allow-expensive-queries]] ===== Allow expensive queries Prefix queries will not be executed if <> -is set to false. If <> are enabled though, an optimised query is -built, which is not considered slow and is executed despite of the fact that the -<> set to false. - +is set to false. However, if <> are enabled, an optimised query is built which +is not considered slow, and will be executed in spite of this setting. diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java index dcb0fcad86f1b..80f389521e75a 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasChildQueryBuilder.java @@ -298,7 +298,7 @@ public String getWriteableName() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java index 85273a3d397d6..7dd3a6dbbd5a4 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/HasParentQueryBuilder.java @@ -161,7 +161,7 @@ public boolean ignoreUnmapped() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java index 3b6ab9c99ba52..27b98a8c1130a 100644 --- a/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java +++ b/modules/parent-join/src/main/java/org/elasticsearch/join/query/ParentIdQueryBuilder.java @@ -156,7 +156,7 @@ public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IO @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java index f51ed448ad50d..d41acf75d1298 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasChildQueryBuilderTests.java @@ -368,7 +368,7 @@ public void testExtractInnerHitBuildersWithDuplicate() { public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); HasChildQueryBuilder queryBuilder = hasChildQuery(CHILD_DOC, new TermQueryBuilder("custom_string", "value"), ScoreMode.None); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java index 0cd2de76559b5..53477e9e8349d 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/HasParentQueryBuilderTests.java @@ -269,7 +269,7 @@ public void testExtractInnerHitBuildersWithDuplicate() { public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder( CHILD_DOC, new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()), false); diff --git a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java index 5322ff3e162f3..27fbce48d9fae 100644 --- a/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java +++ b/modules/parent-join/src/test/java/org/elasticsearch/join/query/ParentIdQueryBuilderTests.java @@ -159,7 +159,7 @@ public void testIgnoreUnmapped() throws IOException { public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); ParentIdQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index f886ef1e9fe81..57a49dbed4f65 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -493,7 +493,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[percolate] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java index 25c78febdfd3e..f5decb7303e64 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolateQueryBuilderTests.java @@ -346,7 +346,7 @@ public void testSettingNameWhileRewritingWhenDocumentSupplierAndSourceNotNull() public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); PercolateQueryBuilder queryBuilder = doCreateTestQueryBuilder(true); ElasticsearchException e = expectThrows(ElasticsearchException.class, diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java index cbd319d342c94..75a9d5d105d30 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorQuerySearchIT.java @@ -879,7 +879,7 @@ public void testPercolatorQueryViaMultiSearch() throws Exception { assertThat(item.getFailureMessage(), containsString("[test/6] couldn't be found")); } - public void testDisallowExpensiveQueries() throws IOException { + public void testDallowExpensiveQueries() throws IOException { try { assertAcked(client().admin().indices().prepareCreate("test") .setMapping("id", "type=keyword", "field1", "type=keyword", "query", "type=percolator") diff --git a/server/src/main/java/org/elasticsearch/index/IndexModule.java b/server/src/main/java/org/elasticsearch/index/IndexModule.java index e8bef9e48f465..db79a3c1bc3fb 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexModule.java +++ b/server/src/main/java/org/elasticsearch/index/IndexModule.java @@ -130,7 +130,7 @@ public final class IndexModule { private final List searchOperationListeners = new ArrayList<>(); private final List indexOperationListeners = new ArrayList<>(); private final AtomicBoolean frozen = new AtomicBoolean(false); - private final BooleanSupplier isAllowExpensiveQueries; + private final BooleanSupplier allowExpensiveQueries; /** * Construct the index module for the index with the specified index settings. The index module contains extension points for plugins @@ -146,14 +146,14 @@ public IndexModule( final AnalysisRegistry analysisRegistry, final EngineFactory engineFactory, final Map directoryFactories, - final BooleanSupplier isAllowExpensiveQueries) { + final BooleanSupplier allowExpensiveQueries) { this.indexSettings = indexSettings; this.analysisRegistry = analysisRegistry; this.engineFactory = Objects.requireNonNull(engineFactory); this.searchOperationListeners.add(new SearchSlowLog(indexSettings)); this.indexOperationListeners.add(new IndexingSlowLog(indexSettings)); this.directoryFactories = Collections.unmodifiableMap(directoryFactories); - this.isAllowExpensiveQueries = isAllowExpensiveQueries; + this.allowExpensiveQueries = allowExpensiveQueries; } /** @@ -427,7 +427,7 @@ public IndexService newIndexService( new SimilarityService(indexSettings, scriptService, similarities), shardStoreDeleter, indexAnalyzers, engineFactory, circuitBreakerService, bigArrays, threadPool, scriptService, clusterService, client, queryCache, directoryFactory, eventListener, readerWrapperFactory, mapperRegistry, indicesFieldDataCache, searchOperationListeners, - indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled, isAllowExpensiveQueries); + indexOperationListeners, namedWriteableRegistry, idFieldDataEnabled, allowExpensiveQueries); success = true; return indexService; } finally { diff --git a/server/src/main/java/org/elasticsearch/index/IndexService.java b/server/src/main/java/org/elasticsearch/index/IndexService.java index dbf3861d79701..c52779d645970 100644 --- a/server/src/main/java/org/elasticsearch/index/IndexService.java +++ b/server/src/main/java/org/elasticsearch/index/IndexService.java @@ -126,7 +126,7 @@ public class IndexService extends AbstractIndexComponent implements IndicesClust private final IndexSettings indexSettings; private final List searchOperationListeners; private final List indexingOperationListeners; - private final BooleanSupplier isAllowExpensiveQueries; + private final BooleanSupplier allowExpensiveQueries; private volatile AsyncRefreshTask refreshTask; private volatile AsyncTranslogFSync fsyncTask; private volatile AsyncGlobalCheckpointTask globalCheckpointTask; @@ -168,9 +168,9 @@ public IndexService( List indexingOperationListeners, NamedWriteableRegistry namedWriteableRegistry, BooleanSupplier idFieldDataEnabled, - BooleanSupplier isAllowExpensiveQueries) { + BooleanSupplier allowExpensiveQueries) { super(indexSettings); - this.isAllowExpensiveQueries = isAllowExpensiveQueries; + this.allowExpensiveQueries = allowExpensiveQueries; this.indexSettings = indexSettings; this.xContentRegistry = xContentRegistry; this.similarityService = similarityService; @@ -571,7 +571,7 @@ public QueryShardContext newQueryShardContext(int shardId, IndexSearcher searche return new QueryShardContext( shardId, indexSettings, bigArrays, indexCache.bitsetFilterCache(), indexFieldData::getForField, mapperService(), similarityService(), scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, clusterAlias, - indexNameMatcher, isAllowExpensiveQueries); + indexNameMatcher, allowExpensiveQueries); } /** diff --git a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java index 194e4314a7061..4ddda3df0af2d 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/StringFieldType.java @@ -66,7 +66,7 @@ public Query termsQuery(List values, QueryShardContext context) { @Override public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int maxExpansions, boolean transpositions, QueryShardContext context) { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[fuzzy] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } @@ -77,7 +77,7 @@ public Query fuzzyQuery(Object value, Fuzziness fuzziness, int prefixLength, int @Override public Query prefixQuery(String value, MultiTermQuery.RewriteMethod method, QueryShardContext context) { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[prefix] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false. For optimised prefix queries on text " + "fields please enable [index_prefixes]."); @@ -97,7 +97,7 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu return termQuery; } - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[wildcard] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } @@ -111,7 +111,7 @@ public Query wildcardQuery(String value, MultiTermQuery.RewriteMethod method, Qu @Override public Query regexpQuery(String value, int flags, int maxDeterminizedStates, MultiTermQuery.RewriteMethod method, QueryShardContext context) { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[regexp] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } @@ -125,7 +125,7 @@ public Query regexpQuery(String value, int flags, int maxDeterminizedStates, @Override public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower, boolean includeUpper, QueryShardContext context) { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[range] queries on [text] or [keyword] fields cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java index 7b1abe71c241e..20bbb65f34f39 100644 --- a/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java +++ b/server/src/main/java/org/elasticsearch/index/query/LegacyGeoShapeQueryProcessor.java @@ -77,7 +77,7 @@ public Query process(Geometry shape, String fieldName, ShapeRelation relation, Q @Override public Query process(Geometry shape, String fieldName, SpatialStrategy strategy, ShapeRelation relation, QueryShardContext context) { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[geo-shape] queries on [PrefixTree geo shapes] cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java index 00674fce87605..a560af6826af9 100644 --- a/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/NestedQueryBuilder.java @@ -268,7 +268,7 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[joining] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 4eb545664d1b8..611a6fbe6d509 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -93,7 +93,7 @@ public class QueryShardContext extends QueryRewriteContext { private final Index fullyQualifiedIndex; private final Predicate indexNameMatcher; - private final BooleanSupplier isAllowExpensiveQueries; + private final BooleanSupplier allowExpensiveQueries; private final Map namedQueries = new HashMap<>(); private boolean allowUnmappedFields; @@ -115,18 +115,18 @@ public QueryShardContext(int shardId, LongSupplier nowInMillis, String clusterAlias, Predicate indexNameMatcher, - BooleanSupplier isAllowExpensiveQueries) { + BooleanSupplier allowExpensiveQueries) { this(shardId, indexSettings, bigArrays, bitsetFilterCache, indexFieldDataLookup, mapperService, similarityService, scriptService, xContentRegistry, namedWriteableRegistry, client, searcher, nowInMillis, indexNameMatcher, new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), - indexSettings.getIndex().getUUID()), isAllowExpensiveQueries); + indexSettings.getIndex().getUUID()), allowExpensiveQueries); } public QueryShardContext(QueryShardContext source) { this(source.shardId, source.indexSettings, source.bigArrays, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.getXContentRegistry(), source.getWriteableRegistry(), source.client, source.searcher, source.nowInMillis, source.indexNameMatcher, - source.fullyQualifiedIndex, source.isAllowExpensiveQueries); + source.fullyQualifiedIndex, source.allowExpensiveQueries); } private QueryShardContext(int shardId, @@ -144,7 +144,7 @@ private QueryShardContext(int shardId, LongSupplier nowInMillis, Predicate indexNameMatcher, Index fullyQualifiedIndex, - BooleanSupplier isAllowExpensiveQueries) { + BooleanSupplier allowExpensiveQueries) { super(xContentRegistry, namedWriteableRegistry, client, nowInMillis); this.shardId = shardId; this.similarityService = similarityService; @@ -159,7 +159,7 @@ private QueryShardContext(int shardId, this.searcher = searcher; this.indexNameMatcher = indexNameMatcher; this.fullyQualifiedIndex = fullyQualifiedIndex; - this.isAllowExpensiveQueries = isAllowExpensiveQueries; + this.allowExpensiveQueries = allowExpensiveQueries; } private void reset() { @@ -197,8 +197,8 @@ public BitSetProducer bitsetFilter(Query filter) { return bitsetFilterCache.getBitSetProducer(filter); } - public boolean isAllowExpensiveQueries() { - return isAllowExpensiveQueries.getAsBoolean(); + public boolean allowExpensiveQueries() { + return allowExpensiveQueries.getAsBoolean(); } @SuppressWarnings("unchecked") diff --git a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java index 990a3841493f7..da3f7850c0eb5 100644 --- a/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/ScriptQueryBuilder.java @@ -133,7 +133,7 @@ public static ScriptQueryBuilder fromXContent(XContentParser parser) throws IOEx @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[script] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java index 56b905c12a7d8..ff1db8a9e8c8f 100644 --- a/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java +++ b/server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java @@ -172,7 +172,7 @@ protected int doHashCode() { @Override protected Query doToQuery(QueryShardContext context) throws IOException { - if (context.isAllowExpensiveQueries() == false) { + if (context.allowExpensiveQueries() == false) { throw new ElasticsearchException("[script score] queries cannot be executed when '" + ALLOW_EXPENSIVE_QUERIES.getKey() + "' is set to false."); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java index ebe3fe64e3b75..aaabf3f9edbf0 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/LegacyGeoShapeFieldMapperTests.java @@ -704,7 +704,7 @@ public void testPointsOnlyFalseWithTermStrategy() throws Exception { public void testDisallowExpensiveQueries() throws IOException { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type1") .startObject("properties").startObject("location") .field("type", "geo_shape") diff --git a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 0de7105a9b6ec..c7f703113f264 100644 --- a/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -360,7 +360,7 @@ public void testExtractInnerHitBuildersWithDuplicate() { public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); NestedQueryBuilder queryBuilder = new NestedQueryBuilder("path", new MatchAllQueryBuilder(), ScoreMode.None); ElasticsearchException e = expectThrows(ElasticsearchException.class, diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java index d2f867f99728b..7eda1083b263e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptQueryBuilderTests.java @@ -134,7 +134,7 @@ public void testCacheability() throws IOException { public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); ScriptQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, diff --git a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java index 4176057f5fd78..04322a01d0f68 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ScriptScoreQueryBuilderTests.java @@ -101,7 +101,7 @@ public void testCacheability() throws IOException { public void testDisallowExpensiveQueries() { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(false); + when(queryShardContext.allowExpensiveQueries()).thenReturn(false); ScriptScoreQueryBuilder queryBuilder = doCreateTestQueryBuilder(); ElasticsearchException e = expectThrows(ElasticsearchException.class, diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java index 1a509a37b2cfc..1b8f78c761f2e 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/FieldTypeTestCase.java @@ -255,7 +255,7 @@ protected QueryShardContext randomMockShardContext() { static QueryShardContext createMockQueryShardContext(boolean allowExpensiveQueries) { QueryShardContext queryShardContext = mock(QueryShardContext.class); - when(queryShardContext.isAllowExpensiveQueries()).thenReturn(allowExpensiveQueries); + when(queryShardContext.allowExpensiveQueries()).thenReturn(allowExpensiveQueries); return queryShardContext; } From 1759a844ae27515599730ef6f910ee5b3f8f36d4 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Wed, 12 Feb 2020 17:01:47 +0100 Subject: [PATCH 25/25] fix docs --- docs/reference/query-dsl.asciidoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference/query-dsl.asciidoc b/docs/reference/query-dsl.asciidoc index b69d19b73d3e1..51889a5f2c159 100644 --- a/docs/reference/query-dsl.asciidoc +++ b/docs/reference/query-dsl.asciidoc @@ -32,7 +32,6 @@ Certain types of queries will generally execute slowly due to the way they are i the stability of the cluster. Those queries can be categorised as follows: * Queries that need to do linear scans to identify matches: ** <> -** <> * Queries that have a high up-front cost: ** <> ** <> @@ -42,6 +41,7 @@ the stability of the cluster. Those queries can be categorised as follows: * <> * Queries on <> * Queries that may have a high per-document cost: +** <> ** <> The execution of such queries can be prevented by setting the value of the `search.allow_expensive_queries`