Skip to content

Commit 00fe805

Browse files
kelChristoph Büscher
authored andcommitted
Ignore null value for range field (#27845) (#28116)
Currently when adding a document with a `null` value for a range field, the range field mapper raises an error. Instead we should ignore null like we do eg. with numbers or geo points. Closes #27845
1 parent 20aa4bc commit 00fe805

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

core/src/main/java/org/elasticsearch/index/mapper/RangeFieldMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
361361
} else {
362362
XContentParser parser = context.parser();
363363
final XContentParser.Token start = parser.currentToken();
364-
if (start == XContentParser.Token.START_OBJECT) {
364+
if (start == XContentParser.Token.VALUE_NULL) {
365+
return;
366+
} else if (start == XContentParser.Token.START_OBJECT) {
365367
RangeFieldType fieldType = fieldType();
366368
RangeType rangeType = fieldType.rangeType;
367369
String fieldName = null;

core/src/test/java/org/elasticsearch/index/mapper/RangeFieldMapperTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,15 @@ protected void doTestNullValue(String type) throws IOException {
363363
+ InetAddresses.toAddrString(InetAddresses.forString("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"));
364364
}
365365
assertThat(storedField.stringValue(), containsString(strVal));
366+
367+
// test null range
368+
doc = mapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
369+
.startObject()
370+
.nullField("field")
371+
.endObject()
372+
.bytes(),
373+
XContentType.JSON));
374+
assertNull(doc.rootDoc().get("field"));
366375
}
367376

368377
public void testNoBounds() throws Exception {

rest-api-spec/src/main/resources/rest-api-spec/test/range/10_basic.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ setup:
4545
id: 3
4646
body: { "integer_range" : { "gte": 4, "lte": 5 } }
4747

48+
- do:
49+
index:
50+
index: test
51+
type: doc
52+
id: 4
53+
body: { "integer_range" : null }
4854

4955
- do:
5056
indices.refresh: {}
@@ -73,6 +79,12 @@ setup:
7379

7480
- match: { hits.total: 0 }
7581

82+
- do:
83+
search:
84+
body: { "query" : { "match_all": {} } }
85+
86+
- match: { hits.total: 4 }
87+
7688
---
7789
"Long range":
7890

0 commit comments

Comments
 (0)