Skip to content

Commit

Permalink
Exclude unmapped fields from query max_clause limit (elastic#49523)
Browse files Browse the repository at this point in the history
Take into account of number of unmapped fields when calculating against limit. 
Closes elastic#49002
  • Loading branch information
zacharymorn authored and SivagurunathanV committed Jan 21, 2020
1 parent 9cab6e8 commit c31d60e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public static Map<String, Float> 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<String, Float> fieldMap = resolveMappingField(context, fieldEntry.getKey(), weight,
!multiField, !allField, fieldSuffix);
Map<String, Float> fieldMap = resolveMappingField(context, fieldEntry.getKey(), weight, !multiField, !allField, fieldSuffix);

for (Map.Entry<String, Float> field : fieldMap.entrySet()) {
float boost = field.getValue();
if (resolvedFields.containsKey(field.getKey())) {
Expand All @@ -97,6 +97,7 @@ public static Map<String, Float> resolveMappingFields(QueryShardContext context,
resolvedFields.put(field.getKey(), boost);
}
}

checkForTooManyFields(resolvedFields, context);
return resolvedFields;
}
Expand Down Expand Up @@ -141,8 +142,6 @@ public static Map<String, Float> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndexRequestBuilder> indexRequests = new ArrayList<>();
indexRequests.add(client().prepareIndex("test").setId("1").setSource("f3", "text", "f2", "one"));
Expand Down

0 comments on commit c31d60e

Please sign in to comment.