Null completion field should not throw IAE (#33268)

Ignore null value on the completion field

Closes #33200
This commit is contained in:
tony-dillon 2018-09-03 15:49:53 +01:00 committed by Jim Ferenczi
parent 0bf36253a9
commit a9d2b1dde8
3 changed files with 14 additions and 32 deletions

View File

@ -435,8 +435,10 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp
XContentParser parser = context.parser(); XContentParser parser = context.parser();
Token token = parser.currentToken(); Token token = parser.currentToken();
Map<String, CompletionInputMetaData> inputMap = new HashMap<>(1); Map<String, CompletionInputMetaData> inputMap = new HashMap<>(1);
// ignore null values
if (token == Token.VALUE_NULL) { 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) { } else if (token == Token.START_ARRAY) {
while ((token = parser.nextToken()) != Token.END_ARRAY) { while ((token = parser.nextToken()) != Token.END_ARRAY) {
parse(context, token, parser, inputMap); parse(context, token, parser, inputMap);

View File

@ -448,6 +448,17 @@ public class CompletionFieldMapperTests extends ESSingleNodeTestCase {
assertNotNull(doc.docs().get(0).getField("_ignored")); assertNotNull(doc.docs().get(0).getField("_ignored"));
IndexableField ignoredFields = doc.docs().get(0).getField("_ignored"); IndexableField ignoredFields = doc.docs().get(0).getField("_ignored");
assertThat(ignoredFields.stringValue(), equalTo("completion")); 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 { public void testPrefixQueryType() throws Exception {

View File

@ -31,12 +31,10 @@ import org.elasticsearch.action.search.SearchPhaseExecutionException;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse; import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.common.FieldMemoryStats; import org.elasticsearch.common.FieldMemoryStats;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.plugins.Plugin; import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.aggregations.AggregationBuilders; 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 { public void testMultiDocSuggestions() throws Exception {
final CompletionMappingBuilder mapping = new CompletionMappingBuilder(); final CompletionMappingBuilder mapping = new CompletionMappingBuilder();
createIndexAndMapping(mapping); createIndexAndMapping(mapping);