From fb058c6dd8c6c73d7a4bb67905d8405f675d6aeb Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Thu, 12 Mar 2020 18:54:12 +0100 Subject: [PATCH] Fix Term Vectors with artificial docs and keyword fields Previously, Term Vectors API was returning empty results for artificial documents with keyword fields. Checking only for `string()` on `IndexableField` is not enough, since for `KeywordFieldType` `binaryValue()` must be used instead. Fixes #53494 --- .../java/org/elasticsearch/index/mapper/ParseContext.java | 8 ++++++-- .../index/mapper/KeywordFieldMapperTests.java | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java b/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java index b2c536a900e33..c6418d75f5017 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java @@ -131,8 +131,12 @@ public IndexableField[] getFields(String name) { public final String[] getValues(String name) { List result = new ArrayList<>(); for (IndexableField field : fields) { - if (field.name().equals(name) && field.stringValue() != null) { - result.add(field.stringValue()); + if (field.name().equals(name)) { + if (field.stringValue() != null) { + result.add(field.stringValue()); + } else if (field.binaryValue() != null) { // KeywordFieldType + result.add(field.binaryValue().utf8ToString()); + } } } return result.toArray(new String[result.size()]); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java index 0e25c0a6a212b..b084e2e041820 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/KeywordFieldMapperTests.java @@ -128,6 +128,8 @@ public void testDefaults() throws Exception { fieldType = fields[1].fieldType(); assertThat(fieldType.indexOptions(), equalTo(IndexOptions.NONE)); assertEquals(DocValuesType.SORTED_SET, fieldType.docValuesType()); + + assertArrayEquals(new String[] { "1234", "1234" }, doc.rootDoc().getValues("field")); // used for TermVectors } public void testIgnoreAbove() throws IOException {