From c31d60e04432a4c855bf55906f19243ef3c58961 Mon Sep 17 00:00:00 2001 From: zacharymorn Date: Mon, 20 Jan 2020 02:53:38 -0800 Subject: [PATCH] Exclude unmapped fields from query max_clause limit (#49523) Take into account of number of unmapped fields when calculating against limit. Closes #49002 --- .../index/search/QueryParserHelper.java | 7 ++--- .../search/query/QueryStringIT.java | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java b/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java index e4b397c9a538b..768d017974e2f 100644 --- a/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java +++ b/server/src/main/java/org/elasticsearch/index/search/QueryParserHelper.java @@ -87,8 +87,8 @@ public static Map resolveMappingFields(QueryShardContext context, boolean allField = Regex.isMatchAllPattern(fieldEntry.getKey()); boolean multiField = Regex.isSimpleMatchPattern(fieldEntry.getKey()); float weight = fieldEntry.getValue() == null ? 1.0f : fieldEntry.getValue(); - Map fieldMap = resolveMappingField(context, fieldEntry.getKey(), weight, - !multiField, !allField, fieldSuffix); + Map fieldMap = resolveMappingField(context, fieldEntry.getKey(), weight, !multiField, !allField, fieldSuffix); + for (Map.Entry field : fieldMap.entrySet()) { float boost = field.getValue(); if (resolvedFields.containsKey(field.getKey())) { @@ -97,6 +97,7 @@ public static Map resolveMappingFields(QueryShardContext context, resolvedFields.put(field.getKey(), boost); } } + checkForTooManyFields(resolvedFields, context); return resolvedFields; } @@ -141,8 +142,6 @@ public static Map resolveMappingField(QueryShardContext context, MappedFieldType fieldType = context.getMapperService().fullName(fieldName); if (fieldType == null) { - // Note that we don't ignore unmapped fields. - fields.put(fieldName, weight); continue; } diff --git a/server/src/test/java/org/elasticsearch/search/query/QueryStringIT.java b/server/src/test/java/org/elasticsearch/search/query/QueryStringIT.java index c901ab8270433..e47c330bb9fb5 100644 --- a/server/src/test/java/org/elasticsearch/search/query/QueryStringIT.java +++ b/server/src/test/java/org/elasticsearch/search/query/QueryStringIT.java @@ -295,6 +295,35 @@ public void testLimitOnExpandedFields() throws Exception { + (CLUSTER_MAX_CLAUSE_COUNT + 1))); } + // The only expectation for this test is to not throw exception + public void testLimitOnExpandedFieldsButIgnoreUnmappedFields() throws Exception { + XContentBuilder builder = jsonBuilder(); + builder.startObject(); + builder.startObject("_doc"); + builder.startObject("properties"); + for (int i = 0; i < CLUSTER_MAX_CLAUSE_COUNT; i++) { + builder.startObject("field" + i).field("type", "text").endObject(); + } + builder.endObject(); // properties + builder.endObject(); // type1 + builder.endObject(); + + assertAcked(prepareCreate("ignoreunmappedfields").setMapping(builder)); + + client().prepareIndex("ignoreunmappedfields").setId("1").setSource("field1", "foo bar baz").get(); + refresh(); + + QueryStringQueryBuilder qb = queryStringQuery("bar"); + if (randomBoolean()) { + qb.field("*") + .field("unmappedField1") + .field("unmappedField2") + .field("unmappedField3") + .field("unmappedField4"); + } + client().prepareSearch("ignoreunmappedfields").setQuery(qb).get(); + } + public void testFieldAlias() throws Exception { List indexRequests = new ArrayList<>(); indexRequests.add(client().prepareIndex("test").setId("1").setSource("f3", "text", "f2", "one"));