From ffee9e8833415d4cbdd02280b2b5ed8da63f3680 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 16 Aug 2016 12:57:54 +0200 Subject: [PATCH] Automatically upgrade analyzed string fields that have `index_options` or `position_increment_gap` set. #20002 Closes #19974 --- .../index/mapper/StringFieldMapper.java | 13 ++++---- .../mapper/StringMappingUpgradeTests.java | 32 ++++++++++++++++++- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java index 6f529c82ea1..887a40fe70b 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/StringFieldMapper.java @@ -22,13 +22,9 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.document.Field; import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.index.IndexOptions; -import org.apache.lucene.index.Term; -import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.Query; -import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.util.BytesRef; import org.elasticsearch.Version; -import org.elasticsearch.common.Nullable; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; @@ -40,7 +36,6 @@ import org.elasticsearch.index.analysis.NamedAnalyzer; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.DocValuesIndexFieldData; import org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData; -import org.elasticsearch.index.query.QueryShardContext; import java.io.IOException; import java.util.Arrays; @@ -71,7 +66,8 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc "type", // common text parameters, for which the upgrade is straightforward "index", "store", "doc_values", "omit_norms", "norms", "fields", "copy_to", - "fielddata", "include_in_all", "analyzer", "search_analyzer", "search_quote_analyzer")); + "fielddata", "include_in_all", "analyzer", "search_analyzer", "search_quote_analyzer", + "index_options", "position_increment_gap")); public static class Defaults { public static double FIELDDATA_MIN_FREQUENCY = 0; @@ -259,7 +255,10 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc } } - throw new IllegalArgumentException("The [string] type is removed in 5.0. You should now use either a [text] " + Set unsupportedParameters = new HashSet<>(node.keySet()); + unsupportedParameters.removeAll(autoUpgradeParameters); + throw new IllegalArgumentException("The [string] type is removed in 5.0 and automatic upgrade failed because parameters " + + unsupportedParameters + " are not supported for automatic upgrades. You should now use either a [text] " + "or [keyword] field instead for field [" + fieldName + "]"); } diff --git a/core/src/test/java/org/elasticsearch/index/mapper/StringMappingUpgradeTests.java b/core/src/test/java/org/elasticsearch/index/mapper/StringMappingUpgradeTests.java index 41077493761..311bf0205ed 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/StringMappingUpgradeTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/StringMappingUpgradeTests.java @@ -106,6 +106,32 @@ public class StringMappingUpgradeTests extends ESSingleNodeTestCase { assertEquals(IndexOptions.NONE, field.fieldType().indexOptions()); } + public void testUpgradeIndexOptions() throws IOException { + IndexService indexService = createIndex("test"); + DocumentMapperParser parser = indexService.mapperService().documentMapperParser(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("field").field("type", "string") + .field("index_options", "offsets").endObject().endObject() + .endObject().endObject().string(); + DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + FieldMapper field = mapper.mappers().getMapper("field"); + assertThat(field, instanceOf(TextFieldMapper.class)); + assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS, field.fieldType().indexOptions()); + } + + public void testUpgradePositionGap() throws IOException { + IndexService indexService = createIndex("test"); + DocumentMapperParser parser = indexService.mapperService().documentMapperParser(); + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") + .startObject("properties").startObject("field").field("type", "string") + .field("position_increment_gap", 42).endObject().endObject() + .endObject().endObject().string(); + DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); + FieldMapper field = mapper.mappers().getMapper("field"); + assertThat(field, instanceOf(TextFieldMapper.class)); + assertEquals(42, field.fieldType().indexAnalyzer().getPositionIncrementGap("field")); + } + public void testIllegalIndexValue() throws IOException { IndexService indexService = createIndex("test"); DocumentMapperParser parser = indexService.mapperService().documentMapperParser(); @@ -297,7 +323,11 @@ public class StringMappingUpgradeTests extends ESSingleNodeTestCase { } if (randomBoolean()) { // this option is not upgraded automatically - mapping.field("index_options", "docs"); + if (keyword) { + mapping.field("index_options", "docs"); + } else { + mapping.field("ignore_above", 30); + } shouldUpgrade = false; } mapping.endObject().endObject().endObject().endObject();