Skip to content

Commit

Permalink
Ignore empty completion input (#30713)
Browse files Browse the repository at this point in the history
This change makes sure that an empty completion input does not throw an IAE when indexing.
Instead the input is ignored and the completion field is added in the list of ignored fields
for the document.

Closes #23121
  • Loading branch information
jimczi authored May 22, 2018
1 parent 59fc6a4 commit da6a56f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ public Mapper parse(ParseContext context) throws IOException {
// index
for (Map.Entry<String, CompletionInputMetaData> completionInput : inputMap.entrySet()) {
String input = completionInput.getKey();
if (input.trim().isEmpty()) {
context.addIgnoredField(fieldType.name());
continue;
}
// truncate input
if (input.length() > maxInputLength) {
int len = Math.min(maxInputLength, input.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,19 @@ public void testFieldValueValidation() throws Exception {
assertThat(cause, instanceOf(IllegalArgumentException.class));
assertThat(cause.getMessage(), containsString("[0x1e]"));
}

// empty inputs are ignored
ParsedDocument doc = defaultMapper.parse(SourceToParse.source("test", "type1", "1", BytesReference
.bytes(XContentFactory.jsonBuilder()
.startObject()
.array("completion", " ", "")
.endObject()),
XContentType.JSON));
assertThat(doc.docs().size(), equalTo(1));
assertNull(doc.docs().get(0).get("completion"));
assertNotNull(doc.docs().get(0).getField("_ignored"));
IndexableField ignoredFields = doc.docs().get(0).getField("_ignored");
assertThat(ignoredFields.stringValue(), equalTo("completion"));
}

public void testPrefixQueryType() throws Exception {
Expand Down

0 comments on commit da6a56f

Please sign in to comment.