Skip to content

Commit 72f9c4c

Browse files
authored
Add null-field checks to shape field mappers (#71999)
#71696 introduced a regression to the various shape field mappers, where they would no longer handle null values. This commit fixes that regression and adds a testNullValues method to MapperTestCase to ensure that all field mappers correctly handle nulls. Fixes #71874
1 parent 1f2e54b commit 72f9c4c

File tree

8 files changed

+42
-0
lines changed

8 files changed

+42
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,9 @@ protected Object generateRandomInputValue(MappedFieldType ft) {
141141
assumeFalse("Test implemented in a follow up", true);
142142
return null;
143143
}
144+
145+
@Override
146+
protected boolean allowsNullValues() {
147+
return false; // TODO should this allow null values?
148+
}
144149
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ protected void checkIncomingMergeType(FieldMapper mergeWith) {
198198

199199
@Override
200200
protected void index(ParseContext context, Geometry geometry) throws IOException {
201+
if (geometry == null) {
202+
return;
203+
}
201204
context.doc().addAll(indexer.indexShape(geometry));
202205
createFieldNamesField(context);
203206
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,9 @@ String strategy() {
476476

477477
@Override
478478
protected void index(ParseContext context, ShapeBuilder<?, ?, ?> shapeBuilder) throws IOException {
479+
if (shapeBuilder == null) {
480+
return;
481+
}
479482
Shape shape = shapeBuilder.buildS4J();
480483
if (fieldType().pointsOnly()) {
481484
// index configured for pointsOnly

test/framework/src/main/java/org/elasticsearch/index/mapper/MapperTestCase.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,4 +691,19 @@ private BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> f
691691
.fielddataBuilder("test", lookupSource)
692692
.build(new IndexFieldDataCache.None(), new NoneCircuitBreakerService());
693693
}
694+
695+
public final void testNullInput() throws Exception {
696+
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
697+
if (allowsNullValues()) {
698+
ParsedDocument doc = mapper.parse(source(b -> b.nullField("field")));
699+
assertThat(doc.docs().get(0).getFields("field").length, equalTo(0));
700+
assertThat(doc.docs().get(0).getFields("_field_names").length, equalTo(0));
701+
} else {
702+
expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.nullField("field"))));
703+
}
704+
}
705+
706+
protected boolean allowsNullValues() {
707+
return true;
708+
}
694709
}

x-pack/plugin/mapper-constant-keyword/src/internalClusterTest/java/org/elasticsearch/xpack/constantkeyword/mapper/ConstantKeywordFieldMapperTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,9 @@ protected String generateRandomInputValue(MappedFieldType ft) {
172172
protected void randomFetchTestFieldConfig(XContentBuilder b) throws IOException {
173173
b.field("type", "constant_keyword").field("value", randomAlphaOfLengthBetween(1, 10));
174174
}
175+
176+
@Override
177+
protected boolean allowsNullValues() {
178+
return false; // null is an error for constant keyword
179+
}
175180
}

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ public GeoShapeWithDocValuesFieldMapper(String simpleName, MappedFieldType mappe
207207

208208
@Override
209209
protected void index(ParseContext context, Geometry geometry) throws IOException {
210+
if (geometry == null) {
211+
return;
212+
}
210213
List<IndexableField> fields = indexer.indexShape(geometry);
211214
if (fieldType().isSearchable()) {
212215
context.doc().addAll(fields);

x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/ShapeFieldMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public ShapeFieldMapper(String simpleName, MappedFieldType mappedFieldType,
137137

138138
@Override
139139
protected void index(ParseContext context, Geometry geometry) throws IOException {
140+
if (geometry == null) {
141+
return;
142+
}
140143
context.doc().addAll(indexer.indexShape(geometry));
141144
createFieldNamesField(context);
142145
}

x-pack/plugin/vectors/src/test/java/org/elasticsearch/xpack/vectors/mapper/DenseVectorFieldMapperTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,9 @@ protected Object generateRandomInputValue(MappedFieldType ft) {
160160
assumeFalse("Test implemented in a follow up", true);
161161
return null;
162162
}
163+
164+
@Override
165+
protected boolean allowsNullValues() {
166+
return false; // TODO should this allow null values?
167+
}
163168
}

0 commit comments

Comments
 (0)