diff --git a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java index 6619315e34d..a223befffd9 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/AbstractFieldMapper.java @@ -615,16 +615,7 @@ public abstract class AbstractFieldMapper implements FieldMapper, Mapper { builder.field("store", fieldType.stored()); } if (fieldType.storeTermVectors() != defaultFieldType.storeTermVectors()) { - builder.field("store_term_vector", fieldType.storeTermVectors()); - } - if (fieldType.storeTermVectorOffsets() != defaultFieldType.storeTermVectorOffsets()) { - builder.field("store_term_vector_offsets", fieldType.storeTermVectorOffsets()); - } - if (fieldType.storeTermVectorPositions() != defaultFieldType.storeTermVectorPositions()) { - builder.field("store_term_vector_positions", fieldType.storeTermVectorPositions()); - } - if (fieldType.storeTermVectorPayloads() != defaultFieldType.storeTermVectorPayloads()) { - builder.field("store_term_vector_payloads", fieldType.storeTermVectorPayloads()); + builder.field("term_vector", termVectorOptionsToString(fieldType)); } if (fieldType.omitNorms() != defaultFieldType.omitNorms()) { builder.field("omit_norms", fieldType.omitNorms()); @@ -674,6 +665,28 @@ public abstract class AbstractFieldMapper implements FieldMapper, Mapper { } } + protected static String termVectorOptionsToString(FieldType fieldType) { + if (!fieldType.storeTermVectors()) { + return "no"; + } else if(!fieldType.storeTermVectorOffsets() && !fieldType.storeTermVectorPositions()) { + return "yes"; + } else if (fieldType.storeTermVectorOffsets() && !fieldType.storeTermVectorPositions()) { + return "with_offsets"; + } else { + StringBuilder builder = new StringBuilder("with"); + if (fieldType.storeTermVectorPositions()) { + builder.append("_positions"); + } + if (fieldType.storeTermVectorOffsets()) { + builder.append("_offsets"); + } + if (fieldType.storeTermVectorPayloads()) { + builder.append("_payloads"); + } + return builder.toString(); + } + } + protected static String indexTokenizeOptionToString(boolean indexed, boolean tokenized) { if (!indexed) { return "no"; diff --git a/src/main/java/org/elasticsearch/index/mapper/core/TypeParsers.java b/src/main/java/org/elasticsearch/index/mapper/core/TypeParsers.java index 94890363a4b..9abc1533b39 100644 --- a/src/main/java/org/elasticsearch/index/mapper/core/TypeParsers.java +++ b/src/main/java/org/elasticsearch/index/mapper/core/TypeParsers.java @@ -155,6 +155,10 @@ public class TypeParsers { } else if ("with_positions_offsets".equals(termVector)) { builder.storeTermVectorPositions(true); builder.storeTermVectorOffsets(true); + } else if ("with_positions_offsets_payloads".equals(termVector)) { + builder.storeTermVectorPositions(true); + builder.storeTermVectorOffsets(true); + builder.storeTermVectorPayloads(true); } else { throw new MapperParsingException("Wrong value for termVector [" + termVector + "] for field [" + fieldName + "]"); } diff --git a/src/test/java/org/elasticsearch/test/unit/index/mapper/string/SimpleStringMappingTests.java b/src/test/java/org/elasticsearch/test/unit/index/mapper/string/SimpleStringMappingTests.java index 7b7e0a50f76..6a45f2f544b 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/mapper/string/SimpleStringMappingTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/mapper/string/SimpleStringMappingTests.java @@ -83,6 +83,10 @@ public class SimpleStringMappingTests { assertThat(doc.rootDoc().getField("field").fieldType().omitNorms(), equalTo(false)); assertThat(doc.rootDoc().getField("field").fieldType().indexOptions(), equalTo(FieldInfo.IndexOptions.DOCS_AND_FREQS_AND_POSITIONS)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectors(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorOffsets(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorPositions(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorPayloads(), equalTo(false)); } @Test @@ -101,6 +105,10 @@ public class SimpleStringMappingTests { assertThat(doc.rootDoc().getField("field").fieldType().omitNorms(), equalTo(true)); assertThat(doc.rootDoc().getField("field").fieldType().indexOptions(), equalTo(FieldInfo.IndexOptions.DOCS_ONLY)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectors(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorOffsets(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorPositions(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorPayloads(), equalTo(false)); // now test it explicitly set @@ -118,5 +126,85 @@ public class SimpleStringMappingTests { assertThat(doc.rootDoc().getField("field").fieldType().omitNorms(), equalTo(false)); assertThat(doc.rootDoc().getField("field").fieldType().indexOptions(), equalTo(FieldInfo.IndexOptions.DOCS_AND_FREQS)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectors(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorOffsets(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorPositions(), equalTo(false)); + assertThat(doc.rootDoc().getField("field").fieldType().storeTermVectorPayloads(), equalTo(false)); } + + @Test + public void testTermVectors() throws Exception { + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties") + .startObject("field1") + .field("type", "string") + .field("term_vector", "no") + .endObject() + .startObject("field2") + .field("type", "string") + .field("term_vector", "yes") + .endObject() + .startObject("field3") + .field("type", "string") + .field("term_vector", "with_offsets") + .endObject() + .startObject("field4") + .field("type", "string") + .field("term_vector", "with_positions") + .endObject() + .startObject("field5") + .field("type", "string") + .field("term_vector", "with_positions_offsets") + .endObject() + .startObject("field6") + .field("type", "string") + .field("term_vector", "with_positions_offsets_payloads") + .endObject() + .endObject() + .endObject().endObject().string(); + + DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping); + + ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder() + .startObject() + .field("field1", "1234") + .field("field2", "1234") + .field("field3", "1234") + .field("field4", "1234") + .field("field5", "1234") + .field("field6", "1234") + .endObject() + .bytes()); + + assertThat(doc.rootDoc().getField("field1").fieldType().storeTermVectors(), equalTo(false)); + assertThat(doc.rootDoc().getField("field1").fieldType().storeTermVectorOffsets(), equalTo(false)); + assertThat(doc.rootDoc().getField("field1").fieldType().storeTermVectorPositions(), equalTo(false)); + assertThat(doc.rootDoc().getField("field1").fieldType().storeTermVectorPayloads(), equalTo(false)); + + assertThat(doc.rootDoc().getField("field2").fieldType().storeTermVectors(), equalTo(true)); + assertThat(doc.rootDoc().getField("field2").fieldType().storeTermVectorOffsets(), equalTo(false)); + assertThat(doc.rootDoc().getField("field2").fieldType().storeTermVectorPositions(), equalTo(false)); + assertThat(doc.rootDoc().getField("field2").fieldType().storeTermVectorPayloads(), equalTo(false)); + + assertThat(doc.rootDoc().getField("field3").fieldType().storeTermVectors(), equalTo(true)); + assertThat(doc.rootDoc().getField("field3").fieldType().storeTermVectorOffsets(), equalTo(true)); + assertThat(doc.rootDoc().getField("field3").fieldType().storeTermVectorPositions(), equalTo(false)); + assertThat(doc.rootDoc().getField("field3").fieldType().storeTermVectorPayloads(), equalTo(false)); + + assertThat(doc.rootDoc().getField("field4").fieldType().storeTermVectors(), equalTo(true)); + assertThat(doc.rootDoc().getField("field4").fieldType().storeTermVectorOffsets(), equalTo(false)); + assertThat(doc.rootDoc().getField("field4").fieldType().storeTermVectorPositions(), equalTo(true)); + assertThat(doc.rootDoc().getField("field4").fieldType().storeTermVectorPayloads(), equalTo(false)); + + assertThat(doc.rootDoc().getField("field5").fieldType().storeTermVectors(), equalTo(true)); + assertThat(doc.rootDoc().getField("field5").fieldType().storeTermVectorOffsets(), equalTo(true)); + assertThat(doc.rootDoc().getField("field5").fieldType().storeTermVectorPositions(), equalTo(true)); + assertThat(doc.rootDoc().getField("field5").fieldType().storeTermVectorPayloads(), equalTo(false)); + + assertThat(doc.rootDoc().getField("field6").fieldType().storeTermVectors(), equalTo(true)); + assertThat(doc.rootDoc().getField("field6").fieldType().storeTermVectorOffsets(), equalTo(true)); + assertThat(doc.rootDoc().getField("field6").fieldType().storeTermVectorPositions(), equalTo(true)); + assertThat(doc.rootDoc().getField("field6").fieldType().storeTermVectorPayloads(), equalTo(true)); + } + }