Skip to content

Commit 05c7787

Browse files
committed
Ignore empty completion input (#30713)
This change makes sure that an empty completion input does not throw an IAE when indexing. Instead the input is ignored and the completion field is added in the list of ignored fields for the document. Closes #23121
1 parent ddbb225 commit 05c7787

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

server/src/main/java/org/elasticsearch/index/mapper/CompletionFieldMapper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ public Mapper parse(ParseContext context) throws IOException {
449449
// index
450450
for (Map.Entry<String, CompletionInputMetaData> completionInput : inputMap.entrySet()) {
451451
String input = completionInput.getKey();
452+
if (input.trim().isEmpty()) {
453+
context.addIgnoredField(fieldType.name());
454+
continue;
455+
}
452456
// truncate input
453457
if (input.length() > maxInputLength) {
454458
int len = Math.min(maxInputLength, input.length());

server/src/test/java/org/elasticsearch/index/mapper/CompletionFieldMapperTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,19 @@ public void testFieldValueValidation() throws Exception {
397397
assertThat(cause, instanceOf(IllegalArgumentException.class));
398398
assertThat(cause.getMessage(), containsString("[0x1e]"));
399399
}
400+
401+
// empty inputs are ignored
402+
ParsedDocument doc = defaultMapper.parse(SourceToParse.source("test", "type1", "1", BytesReference
403+
.bytes(XContentFactory.jsonBuilder()
404+
.startObject()
405+
.array("completion", " ", "")
406+
.endObject()),
407+
XContentType.JSON));
408+
assertThat(doc.docs().size(), equalTo(1));
409+
assertNull(doc.docs().get(0).get("completion"));
410+
assertNotNull(doc.docs().get(0).getField("_ignored"));
411+
IndexableField ignoredFields = doc.docs().get(0).getField("_ignored");
412+
assertThat(ignoredFields.stringValue(), equalTo("completion"));
400413
}
401414

402415
public void testPrefixQueryType() throws Exception {

0 commit comments

Comments
 (0)