Skip to content

Commit 708f6bf

Browse files
authored
Add serialization test for FieldMappers when include_defaults=true (#58235)
Fixes a bug in TextFieldMapper serialization when index is false, and adds a base-class test to ensure that all field mappers are tested against all variations with defaults both included and excluded. Fixes #58188
1 parent 34a47f7 commit 708f6bf

File tree

23 files changed

+60
-25
lines changed

23 files changed

+60
-25
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public class ScaledFloatFieldMapper extends FieldMapper {
7272
public static final String CONTENT_TYPE = "scaled_float";
7373
// use the same default as numbers
7474
private static final Setting<Boolean> COERCE_SETTING = NumberFieldMapper.COERCE_SETTING;
75+
private static final FieldType FIELD_TYPE = new FieldType();
76+
static {
77+
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS);
78+
}
7579

7680
public static class Builder extends FieldMapper.Builder<Builder> {
7781

@@ -82,7 +86,7 @@ public static class Builder extends FieldMapper.Builder<Builder> {
8286
private Double nullValue;
8387

8488
public Builder(String name) {
85-
super(name, new FieldType());
89+
super(name, FIELD_TYPE);
8690
builder = this;
8791
}
8892

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeatureFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class RankFeatureFieldMapperTests extends FieldMapperTestCase<RankFeature
4545

4646
@Override
4747
protected Set<String> unsupportedProperties() {
48-
return Set.of("analyzer", "similarity", "store", "doc_values");
48+
return Set.of("analyzer", "similarity", "store", "doc_values", "index");
4949
}
5050

5151
@Before

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/RankFeaturesFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class RankFeaturesFieldMapperTests extends FieldMapperTestCase<RankFeatur
4040

4141
@Override
4242
protected Set<String> unsupportedProperties() {
43-
return Set.of("analyzer", "similarity", "store", "doc_values");
43+
return Set.of("analyzer", "similarity", "store", "doc_values", "index");
4444
}
4545

4646
IndexService indexService;

plugins/analysis-icu/src/main/java/org/elasticsearch/index/mapper/ICUCollationKeywordFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ protected void mergeOptions(FieldMapper other, List<String> conflicts) {
658658
@Override
659659
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
660660
super.doXContentBody(builder, includeDefaults, params);
661-
if (includeDefaults || (mappedFieldType.isSearchable() && fieldType.indexOptions() != IndexOptions.DOCS)) {
661+
if (fieldType.indexOptions() != IndexOptions.NONE && (includeDefaults || fieldType.indexOptions() != IndexOptions.DOCS)) {
662662
builder.field("index_options", indexOptionToString(fieldType.indexOptions()));
663663
}
664664
if (nullValue != null) {

plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class Murmur3FieldMapperTests extends FieldMapperTestCase<Murmur3FieldMap
5858

5959
@Override
6060
protected Set<String> unsupportedProperties() {
61-
return Set.of("analyzer", "similarity", "doc_values");
61+
return Set.of("analyzer", "similarity", "doc_values", "index");
6262
}
6363

6464
@Before

server/src/internalClusterTest/java/org/elasticsearch/indices/mapping/SimpleGetFieldMappingsIT.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,10 @@ public void testGetFieldMappings() throws Exception {
127127
public void testSimpleGetFieldMappingsWithDefaults() throws Exception {
128128
assertAcked(prepareCreate("test").setMapping(getMappingForType()));
129129
client().admin().indices().preparePutMapping("test").setSource("num", "type=long").get();
130+
client().admin().indices().preparePutMapping("test").setSource("field2", "type=text,index=false").get();
130131

131132
GetFieldMappingsResponse response = client().admin().indices().prepareGetFieldMappings()
132-
.setFields("num", "field1", "obj.subfield").includeDefaults(true).get();
133+
.setFields("num", "field1", "field2", "obj.subfield").includeDefaults(true).get();
133134

134135
assertThat((Map<String, Object>) response.fieldMappings("test", "num").sourceAsMap().get("num"),
135136
hasEntry("index", Boolean.TRUE));
@@ -139,6 +140,8 @@ public void testSimpleGetFieldMappingsWithDefaults() throws Exception {
139140
hasEntry("index", Boolean.TRUE));
140141
assertThat((Map<String, Object>) response.fieldMappings("test", "field1").sourceAsMap().get("field1"),
141142
hasEntry("type", "text"));
143+
assertThat((Map<String, Object>) response.fieldMappings("test", "field2").sourceAsMap().get("field2"),
144+
hasEntry("type", "text"));
142145
assertThat((Map<String, Object>) response.fieldMappings("test", "obj.subfield").sourceAsMap().get("subfield"),
143146
hasEntry("type", "keyword"));
144147
}

server/src/main/java/org/elasticsearch/index/mapper/AbstractGeometryFieldMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.elasticsearch.index.mapper;
2020

2121
import org.apache.lucene.document.FieldType;
22+
import org.apache.lucene.index.IndexOptions;
2223
import org.apache.lucene.index.IndexableField;
2324
import org.apache.lucene.index.Term;
2425
import org.apache.lucene.search.DocValuesFieldExistsQuery;
@@ -60,6 +61,7 @@ public static class Defaults {
6061
public static final FieldType FIELD_TYPE = new FieldType();
6162
static {
6263
FIELD_TYPE.setStored(false);
64+
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS);
6365
FIELD_TYPE.freeze();
6466
}
6567
}

server/src/main/java/org/elasticsearch/index/mapper/GeoPointFieldMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.document.LatLonDocValuesField;
2323
import org.apache.lucene.document.LatLonPoint;
2424
import org.apache.lucene.document.StoredField;
25+
import org.apache.lucene.index.IndexOptions;
2526
import org.apache.lucene.index.IndexableField;
2627
import org.elasticsearch.ElasticsearchParseException;
2728
import org.elasticsearch.common.Explicit;
@@ -49,6 +50,7 @@ public class GeoPointFieldMapper extends AbstractPointGeometryFieldMapper<List<?
4950
public static final FieldType FIELD_TYPE = new FieldType();
5051
static {
5152
FIELD_TYPE.setStored(false);
53+
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS);
5254
FIELD_TYPE.freeze();
5355
}
5456

server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.lucene.document.FieldType;
2222
import org.apache.lucene.document.LatLonShape;
23+
import org.apache.lucene.index.IndexOptions;
2324
import org.elasticsearch.common.Explicit;
2425
import org.elasticsearch.common.geo.GeometryParser;
2526
import org.elasticsearch.common.geo.builders.ShapeBuilder;
@@ -54,6 +55,7 @@ public class GeoShapeFieldMapper extends AbstractShapeGeometryFieldMapper<Geomet
5455
public static final FieldType FIELD_TYPE = new FieldType();
5556
static {
5657
FIELD_TYPE.setDimensions(7, 4, Integer.BYTES);
58+
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS);
5759
FIELD_TYPE.setOmitNorms(true);
5860
FIELD_TYPE.freeze();
5961
}

server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.lucene.document.InetAddressPoint;
2424
import org.apache.lucene.document.SortedSetDocValuesField;
2525
import org.apache.lucene.document.StoredField;
26+
import org.apache.lucene.index.IndexOptions;
2627
import org.apache.lucene.index.SortedSetDocValues;
2728
import org.apache.lucene.index.Term;
2829
import org.apache.lucene.search.DocValuesFieldExistsQuery;
@@ -63,6 +64,7 @@ public static class Defaults {
6364
public static final FieldType FIELD_TYPE = new FieldType();
6465
static {
6566
FIELD_TYPE.setDimensions(1, Integer.BYTES);
67+
FIELD_TYPE.setIndexOptions(IndexOptions.DOCS);
6668
FIELD_TYPE.freeze();
6769
}
6870
}

0 commit comments

Comments
 (0)