Skip to content

Commit f9cd001

Browse files
committed
Add null-field checks to shape field mappers (#71999) (#72025)
#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 06eceff commit f9cd001

File tree

9 files changed

+47
-0
lines changed

9 files changed

+47
-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
@@ -145,4 +145,9 @@ protected Object generateRandomInputValue(MappedFieldType ft) {
145145
assumeFalse("Test implemented in a follow up", true);
146146
return null;
147147
}
148+
149+
@Override
150+
protected boolean allowsNullValues() {
151+
return false; // TODO should this allow null values?
152+
}
148153
}

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

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

189189
@Override
190190
protected void index(ParseContext context, Geometry geometry) throws IOException {
191+
if (geometry == null) {
192+
return;
193+
}
191194
context.doc().addAll(indexer.indexShape(geometry));
192195
createFieldNamesField(context);
193196
}

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

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

469469
@Override
470470
protected void index(ParseContext context, ShapeBuilder<?, ?, ?> shapeBuilder) throws IOException {
471+
if (shapeBuilder == null) {
472+
return;
473+
}
471474
Shape shape = shapeBuilder.buildS4J();
472475
if (fieldType().pointsOnly()) {
473476
// 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
@@ -684,4 +684,19 @@ private BiFunction<MappedFieldType, Supplier<SearchLookup>, IndexFieldData<?>> f
684684
.fielddataBuilder("test", lookupSource)
685685
.build(new IndexFieldDataCache.None(), new NoneCircuitBreakerService());
686686
}
687+
688+
public final void testNullInput() throws Exception {
689+
DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
690+
if (allowsNullValues()) {
691+
ParsedDocument doc = mapper.parse(source(b -> b.nullField("field")));
692+
assertThat(doc.docs().get(0).getFields("field").length, equalTo(0));
693+
assertThat(doc.docs().get(0).getFields("_field_names").length, equalTo(0));
694+
} else {
695+
expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.nullField("field"))));
696+
}
697+
}
698+
699+
protected boolean allowsNullValues() {
700+
return true;
701+
}
687702
}

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
@@ -177,4 +177,9 @@ protected String generateRandomInputValue(MappedFieldType ft) {
177177
protected void randomFetchTestFieldConfig(XContentBuilder b) throws IOException {
178178
b.field("type", "constant_keyword").field("value", randomAlphaOfLengthBetween(1, 10));
179179
}
180+
181+
@Override
182+
protected boolean allowsNullValues() {
183+
return false; // null is an error for constant keyword
184+
}
180185
}

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
@@ -195,6 +195,9 @@ public GeoShapeWithDocValuesFieldMapper(String simpleName, MappedFieldType mappe
195195

196196
@Override
197197
protected void index(ParseContext context, Geometry geometry) throws IOException {
198+
if (geometry == null) {
199+
return;
200+
}
198201
List<IndexableField> fields = indexer.indexShape(geometry);
199202
if (fieldType().isSearchable()) {
200203
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
@@ -125,6 +125,9 @@ public ShapeFieldMapper(String simpleName, MappedFieldType mappedFieldType,
125125

126126
@Override
127127
protected void index(ParseContext context, Geometry geometry) throws IOException {
128+
if (geometry == null) {
129+
return;
130+
}
128131
context.doc().addAll(indexer.indexShape(geometry));
129132
createFieldNamesField(context);
130133
}

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
@@ -166,4 +166,9 @@ protected Object generateRandomInputValue(MappedFieldType ft) {
166166
assumeFalse("Test implemented in a follow up", true);
167167
return null;
168168
}
169+
170+
@Override
171+
protected boolean allowsNullValues() {
172+
return false; // TODO should this allow null values?
173+
}
169174
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,9 @@ protected Object generateRandomInputValue(MappedFieldType ft) {
246246
assumeFalse("doesn't support docvalues_fetcher", true);
247247
return null;
248248
}
249+
250+
@Override
251+
protected boolean allowsNullValues() {
252+
return false;
253+
}
249254
}

0 commit comments

Comments
 (0)