From 1d74549d7f134f43dc2ea655b114361bb9934eec Mon Sep 17 00:00:00 2001 From: markharwood Date: Tue, 26 May 2020 16:07:49 +0100 Subject: [PATCH] Wildcard field - add support for null field with test (#57047) (#57139) Backport of #57047 --- .../test/wildcard/10_wildcard_basic.yml | 18 ++++++++++++++++++ .../wildcard/mapper/WildcardFieldMapper.java | 14 ++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/x-pack/plugin/src/test/resources/rest-api-spec/test/wildcard/10_wildcard_basic.yml b/x-pack/plugin/src/test/resources/rest-api-spec/test/wildcard/10_wildcard_basic.yml index 126f6f2b2c0..be3e37a63c6 100644 --- a/x-pack/plugin/src/test/resources/rest-api-spec/test/wildcard/10_wildcard_basic.yml +++ b/x-pack/plugin/src/test/resources/rest-api-spec/test/wildcard/10_wildcard_basic.yml @@ -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 @@ -32,6 +36,7 @@ setup: id: 3 body: my_wildcard: cAsE iNsEnSiTiVe World + null_wildcard: HAS_VALUE - do: indices.refresh: {} @@ -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: diff --git a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java index 00423520486..cbb68ab4900 100644 --- a/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java +++ b/x-pack/plugin/wildcard/src/main/java/org/elasticsearch/xpack/wildcard/mapper/WildcardFieldMapper.java @@ -98,11 +98,12 @@ public class WildcardFieldMapper extends FieldMapper { 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 { protected int ignoreAbove = Defaults.IGNORE_ABOVE; - + protected String nullValue = Defaults.NULL_VALUE; public Builder(String name) { super(name, Defaults.FIELD_TYPE, Defaults.FIELD_TYPE); @@ -188,7 +189,13 @@ public class WildcardFieldMapper extends FieldMapper { Map.Entry 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(); } @@ -526,6 +533,9 @@ public class WildcardFieldMapper extends FieldMapper { @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); }