Automatically upgrade analyzed string fields that have `index_options` or `position_increment_gap` set. #20002

Closes #19974
This commit is contained in:
Adrien Grand 2016-08-16 12:57:54 +02:00
parent 1825d8060c
commit ffee9e8833
2 changed files with 37 additions and 8 deletions

View File

@ -22,13 +22,9 @@ package org.elasticsearch.index.mapper;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedSetDocValuesField; import org.apache.lucene.document.SortedSetDocValuesField;
import org.apache.lucene.index.IndexOptions; 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.Query;
import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers; 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.IndexFieldData;
import org.elasticsearch.index.fielddata.plain.DocValuesIndexFieldData; import org.elasticsearch.index.fielddata.plain.DocValuesIndexFieldData;
import org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData; import org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData;
import org.elasticsearch.index.query.QueryShardContext;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
@ -71,7 +66,8 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc
"type", "type",
// common text parameters, for which the upgrade is straightforward // common text parameters, for which the upgrade is straightforward
"index", "store", "doc_values", "omit_norms", "norms", "fields", "copy_to", "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 class Defaults {
public static double FIELDDATA_MIN_FREQUENCY = 0; 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<String> 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 + "]"); + "or [keyword] field instead for field [" + fieldName + "]");
} }

View File

@ -106,6 +106,32 @@ public class StringMappingUpgradeTests extends ESSingleNodeTestCase {
assertEquals(IndexOptions.NONE, field.fieldType().indexOptions()); 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 { public void testIllegalIndexValue() throws IOException {
IndexService indexService = createIndex("test"); IndexService indexService = createIndex("test");
DocumentMapperParser parser = indexService.mapperService().documentMapperParser(); DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
@ -297,7 +323,11 @@ public class StringMappingUpgradeTests extends ESSingleNodeTestCase {
} }
if (randomBoolean()) { if (randomBoolean()) {
// this option is not upgraded automatically // 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; shouldUpgrade = false;
} }
mapping.endObject().endObject().endObject().endObject(); mapping.endObject().endObject().endObject().endObject();