Browse Source

Reject the `index_options` parameter for numeric fields (#26668)

Numeric fields no longer support the index_options parameter. This changes the parameter
to be rejected in numeric field types after it was deprecated in 6.0.

Closes #21475
Christoph Büscher 8 years ago
parent
commit
6189c54c84

+ 1 - 0
core/src/main/java/org/elasticsearch/index/mapper/FieldMapper.java

@@ -21,6 +21,7 @@ package org.elasticsearch.index.mapper;
 
 import com.carrotsearch.hppc.cursors.ObjectCursor;
 import com.carrotsearch.hppc.cursors.ObjectObjectCursor;
+
 import org.apache.lucene.document.FieldType;
 import org.apache.lucene.index.IndexOptions;
 import org.apache.lucene.index.IndexableField;

+ 6 - 0
core/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java

@@ -85,6 +85,12 @@ public class NumberFieldMapper extends FieldMapper {
             return builder;
         }
 
+        @Override
+        public Builder indexOptions(IndexOptions indexOptions) {
+            throw new MapperParsingException(
+                    "index_options not allowed in field [" + name + "] of type [" + builder.fieldType().typeName() + "]");
+        }
+
         protected Explicit<Boolean> ignoreMalformed(BuilderContext context) {
             if (ignoreMalformed != null) {
                 return new Explicit<>(ignoreMalformed, true);

+ 23 - 4
core/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java

@@ -31,9 +31,9 @@ import org.elasticsearch.index.mapper.NumberFieldTypeTests.OutOfRangeSpec;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.math.BigInteger;
-import java.util.List;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 
 import static org.hamcrest.Matchers.containsString;
 
@@ -41,7 +41,7 @@ public class NumberFieldMapperTests extends AbstractNumericFieldMapperTestCase {
 
     @Override
     protected void setTypeList() {
-        TYPES = new HashSet<>(Arrays.asList("byte", "short", "integer", "long", "float", "double"));
+        TYPES = new HashSet<>(Arrays.asList("byte", "short", "integer", "long", "float", "double", "half_float"));
         WHOLE_TYPES = new HashSet<>(Arrays.asList("byte", "short", "integer", "long"));
     }
 
@@ -258,7 +258,7 @@ public class NumberFieldMapperTests extends AbstractNumericFieldMapperTestCase {
 
     public void testRejectNorms() throws IOException {
         // not supported as of 5.0
-        for (String type : Arrays.asList("byte", "short", "integer", "long", "float", "double")) {
+        for (String type : TYPES) {
             DocumentMapperParser parser = createIndex("index-" + type).mapperService().documentMapperParser();
             String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
                 .startObject("properties")
@@ -273,6 +273,25 @@ public class NumberFieldMapperTests extends AbstractNumericFieldMapperTestCase {
         }
     }
 
+    /**
+     * `index_options` was deprecated and is rejected as of 7.0
+     */
+    public void testRejectIndexOptions() throws IOException {
+        for (String type : TYPES) {
+            DocumentMapperParser parser = createIndex("index-" + type).mapperService().documentMapperParser();
+            String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
+                .startObject("properties")
+                    .startObject("foo")
+                        .field("type", type)
+                    .field("index_options", randomFrom(new String[] { "docs", "freqs", "positions", "offsets" }))
+                    .endObject()
+                .endObject().endObject().endObject().string();
+            MapperParsingException e = expectThrows(MapperParsingException.class,
+                    () -> parser.parse("type", new CompressedXContent(mapping)));
+            assertThat(e.getMessage(), containsString("index_options not allowed in field [foo] of type [" + type +"]"));
+        }
+    }
+
     @Override
     protected void doTestNullValue(String type) throws IOException {
         String mapping = XContentFactory.jsonBuilder().startObject()
@@ -296,7 +315,7 @@ public class NumberFieldMapperTests extends AbstractNumericFieldMapperTestCase {
         assertArrayEquals(new IndexableField[0], doc.rootDoc().getFields("field"));
 
         Object missing;
-        if (Arrays.asList("float", "double").contains(type)) {
+        if (Arrays.asList("float", "double", "half_float").contains(type)) {
             missing = 123d;
         } else {
             missing = 123L;

+ 2 - 0
docs/reference/mapping/params/index-options.asciidoc

@@ -28,6 +28,8 @@ following settings:
     offsets (which map the term back to the original string) are indexed.
     Offsets are used by the <<unified-highlighter,unified highlighter>> to speed up highlighting.
 
+NOTE: <<number,Numeric fields>> don't support the `index_options` parameter any longer.
+
 <<mapping-index,Analyzed>> string fields use `positions` as the default, and
 all other fields use `docs` as the default.
 

+ 5 - 1
docs/reference/migration/migrate_7_0/mappings.asciidoc

@@ -3,4 +3,8 @@
 
 ==== The `_all` meta field is removed
 
-The `_all` field deprecated in 6 have now been removed.
+The `_all` field deprecated in 6 have now been removed.
+
+==== `index_options` for numeric fields has been removed
+
+The `index_options` field for numeric  fields has been deprecated in 6 and has now been removed.

+ 6 - 0
modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java

@@ -88,6 +88,12 @@ public class ScaledFloatFieldMapper extends FieldMapper {
             return builder;
         }
 
+        @Override
+        public Builder indexOptions(IndexOptions indexOptions) {
+            throw new MapperParsingException(
+                    "index_options not allowed in field [" + name + "] of type [" + builder.fieldType().typeName() + "]");
+        }
+
         protected Explicit<Boolean> ignoreMalformed(BuilderContext context) {
             if (ignoreMalformed != null) {
                 return new Explicit<>(ignoreMalformed, true);

+ 15 - 0
modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapperTests.java

@@ -336,4 +336,19 @@ public class ScaledFloatFieldMapperTests extends ESSingleNodeTestCase {
         );
         assertThat(e.getMessage(), containsString("name cannot be empty string"));
     }
+
+    /**
+     * `index_options` was deprecated and is rejected as of 7.0
+     */
+    public void testRejectIndexOptions() throws IOException {
+        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
+            .startObject("properties")
+                .startObject("foo")
+                    .field("type", "scaled_float")
+                .field("index_options", randomFrom(new String[] { "docs", "freqs", "positions", "offsets" }))
+                .endObject()
+            .endObject().endObject().endObject().string();
+        MapperParsingException e = expectThrows(MapperParsingException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
+        assertThat(e.getMessage(), containsString("index_options not allowed in field [foo] of type [scaled_float]"));
+    }
 }