Skip to content

Commit 373edee

Browse files
nilabhsagarnik9000
authored andcommitted
Provide informative error message in case of unknown suggestion context. (#24241)
Provide a list of available contexts when you send an unknown context to the completion suggester.
1 parent 9311986 commit 373edee

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

core/src/main/java/org/elasticsearch/search/suggest/completion/context/ContextMappings.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public int size() {
8181
public ContextMapping get(String name) {
8282
ContextMapping contextMapping = contextNameMap.get(name);
8383
if (contextMapping == null) {
84-
throw new IllegalArgumentException("Unknown context name[" + name + "], must be one of " + contextNameMap.size());
84+
List<String> keys = new ArrayList<>(contextNameMap.keySet());
85+
Collections.sort(keys);
86+
throw new IllegalArgumentException("Unknown context name [" + name + "], must be one of " + keys.toString());
8587
}
8688
return contextMapping;
8789
}

core/src/test/java/org/elasticsearch/search/suggest/completion/CategoryContextMappingTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.elasticsearch.common.xcontent.XContentParser;
3030
import org.elasticsearch.common.xcontent.XContentType;
3131
import org.elasticsearch.common.xcontent.json.JsonXContent;
32+
import org.elasticsearch.index.mapper.CompletionFieldMapper.CompletionFieldType;
3233
import org.elasticsearch.index.mapper.DocumentMapper;
3334
import org.elasticsearch.index.mapper.FieldMapper;
3435
import org.elasticsearch.index.mapper.MappedFieldType;
@@ -673,6 +674,31 @@ public void testQueryContextParsingMixedHavingNULL() throws Exception {
673674
Exception e = expectThrows(ElasticsearchParseException.class, () -> mapping.parseQueryContext(createParseContext(parser)));
674675
assertEquals("category context must be an object, string, number or boolean", e.getMessage());
675676
}
677+
678+
public void testUnknownQueryContextParsing() throws Exception {
679+
String mapping = jsonBuilder().startObject().startObject("type1")
680+
.startObject("properties").startObject("completion")
681+
.field("type", "completion")
682+
.startArray("contexts")
683+
.startObject()
684+
.field("name", "ctx")
685+
.field("type", "category")
686+
.endObject()
687+
.startObject()
688+
.field("name", "type")
689+
.field("type", "category")
690+
.endObject()
691+
.endArray()
692+
.endObject().endObject()
693+
.endObject().endObject().string();
694+
695+
DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
696+
FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
697+
CompletionFieldType completionFieldType = (CompletionFieldType) fieldMapper.fieldType();
698+
699+
Exception e = expectThrows(IllegalArgumentException.class, () -> completionFieldType.getContextMappings().get("brand"));
700+
assertEquals("Unknown context name [brand], must be one of [ctx, type]", e.getMessage());
701+
}
676702

677703
public void testParsingContextFromDocument() throws Exception {
678704
CategoryContextMapping mapping = ContextBuilder.category("cat").field("category").build();

0 commit comments

Comments
 (0)