Skip to content

Commit 682b0d5

Browse files
author
Christoph Büscher
authored
Add warning about upcoming expanded fields limit (#34906)
Starting with Elasticsearch 7.0, we limit the number of automatically expanded fields for the "all fields" mode ("default_field": "*") for the query_string and simple_query_string queries to 1024 fields (see #26541). This change adds a deprecation warning to the QueryParserHelper where in the next major version we will throw an error to warn users of the upcoming change. Relates to #26541
1 parent 44fe716 commit 682b0d5

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

docs/reference/migration/index.asciidoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ include::migrate_6_3.asciidoc[]
3333
include::migrate_6_4.asciidoc[]
3434

3535
include::migrate_6_5.asciidoc[]
36+
37+
include::migrate_6_6.asciidoc[]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[[breaking-changes-6.6]]
2+
== Breaking changes in 6.6
3+
++++
4+
<titleabbrev>6.6</titleabbrev>
5+
++++
6+
7+
This section discusses the changes that you need to be aware of when migrating
8+
your application to Elasticsearch 6.6.
9+
10+
* <<breaking_66_search_changes>>
11+
12+
See also <<release-highlights>> and <<es-release-notes>>.
13+
14+
[float]
15+
[[breaking_66_search_changes]]
16+
=== Search changes
17+
18+
[float]
19+
==== `query_string`, `multi_match` and `simple_query_string` query
20+
21+
Using automatically expanded fields for the "all fields" mode ("default_field": "*")
22+
for the `query_string`, `multi_match` and `simple_query_string` now raises a warning and
23+
a deprecation notice to be logged for queries beyond 1024 fields. This limit will be
24+
enforced with a hard error starting in 7.0.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ settings, which in turn defaults to `*`.
6363
and filters the metadata fields. All extracted fields are then combined
6464
to build a query when no prefix field is provided.
6565

66+
WARNING The amount of fields being allowed to be queries at once will be limited
67+
to 1024 in the next major version or Elasticsearch. Currently this will be raised
68+
and logged as a Warning only.
69+
6670
|`default_operator` |The default operator used if no explicit operator
6771
is specified. For example, with a default operator of `OR`, the query
6872
`capital of Hungary` is translated to `capital OR of OR Hungary`, and

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ The `simple_query_string` top level parameters include:
3333
`*` extracts all fields in the mapping that are eligible to term queries
3434
and filters the metadata fields.
3535

36+
WARNING The amount of fields being allowed to be queries at once will be limited
37+
to 1024 in the next major version or Elasticsearch. Currently this will be raised
38+
and logged as a Warning only.
39+
3640
|`default_operator` |The default operator used if no explicit operator
3741
is specified. For example, with a default operator of `OR`, the query
3842
`capital of Hungary` is translated to `capital OR of OR Hungary`, and

server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
package org.elasticsearch.index.search;
2121

22+
import org.apache.logging.log4j.LogManager;
2223
import org.elasticsearch.ElasticsearchParseException;
24+
import org.elasticsearch.common.logging.DeprecationLogger;
2325
import org.elasticsearch.common.regex.Regex;
2426
import org.elasticsearch.index.mapper.MappedFieldType;
2527
import org.elasticsearch.index.query.QueryShardContext;
@@ -34,6 +36,9 @@
3436
* Helpers to extract and expand field names and boosts
3537
*/
3638
public final class QueryParserHelper {
39+
40+
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(LogManager.getLogger(QueryParserHelper.class));
41+
3742
private QueryParserHelper() {}
3843

3944
/**
@@ -85,6 +90,7 @@ public static Map<String, Float> resolveMappingFields(QueryShardContext context,
8590
!multiField, !allField, fieldSuffix);
8691
resolvedFields.putAll(fieldMap);
8792
}
93+
checkForTooManyFields(resolvedFields);
8894
return resolvedFields;
8995
}
9096

@@ -149,6 +155,16 @@ public static Map<String, Float> resolveMappingField(QueryShardContext context,
149155
}
150156
fields.put(fieldName, weight);
151157
}
158+
checkForTooManyFields(fields);
152159
return fields;
153160
}
161+
162+
private static void checkForTooManyFields(Map<String, Float> fields) {
163+
if (fields.size() > 1024) {
164+
DEPRECATION_LOGGER.deprecatedAndMaybeLog("field_expansion_limit",
165+
"Field expansion matches too many fields, got: {}. A limit of 1024 will be enforced starting "
166+
+ "with version 7.0 of Elasticsearch. Lowering the number of fields will be necessary before upgrading.",
167+
fields.size());
168+
}
169+
}
154170
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.index.search;
21+
22+
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.index.IndexService;
25+
import org.elasticsearch.index.mapper.MapperService;
26+
import org.elasticsearch.index.query.QueryShardContext;
27+
import org.elasticsearch.test.ESSingleNodeTestCase;
28+
29+
import java.util.Collections;
30+
31+
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
32+
33+
public class QueryParserHelperTests extends ESSingleNodeTestCase {
34+
35+
/**
36+
* Test that when {@link QueryParserHelper#resolveMappingFields(QueryShardContext, java.util.Map, String)} exceeds
37+
* the limit of 1024 fields, we emit a warning
38+
*/
39+
public void testLimitOnExpandedFields() throws Exception {
40+
41+
XContentBuilder builder = jsonBuilder();
42+
builder.startObject();
43+
builder.startObject("type1");
44+
builder.startObject("properties");
45+
for (int i = 0; i < 1025; i++) {
46+
builder.startObject("field" + i).field("type", "text").endObject();
47+
}
48+
builder.endObject(); // properties
49+
builder.endObject(); // type1
50+
builder.endObject();
51+
IndexService indexService = createIndex("toomanyfields", Settings.builder()
52+
.put(MapperService.INDEX_MAPPING_TOTAL_FIELDS_LIMIT_SETTING.getKey(), 1200).build(),
53+
"type1", builder);
54+
client().prepareIndex("toomanyfields", "type1", "1").setSource("field171", "foo bar baz").get();
55+
56+
QueryShardContext queryShardContext = indexService.newQueryShardContext(
57+
randomInt(20), null, () -> { throw new UnsupportedOperationException(); }, null);
58+
59+
QueryParserHelper.resolveMappingField(queryShardContext, "*", 1.0f, true, false);
60+
assertWarnings("Field expansion matches too many fields, got: 1025. A limit of 1024 will be enforced starting with "
61+
+ "version 7.0 of Elasticsearch. Lowering the number of fields will be necessary before upgrading.");
62+
63+
QueryParserHelper.resolveMappingFields(queryShardContext,Collections.singletonMap("*", 1.0f));
64+
assertWarnings("Field expansion matches too many fields, got: 1025. A limit of 1024 will be enforced starting with "
65+
+ "version 7.0 of Elasticsearch. Lowering the number of fields will be necessary before upgrading.");
66+
}
67+
}

0 commit comments

Comments
 (0)