Skip to content

Commit

Permalink
Add support for null field with test
Browse files Browse the repository at this point in the history
  • Loading branch information
markharwood committed May 26, 2020
1 parent cde2026 commit 37f3cc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ setup:
properties:
my_wildcard:
type: wildcard
null_wildcard:
type: wildcard
null_value: BLANK
- do:
index:
index: test-index
id: 1
body:
my_wildcard: hello world
null_wildcard: null
- do:
index:
index: test-index
Expand All @@ -32,6 +36,7 @@ setup:
id: 3
body:
my_wildcard: cAsE iNsEnSiTiVe World
null_wildcard: HAS_VALUE

- do:
indices.refresh: {}
Expand Down Expand Up @@ -101,6 +106,19 @@ setup:

- match: {hits.total.value: 1}

---
"null query":
- do:
search:
body:
track_total_hits: true
query:
wildcard:
null_wildcard: {value: "*BLANK*" }


- match: {hits.total.value: 1}

---
"Short suffix query":
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ public static class Defaults {
FIELD_TYPE.freeze();
}
public static final int IGNORE_ABOVE = Integer.MAX_VALUE;
public static final String NULL_VALUE = null;
}

public static class Builder extends FieldMapper.Builder<Builder> {
protected int ignoreAbove = Defaults.IGNORE_ABOVE;

protected String nullValue = Defaults.NULL_VALUE;

public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE);
Expand Down Expand Up @@ -188,7 +189,13 @@ public Mapper.Builder<?> parse(String name, Map<String, Object> node, ParserCont
Map.Entry<String, Object> entry = iterator.next();
String propName = entry.getKey();
Object propNode = entry.getValue();
if (propName.equals("ignore_above")) {
if (propName.equals("null_value")) {
if (propNode == null) {
throw new MapperParsingException("Property [null_value] cannot be null.");
}
builder.nullValue(propNode.toString());
iterator.remove();
} else if (propName.equals("ignore_above")) {
builder.ignoreAbove(XContentMapValues.nodeIntegerValue(propNode, -1));
iterator.remove();
}
Expand Down Expand Up @@ -526,6 +533,9 @@ public WildcardFieldType fieldType() {
@Override
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
super.doXContentBody(builder, includeDefaults, params);
if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", fieldType().nullValue());
}
if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) {
builder.field("ignore_above", ignoreAbove);
}
Expand Down

0 comments on commit 37f3cc7

Please sign in to comment.