Skip to content

Commit

Permalink
Automatically upgrade analyzed string fields that have `index_options…
Browse files Browse the repository at this point in the history
…` or `position_increment_gap` set. elastic#20002

Closes elastic#19974
  • Loading branch information
jpountz committed Aug 17, 2016
1 parent 1825d80 commit ffee9e8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@
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;
Expand All @@ -40,7 +36,6 @@
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -259,7 +255,10 @@ public Mapper.Builder parse(String fieldName, Map<String, Object> node, ParserCo
}

}
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 + "]");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ public void testUpgradeNotIndexedString() throws IOException {
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();
Expand Down Expand Up @@ -297,7 +323,11 @@ private void doTestUpgradeRandomMapping(int iter) throws IOException {
}
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();
Expand Down

0 comments on commit ffee9e8

Please sign in to comment.