From f7a6496e0fc2c3656f884ed9ffe3b32433841e66 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Thu, 18 Jan 2018 10:43:28 +0100 Subject: [PATCH] Ignore empty completion input This change makes sure that an empty completion input does not throw an IAE when indexing. Instead the input is simply ignored. Closes #23121 --- .../index/mapper/CompletionFieldMapper.java | 4 ++++ .../index/mapper/CompletionFieldMapperTests.java | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java index 186334c85cb33..e526ef942ca2c 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -457,6 +457,10 @@ public Mapper parse(ParseContext context) throws IOException { } input = input.substring(0, len); } + if (input.length() == 0) { + // Ignore empty inputs + continue; + } CompletionInputMetaData metaData = completionInput.getValue(); if (fieldType().hasContextMappings()) { fieldType().getContextMappings().addField(context.doc(), fieldType().name(), diff --git a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java index 74183ae864a60..6c5a525463c88 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -395,6 +395,16 @@ 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", XContentFactory.jsonBuilder() + .startObject() + .field("completion", "") + .endObject() + .bytes(), + XContentType.JSON)); + assertThat(doc.docs().size(), equalTo(1)); + assertNull(doc.docs().get(0).get("completion")); } public void testPrefixQueryType() throws Exception {