From a9d2b1dde82d0400b47a9c7208f71e2d2e8133ee Mon Sep 17 00:00:00 2001 From: tony-dillon Date: Mon, 3 Sep 2018 15:49:53 +0100 Subject: [PATCH] Null completion field should not throw IAE (#33268) Ignore null value on the completion field Closes #33200 --- .../index/mapper/CompletionFieldMapper.java | 4 ++- .../mapper/CompletionFieldMapperTests.java | 11 +++++++ .../suggest/CompletionSuggestSearchIT.java | 31 ------------------- 3 files changed, 14 insertions(+), 32 deletions(-) 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 83d9a8178ca..d1490ae4c46 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -435,8 +435,10 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp XContentParser parser = context.parser(); Token token = parser.currentToken(); Map inputMap = new HashMap<>(1); + + // ignore null values if (token == Token.VALUE_NULL) { - throw new MapperParsingException("completion field [" + fieldType().name() + "] does not support null values"); + return null; } else if (token == Token.START_ARRAY) { while ((token = parser.nextToken()) != Token.END_ARRAY) { parse(context, token, parser, inputMap); 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 a01ddccc939..e3739eed336 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -448,6 +448,17 @@ public class CompletionFieldMapperTests extends ESSingleNodeTestCase { assertNotNull(doc.docs().get(0).getField("_ignored")); IndexableField ignoredFields = doc.docs().get(0).getField("_ignored"); assertThat(ignoredFields.stringValue(), equalTo("completion")); + + // null inputs are ignored + ParsedDocument nullDoc = defaultMapper.parse(SourceToParse.source("test", "type1", "1", BytesReference + .bytes(XContentFactory.jsonBuilder() + .startObject() + .nullField("completion") + .endObject()), + XContentType.JSON)); + assertThat(nullDoc.docs().size(), equalTo(1)); + assertNull(nullDoc.docs().get(0).get("completion")); + assertNull(nullDoc.docs().get(0).getField("_ignored")); } public void testPrefixQueryType() throws Exception { diff --git a/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java b/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java index 58b2b863963..65c58e631ec 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java @@ -31,12 +31,10 @@ import org.elasticsearch.action.search.SearchPhaseExecutionException; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.common.FieldMemoryStats; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.plugins.Plugin; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -1111,35 +1109,6 @@ public class CompletionSuggestSearchIT extends ESIntegTestCase { } } - // see issue #6399 - public void testIndexingUnrelatedNullValue() throws Exception { - String mapping = Strings - .toString(jsonBuilder() - .startObject() - .startObject(TYPE) - .startObject("properties") - .startObject(FIELD) - .field("type", "completion") - .endObject() - .endObject() - .endObject() - .endObject()); - - assertAcked(client().admin().indices().prepareCreate(INDEX).addMapping(TYPE, mapping, XContentType.JSON).get()); - ensureGreen(); - - client().prepareIndex(INDEX, TYPE, "1").setSource(FIELD, "strings make me happy", FIELD + "_1", "nulls make me sad") - .setRefreshPolicy(IMMEDIATE).get(); - - try { - client().prepareIndex(INDEX, TYPE, "2").setSource(FIELD, null, FIELD + "_1", "nulls make me sad").get(); - fail("Expected MapperParsingException for null value"); - } catch (MapperParsingException e) { - // make sure that the exception has the name of the field causing the error - assertTrue(e.getDetailedMessage().contains(FIELD)); - } - } - public void testMultiDocSuggestions() throws Exception { final CompletionMappingBuilder mapping = new CompletionMappingBuilder(); createIndexAndMapping(mapping);