From 1c6f5ce9cb0b52a74695577d3bd590b4620fe00d Mon Sep 17 00:00:00 2001 From: David Turner Date: Thu, 9 Nov 2017 10:45:44 +0000 Subject: [PATCH] Improve error message for parse failures of completion fields (#27297) Fix spacing/grammar/punctuation, and include the field name and location in the source document. --- .../index/mapper/CompletionFieldMapper.java | 4 ++-- .../mapper/CompletionFieldMapperTests.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java index 1c92150676c..186334c85cb 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java @@ -30,9 +30,9 @@ import org.apache.lucene.search.suggest.document.FuzzyCompletionQuery; import org.apache.lucene.search.suggest.document.PrefixCompletionQuery; import org.apache.lucene.search.suggest.document.RegexCompletionQuery; import org.apache.lucene.search.suggest.document.SuggestField; -import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; import org.elasticsearch.common.ParseField; +import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.util.set.Sets; @@ -560,7 +560,7 @@ public class CompletionFieldMapper extends FieldMapper implements ArrayValueMapp } } } else { - throw new ElasticsearchParseException("failed to parse expected text or object got" + token.name()); + throw new ParsingException(parser.getTokenLocation(), "failed to parse [" + parser.currentName() + "]: expected text or object, but got " + token.name()); } } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java index 5da524b69c0..74183ae864a 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java @@ -163,6 +163,25 @@ public class CompletionFieldMapperTests extends ESSingleNodeTestCase { assertSuggestFields(fields, 1); } + public void testParsingFailure() throws Exception { + String mapping = jsonBuilder().startObject().startObject("type1") + .startObject("properties").startObject("completion") + .field("type", "completion") + .endObject().endObject() + .endObject().endObject().string(); + + DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping)); + + MapperParsingException e = expectThrows(MapperParsingException.class, () -> + defaultMapper.parse(SourceToParse.source("test", "type1", "1", XContentFactory.jsonBuilder() + .startObject() + .field("completion", 1.0) + .endObject() + .bytes(), + XContentType.JSON))); + assertEquals("failed to parse [completion]: expected text or object, but got VALUE_NUMBER", e.getCause().getMessage()); + } + public void testParsingMultiValued() throws Exception { String mapping = jsonBuilder().startObject().startObject("type1") .startObject("properties").startObject("completion")