Wildcard field - add support for null field with test (#57047) (#57139)

Backport of #57047
This commit is contained in:
markharwood 2020-05-26 16:07:49 +01:00 committed by GitHub
parent 56625e35b7
commit 1d74549d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -14,12 +14,16 @@ setup:
properties: properties:
my_wildcard: my_wildcard:
type: wildcard type: wildcard
null_wildcard:
type: wildcard
null_value: BLANK
- do: - do:
index: index:
index: test-index index: test-index
id: 1 id: 1
body: body:
my_wildcard: hello world my_wildcard: hello world
null_wildcard: null
- do: - do:
index: index:
index: test-index index: test-index
@ -32,6 +36,7 @@ setup:
id: 3 id: 3
body: body:
my_wildcard: cAsE iNsEnSiTiVe World my_wildcard: cAsE iNsEnSiTiVe World
null_wildcard: HAS_VALUE
- do: - do:
indices.refresh: {} indices.refresh: {}
@ -101,6 +106,19 @@ setup:
- match: {hits.total.value: 1} - 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": "Short suffix query":
- do: - do:

View File

@ -98,11 +98,12 @@ public class WildcardFieldMapper extends FieldMapper {
FIELD_TYPE.freeze(); FIELD_TYPE.freeze();
} }
public static final int IGNORE_ABOVE = Integer.MAX_VALUE; public static final int IGNORE_ABOVE = Integer.MAX_VALUE;
public static final String NULL_VALUE = null;
} }
public static class Builder extends FieldMapper.Builder<Builder> { public static class Builder extends FieldMapper.Builder<Builder> {
protected int ignoreAbove = Defaults.IGNORE_ABOVE; protected int ignoreAbove = Defaults.IGNORE_ABOVE;
protected String nullValue = Defaults.NULL_VALUE;
public Builder(String name) { public Builder(String name) {
super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE);
@ -188,7 +189,13 @@ public class WildcardFieldMapper extends FieldMapper {
Map.Entry<String, Object> entry = iterator.next(); Map.Entry<String, Object> entry = iterator.next();
String propName = entry.getKey(); String propName = entry.getKey();
Object propNode = entry.getValue(); 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)); builder.ignoreAbove(XContentMapValues.nodeIntegerValue(propNode, -1));
iterator.remove(); iterator.remove();
} }
@ -526,6 +533,9 @@ public class WildcardFieldMapper extends FieldMapper {
@Override @Override
protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException { protected void doXContentBody(XContentBuilder builder, boolean includeDefaults, Params params) throws IOException {
super.doXContentBody(builder, includeDefaults, params); super.doXContentBody(builder, includeDefaults, params);
if (includeDefaults || fieldType().nullValue() != null) {
builder.field("null_value", fieldType().nullValue());
}
if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) { if (includeDefaults || ignoreAbove != Defaults.IGNORE_ABOVE) {
builder.field("ignore_above", ignoreAbove); builder.field("ignore_above", ignoreAbove);
} }