From 06516f652fa36ed0bf08c012517506dbae98aed4 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Fri, 15 Jan 2016 15:48:40 +0100 Subject: [PATCH 1/9] Add option to exclude based on paths in XContent --- .../common/xcontent/XContent.java | 6 + .../common/xcontent/XContentBuilder.java | 22 +++- .../common/xcontent/XContentGenerator.java | 3 + .../common/xcontent/cbor/CborXContent.java | 6 + .../xcontent/cbor/CborXContentGenerator.java | 6 + .../common/xcontent/json/JsonXContent.java | 5 + .../xcontent/json/JsonXContentGenerator.java | 13 +- .../common/xcontent/smile/SmileXContent.java | 5 + .../smile/SmileXContentGenerator.java | 6 + .../filtering/FilterPathBasedFilter.java | 43 ++++--- .../common/xcontent/yaml/YamlXContent.java | 6 + .../xcontent/yaml/YamlXContentGenerator.java | 5 + .../FilterPathGeneratorFilteringTests.java | 119 ++++++++++++------ 13 files changed, 189 insertions(+), 56 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java index 03e526ce8e5..5ee85c4df61 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java @@ -48,6 +48,11 @@ public interface XContent { */ XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException; + /** + * Creates a new generator using the provided output stream and some + * filters. + */ + XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException; /** * Creates a parser over the provided string content. */ @@ -77,4 +82,5 @@ public interface XContent { * Creates a parser over the provided reader. */ XContentParser createParser(Reader reader) throws IOException; + } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 6733a75db9c..3429dddcbc5 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -30,6 +30,7 @@ import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion; import org.joda.time.DateTimeZone; import org.joda.time.ReadableInstant; import org.joda.time.format.DateTimeFormatter; @@ -102,15 +103,28 @@ public final class XContentBuilder implements BytesStream, Releasable { } /** - * Constructs a new builder using the provided xcontent, an OutputStream and some filters. The - * filters are used to filter fields that won't be written to the OutputStream. Make sure - * to call {@link #close()} when the builder is done with. + * Constructs a new builder using the provided xcontent, an OutputStream and + * some filters. If filters are specified, only those values matching a + * filter will be written to the output stream. Make sure to call + * {@link #close()} when the builder is done with. */ public XContentBuilder(XContent xContent, OutputStream bos, String[] filters) throws IOException { this.bos = bos; this.generator = xContent.createGenerator(bos, filters); } + /** + * Constructs a new builder using the provided xcontent, an OutputStream and + * some filters. If filters are specified and inclusiveFilters is true, only + * those values matching a filter will be written to the output stream. If + * inclusiveFilters is false, those matching will be excluded. Make sure to + * call {@link #close()} when the builder is done with. + */ + public XContentBuilder(XContent xContent, OutputStream bos, boolean inclusiveFilters, String[] filters) throws IOException { + this.bos = bos; + this.generator = xContent.createGenerator(bos, filters, inclusiveFilters); + } + public XContentBuilder fieldCaseConversion(FieldCaseConversion fieldCaseConversion) { this.fieldCaseConversion = fieldCaseConversion; return this; @@ -158,7 +172,7 @@ public final class XContentBuilder implements BytesStream, Releasable { } public XContentBuilder startObject(String name, FieldCaseConversion conversion) throws IOException { - field(name, conversion); + field(name); startObject(); return this; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java index 11a42e37279..a69aae4253f 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java @@ -121,5 +121,8 @@ public interface XContentGenerator extends Closeable { void flush() throws IOException; + @Override void close() throws IOException; + + void writeStartObject(String name) throws IOException; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java index b4af0fdc78e..ac6d4f27b10 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java @@ -76,6 +76,11 @@ public class CborXContent implements XContent { return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); } + @Override + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { + return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + } + @Override public XContentParser createParser(String content) throws IOException { return new CborXContentParser(cborFactory.createParser(new FastStringReader(content))); @@ -108,4 +113,5 @@ public class CborXContent implements XContent { public XContentParser createParser(Reader reader) throws IOException { return new CborXContentParser(cborFactory.createParser(reader)); } + } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java index 517266b8170..13cd3060bcf 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java @@ -20,6 +20,8 @@ package org.elasticsearch.common.xcontent.cbor; import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.dataformat.cbor.CBORGenerator; + import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -34,6 +36,10 @@ public class CborXContentGenerator extends JsonXContentGenerator { super(jsonGenerator, os, filters); } + public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { + super(jsonGenerator, os, inclusiveFilters, filters); + } + @Override public XContentType contentType() { return XContentType.CBOR; diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java index 06e7fe8bd47..5d1ed6912fd 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java @@ -80,6 +80,11 @@ public class JsonXContent implements XContent { return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); } + @Override + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { + return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + } + @Override public XContentParser createParser(String content) throws IOException { return new JsonXContentParser(jsonFactory.createParser(new FastStringReader(content))); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index 8974021266e..97ce35915a4 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -72,6 +72,10 @@ public class JsonXContentGenerator implements XContentGenerator { private boolean prettyPrint = false; public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { + this(jsonGenerator, os, true, filters); + } + + public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String... filters) { if (jsonGenerator instanceof GeneratorBase) { this.base = (GeneratorBase) jsonGenerator; } else { @@ -82,7 +86,8 @@ public class JsonXContentGenerator implements XContentGenerator { this.generator = jsonGenerator; this.filter = null; } else { - this.filter = new FilteringGeneratorDelegate(jsonGenerator, new FilterPathBasedFilter(filters), true, true); + this.filter = new FilteringGeneratorDelegate(jsonGenerator, + new FilterPathBasedFilter(inclusiveFilters, filters), true, true); this.generator = this.filter; } @@ -375,6 +380,7 @@ public class JsonXContentGenerator implements XContentGenerator { } } + @Override public final void writeRawValue(BytesReference content) throws IOException { XContentType contentType = XContentFactory.xContentType(content); if (contentType == null) { @@ -450,4 +456,9 @@ public class JsonXContentGenerator implements XContentGenerator { } generator.close(); } + + @Override + public void writeStartObject(String name) throws IOException { + generator.writeObjectFieldStart(name); + } } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java index 27d6ee4626b..15a418b0c02 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java @@ -77,6 +77,11 @@ public class SmileXContent implements XContent { return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); } + @Override + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { + return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + } + @Override public XContentParser createParser(String content) throws IOException { return new SmileXContentParser(smileFactory.createParser(new FastStringReader(content))); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java index 451abab33d5..abfe95b91d1 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java @@ -20,6 +20,8 @@ package org.elasticsearch.common.xcontent.smile; import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.dataformat.smile.SmileGenerator; + import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -34,6 +36,10 @@ public class SmileXContentGenerator extends JsonXContentGenerator { super(jsonGenerator, os, filters); } + public SmileXContentGenerator(SmileGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { + super(jsonGenerator, os, inclusiveFilters, filters); + } + @Override public XContentType contentType() { return XContentType.SMILE; diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index d2f28611bae..1a537c7b7aa 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -31,25 +31,30 @@ public class FilterPathBasedFilter extends TokenFilter { * Marker value that should be used to indicate that a property name * or value matches one of the filter paths. */ - private static final TokenFilter MATCHING = new TokenFilter(){}; + private static final TokenFilter MATCHING = new TokenFilter() { + }; /** * Marker value that should be used to indicate that none of the * property names/values matches one of the filter paths. */ - private static final TokenFilter NO_MATCHING = new TokenFilter(){}; + private static final TokenFilter NO_MATCHING = new TokenFilter() { + }; private final FilterPath[] filters; - public FilterPathBasedFilter(FilterPath[] filters) { + private final boolean inclusive; + + public FilterPathBasedFilter(boolean inclusive, FilterPath[] filters) { + this.inclusive = inclusive; if (CollectionUtils.isEmpty(filters)) { throw new IllegalArgumentException("filters cannot be null or empty"); } this.filters = filters; } - public FilterPathBasedFilter(String[] filters) { - this(FilterPath.compile(filters)); + public FilterPathBasedFilter(boolean inclusive, String[] filters) { + this(inclusive, FilterPath.compile(filters)); } /** @@ -77,31 +82,39 @@ public class FilterPathBasedFilter extends TokenFilter { } if ((nextFilters != null) && (nextFilters.isEmpty() == false)) { - return new FilterPathBasedFilter(nextFilters.toArray(new FilterPath[nextFilters.size()])); + return new FilterPathBasedFilter(inclusive, nextFilters.toArray(new FilterPath[nextFilters.size()])); } } return NO_MATCHING; } + @Override public TokenFilter includeProperty(String name) { - TokenFilter include = evaluate(name, filters); - if (include == MATCHING) { - return TokenFilter.INCLUDE_ALL; + TokenFilter filter = evaluate(name, filters); + if (inclusive) { + if (filter == MATCHING) { + return TokenFilter.INCLUDE_ALL; + } else if (filter == NO_MATCHING) { + return null; + } + } else { + if (filter == MATCHING) { + return null; + } else if (filter == NO_MATCHING) { + return TokenFilter.INCLUDE_ALL; + } } - if (include == NO_MATCHING) { - return null; - } - return include; + return filter; } @Override protected boolean _includeScalar() { for (FilterPath filter : filters) { if (filter.matches()) { - return true; + return inclusive; } } - return false; + return !inclusive; } } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java index 994df37de30..3cd6416804c 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java @@ -75,6 +75,12 @@ public class YamlXContent implements XContent { return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); } + @Override + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { + return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + } + + @Override public XContentParser createParser(String content) throws IOException { return new YamlXContentParser(yamlFactory.createParser(new FastStringReader(content))); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java index dcb2155c82e..0c9ffc73762 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java @@ -20,6 +20,7 @@ package org.elasticsearch.common.xcontent.yaml; import com.fasterxml.jackson.core.JsonGenerator; + import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -34,6 +35,10 @@ public class YamlXContentGenerator extends JsonXContentGenerator { super(jsonGenerator, os, filters); } + public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { + super(jsonGenerator, os, inclusiveFilters, filters); + } + @Override public XContentType contentType() { return XContentType.YAML; diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index 0ca2eafa417..8f31eb093a3 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -34,56 +34,103 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { public void testFilters() throws Exception { final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; - assertResult(SAMPLE, "a", "{'a':0}"); - assertResult(SAMPLE, "b", "{'b':true}"); - assertResult(SAMPLE, "c", "{'c':'c_value'}"); - assertResult(SAMPLE, "d", "{'d':[0,1,2]}"); - assertResult(SAMPLE, "e", "{'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, "h", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "z", ""); + assertResult(SAMPLE, true, "a", "{'a':0}"); + assertResult(SAMPLE, true, "b", "{'b':true}"); + assertResult(SAMPLE, true, "c", "{'c':'c_value'}"); + assertResult(SAMPLE, true, "d", "{'d':[0,1,2]}"); + assertResult(SAMPLE, true, "e", "{'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, true, "h", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "z", ""); - assertResult(SAMPLE, "e.f1", "{'e':[{'f1':'f1_value'}]}"); - assertResult(SAMPLE, "e.f2", "{'e':[{'f2':'f2_value'}]}"); - assertResult(SAMPLE, "e.f*", "{'e':[{'f1':'f1_value','f2':'f2_value'}]}"); - assertResult(SAMPLE, "e.*2", "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); + assertResult(SAMPLE, true, "e.f1", "{'e':[{'f1':'f1_value'}]}"); + assertResult(SAMPLE, true, "e.f2", "{'e':[{'f2':'f2_value'}]}"); + assertResult(SAMPLE, true, "e.f*", "{'e':[{'f1':'f1_value','f2':'f2_value'}]}"); + assertResult(SAMPLE, true, "e.*2", "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); - assertResult(SAMPLE, "h.i", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "*.i", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "*.i", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "*.i.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "*.i.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.*.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "*.i.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.*.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "*.i.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.*.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.*.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.j.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "*.i.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.*.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.*.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.i.j.k.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "*.i.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.*.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.*.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.j.*.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.i.j.k.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "h.*.j.*.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "**.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "h.*.j.*.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, true, "**.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + + assertResult(SAMPLE, true, "**.*2", "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); + } + + public void testExclusiveFilters() throws Exception { + final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; + + assertResult(SAMPLE, false, "a", "{'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "b", "{'a':0,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "c", "{'a':0,'b':true,'d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "d", "{'a':0,'b':true,'c':'c_value','e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "e", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "h", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "z", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + + assertResult(SAMPLE, false, "e.f1", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "e.f2", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "e.f*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, false, "e.*2", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + + assertResult(SAMPLE, false, "h.i", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.j", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.j.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.j.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + + assertResult(SAMPLE, false, "h.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "*.i", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + + assertResult(SAMPLE, false, "*.i.j", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.*.j", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + + assertResult(SAMPLE, false, "*.i.j.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.*.j.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.*.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.j.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + + assertResult(SAMPLE, false, "*.i.j.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.*.j.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.*.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.j.*.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "h.i.j.k.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + + assertResult(SAMPLE, false, "h.*.j.*.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, false, "**.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + + assertResult(SAMPLE, false, "**.*2", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, "**.*2", "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); } public void testFiltersWithDots() throws Exception { - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b.c", "{'b':{'c':'c_value'}}"); - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b\\.c", "{'b.c':'value'}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b.c", "{'b':{'c':'c_value'}}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b\\.c", "{'b.c':'value'}"); } - private void assertResult(String input, String filter, String expected) throws Exception { + private void assertResult(String input, boolean inclusive, String filter, String expected) throws Exception { try (BytesStreamOutput os = new BytesStreamOutput()) { - try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), new FilterPathBasedFilter(new String[]{filter}), true, true)) { + try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), + new FilterPathBasedFilter(inclusive, new String[] { filter }), true, true)) { try (JsonParser parser = JSON_FACTORY.createParser(replaceQuotes(input))) { while (parser.nextToken() != null) { generator.copyCurrentStructure(parser); From c13ed81d5aaa552cb95b1cde76ede2d9ed1457fe Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Fri, 15 Jan 2016 17:47:55 +0100 Subject: [PATCH 2/9] Changes after review --- .../common/xcontent/XContent.java | 18 ++++++++++++++---- .../common/xcontent/XContentBuilder.java | 6 ++---- .../common/xcontent/XContentGenerator.java | 2 -- .../common/xcontent/cbor/CborXContent.java | 10 ---------- .../xcontent/cbor/CborXContentGenerator.java | 7 +++---- .../common/xcontent/json/JsonXContent.java | 14 ++------------ .../xcontent/json/JsonXContentGenerator.java | 4 ---- .../common/xcontent/smile/SmileXContent.java | 10 ---------- .../xcontent/smile/SmileXContentGenerator.java | 7 +++---- .../filtering/FilterPathBasedFilter.java | 2 +- .../common/xcontent/yaml/YamlXContent.java | 11 ----------- .../xcontent/yaml/YamlXContentGenerator.java | 6 +++--- .../FilterPathGeneratorFilteringTests.java | 9 +++++++-- 13 files changed, 35 insertions(+), 71 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java index 5ee85c4df61..35579965f30 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContent.java @@ -41,18 +41,28 @@ public interface XContent { /** * Creates a new generator using the provided output stream. */ - XContentGenerator createGenerator(OutputStream os) throws IOException; + default XContentGenerator createGenerator(OutputStream os) throws IOException { + return createGenerator(os, null, true); + } /** - * Creates a new generator using the provided output stream and some filters. + * Creates a new generator using the provided output stream and some + * inclusive filters. Same as createGenerator(os, filters, true). */ - XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException; + default XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { + return createGenerator(os, filters, true); + } /** * Creates a new generator using the provided output stream and some * filters. + * + * @param inclusive + * If true only paths matching a filter will be included in + * output. If false no path matching a filter will be included in + * output */ - XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException; + XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException; /** * Creates a parser over the provided string content. */ diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 3429dddcbc5..1776799c4ab 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -30,7 +30,6 @@ import org.elasticsearch.common.lease.Releasable; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.unit.ByteSizeValue; import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.XContentBuilder.FieldCaseConversion; import org.joda.time.DateTimeZone; import org.joda.time.ReadableInstant; import org.joda.time.format.DateTimeFormatter; @@ -109,8 +108,7 @@ public final class XContentBuilder implements BytesStream, Releasable { * {@link #close()} when the builder is done with. */ public XContentBuilder(XContent xContent, OutputStream bos, String[] filters) throws IOException { - this.bos = bos; - this.generator = xContent.createGenerator(bos, filters); + this(xContent, bos, true, filters); } /** @@ -172,7 +170,7 @@ public final class XContentBuilder implements BytesStream, Releasable { } public XContentBuilder startObject(String name, FieldCaseConversion conversion) throws IOException { - field(name); + field(name, conversion); startObject(); return this; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java index a69aae4253f..e11fc42b5a8 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentGenerator.java @@ -123,6 +123,4 @@ public interface XContentGenerator extends Closeable { @Override void close() throws IOException; - - void writeStartObject(String name) throws IOException; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java index ac6d4f27b10..cecdb5a9c86 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java @@ -66,16 +66,6 @@ public class CborXContent implements XContent { throw new ElasticsearchParseException("cbor does not support stream parsing..."); } - @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java index 13cd3060bcf..bf71fc9dc7a 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.xcontent.cbor; import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.dataformat.cbor.CBORGenerator; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -33,11 +32,11 @@ import java.io.OutputStream; public class CborXContentGenerator extends JsonXContentGenerator { public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - super(jsonGenerator, os, filters); + this(jsonGenerator, os, true, filters); } - public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { - super(jsonGenerator, os, inclusiveFilters, filters); + public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { + super(jsonGenerator, os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java index 5d1ed6912fd..f29604eb61f 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java @@ -71,18 +71,8 @@ public class JsonXContent implements XContent { } @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { + return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index 97ce35915a4..7af14f25fcd 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -457,8 +457,4 @@ public class JsonXContentGenerator implements XContentGenerator { generator.close(); } - @Override - public void writeStartObject(String name) throws IOException { - generator.writeObjectFieldStart(name); - } } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java index 15a418b0c02..285314e9b89 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java @@ -67,16 +67,6 @@ public class SmileXContent implements XContent { return (byte) 0xFF; } - @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java index abfe95b91d1..f479102b8ae 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.xcontent.smile; import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.dataformat.smile.SmileGenerator; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContentGenerator; @@ -33,11 +32,11 @@ import java.io.OutputStream; public class SmileXContentGenerator extends JsonXContentGenerator { public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - super(jsonGenerator, os, filters); + this(jsonGenerator, os, true, filters); } - public SmileXContentGenerator(SmileGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { - super(jsonGenerator, os, inclusiveFilters, filters); + public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { + super(jsonGenerator, os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index 1a537c7b7aa..03e0122e9d2 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -46,10 +46,10 @@ public class FilterPathBasedFilter extends TokenFilter { private final boolean inclusive; public FilterPathBasedFilter(boolean inclusive, FilterPath[] filters) { - this.inclusive = inclusive; if (CollectionUtils.isEmpty(filters)) { throw new IllegalArgumentException("filters cannot be null or empty"); } + this.inclusive = inclusive; this.filters = filters; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java index 3cd6416804c..ff4d805cd88 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java @@ -65,22 +65,11 @@ public class YamlXContent implements XContent { throw new ElasticsearchParseException("yaml does not support stream parsing..."); } - @Override - public XContentGenerator createGenerator(OutputStream os) throws IOException { - return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os); - } - - @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters) throws IOException { - return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, filters); - } - @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); } - @Override public XContentParser createParser(String content) throws IOException { return new YamlXContentParser(yamlFactory.createParser(new FastStringReader(content))); diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java index 0c9ffc73762..9e2f5b7cbbd 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java @@ -32,11 +32,11 @@ import java.io.OutputStream; public class YamlXContentGenerator extends JsonXContentGenerator { public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - super(jsonGenerator, os, filters); + this(jsonGenerator, os, true, filters); } - public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String[] filters) { - super(jsonGenerator, os, inclusiveFilters, filters); + public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { + super(jsonGenerator, os, inclusive, filters); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index 8f31eb093a3..ba5301c4160 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -31,7 +31,7 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { private final JsonFactory JSON_FACTORY = new JsonFactory(); - public void testFilters() throws Exception { + public void testInclusiveFilters() throws Exception { final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; assertResult(SAMPLE, true, "a", "{'a':0}"); @@ -122,11 +122,16 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { } - public void testFiltersWithDots() throws Exception { + public void testInclusiveFiltersWithDots() throws Exception { assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b.c", "{'b':{'c':'c_value'}}"); assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b\\.c", "{'b.c':'value'}"); } + public void testExclusiveFiltersWithDots() throws Exception { + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", false, "b.c", "{'a':0,'b.c':'value'}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", false, "b\\.c", "{'a':0,'b':{'c':'c_value'}}"); + } + private void assertResult(String input, boolean inclusive, String filter, String expected) throws Exception { try (BytesStreamOutput os = new BytesStreamOutput()) { try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), From 68742e37548109c995a9199a45253170b7719812 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Fri, 15 Jan 2016 17:59:27 +0100 Subject: [PATCH 3/9] Renamed parameter --- .../common/xcontent/json/JsonXContentGenerator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index 7af14f25fcd..1560ce701ba 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -75,7 +75,7 @@ public class JsonXContentGenerator implements XContentGenerator { this(jsonGenerator, os, true, filters); } - public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusiveFilters, String... filters) { + public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String... filters) { if (jsonGenerator instanceof GeneratorBase) { this.base = (GeneratorBase) jsonGenerator; } else { @@ -87,7 +87,7 @@ public class JsonXContentGenerator implements XContentGenerator { this.filter = null; } else { this.filter = new FilteringGeneratorDelegate(jsonGenerator, - new FilterPathBasedFilter(inclusiveFilters, filters), true, true); + new FilterPathBasedFilter(inclusive, filters), true, true); this.generator = this.filter; } From a0d8d656fc7138f6a4d863beff791084c6dc5196 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Thu, 28 Jan 2016 16:12:57 +0100 Subject: [PATCH 4/9] Added exclusive filter tests to AbstractFilteringJsonGeneratorTestCase --- .../common/xcontent/XContentBuilder.java | 4 + ...bstractFilteringJsonGeneratorTestCase.java | 808 ++++++++++++++++-- 2 files changed, 748 insertions(+), 64 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 1776799c4ab..13e45af1d37 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -83,6 +83,10 @@ public final class XContentBuilder implements BytesStream, Releasable { return new XContentBuilder(xContent, new BytesStreamOutput(), filters); } + public static XContentBuilder builder(XContent xContent, boolean inclusive, String[] filters) throws IOException { + return new XContentBuilder(xContent, new BytesStreamOutput(), inclusive, filters); + } + private XContentGenerator generator; private final OutputStream bos; diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java index ed7aee33eba..13739473b16 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java @@ -83,8 +83,8 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase } } - private XContentBuilder newXContentBuilder(String... filters) throws IOException { - return XContentBuilder.builder(getXContentType().xContent(), filters); + private XContentBuilder newXContentBuilder(boolean inclusive, String... filters) throws IOException { + return XContentBuilder.builder(getXContentType().xContent(), inclusive, filters); } /** @@ -167,34 +167,117 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase } /** - * Instanciates a new XContentBuilder with the given filters and builds a sample with it. + * Instanciates a new XContentBuilder with the given filters and builds a + * sample with it. + * + * @param inclusive + * Specifies if filters are inclusive or exclusive */ - private XContentBuilder sample(String... filters) throws IOException { - return sample(newXContentBuilder(filters)); + private XContentBuilder sample(boolean inclusive, String... filters) throws IOException { + return sample(newXContentBuilder(inclusive, filters)); } public void testNoFiltering() throws Exception { - XContentBuilder expected = sample(); + XContentBuilder expected = sample(true); - assertXContentBuilder(expected, sample()); - assertXContentBuilder(expected, sample("*")); - assertXContentBuilder(expected, sample("**")); + assertXContentBuilder(expected, sample(true)); + assertXContentBuilder(expected, sample(true, "*")); + assertXContentBuilder(expected, sample(true, "**")); + assertXContentBuilder(expected, sample(false, "xyz")); } public void testNoMatch() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject().endObject(); + XContentBuilder expected = newXContentBuilder(true).startObject().endObject(); - assertXContentBuilder(expected, sample("xyz")); + assertXContentBuilder(expected, sample(true, "xyz")); + assertXContentBuilder(expected, sample(false, "*")); + assertXContentBuilder(expected, sample(false, "**")); } - public void testSimpleField() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject().field("title", "My awesome book").endObject(); + public void testSimpleFieldInclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject().field("title", "My awesome book").endObject(); - assertXContentBuilder(expected, sample("title")); + assertXContentBuilder(expected, sample(true, "title")); } - public void testSimpleFieldWithWildcard() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testSimpleFieldExclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "title")); + } + + + public void testSimpleFieldWithWildcardInclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .field("price", 27.99) .startObject("properties") .field("weight", 0.8d) @@ -246,31 +329,207 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample("pr*")); + assertXContentBuilder(expected, sample(true, "pr*")); } - public void testMultipleFields() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testSimpleFieldWithWildcardExclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .endObject(); + + assertXContentBuilder(expected, sample(false, "pr*")); + } + + public void testMultipleFieldsInclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .field("title", "My awesome book") .field("pages", 456) .endObject(); - assertXContentBuilder(expected, sample("title", "pages")); + assertXContentBuilder(expected, sample(true, "title", "pages")); } - public void testSimpleArray() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testMultipleFieldsExclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "title", "pages")); + } + + + public void testSimpleArrayInclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startArray("tags") .value("elasticsearch") .value("java") .endArray() .endObject(); - assertXContentBuilder(expected, sample("tags")); + assertXContentBuilder(expected, sample(true, "tags")); } - public void testSimpleArrayOfObjects() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testSimpleArrayExclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + + assertXContentBuilder(expected, sample(false, "tags")); + } + + + public void testSimpleArrayOfObjectsInclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startArray("authors") .startObject() .field("name", "John Doe") @@ -285,13 +544,79 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endArray() .endObject(); - assertXContentBuilder(expected, sample("authors")); - assertXContentBuilder(expected, sample("authors.*")); - assertXContentBuilder(expected, sample("authors.*name")); + assertXContentBuilder(expected, sample(true, "authors")); + assertXContentBuilder(expected, sample(true, "authors.*")); + assertXContentBuilder(expected, sample(true, "authors.*name")); } - public void testSimpleArrayOfObjectsProperty() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testSimpleArrayOfObjectsExclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "authors")); + assertXContentBuilder(expected, sample(false, "authors.*")); + assertXContentBuilder(expected, sample(false, "authors.*name")); + } + + public void testSimpleArrayOfObjectsPropertyInclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startArray("authors") .startObject() .field("lastname", "John") @@ -302,12 +627,87 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endArray() .endObject(); - assertXContentBuilder(expected, sample("authors.lastname")); - assertXContentBuilder(expected, sample("authors.l*")); + assertXContentBuilder(expected, sample(true, "authors.lastname")); + assertXContentBuilder(expected, sample(true, "authors.l*")); } - public void testRecurseField1() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testSimpleArrayOfObjectsPropertyExclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "authors.lastname")); + assertXContentBuilder(expected, sample(false, "authors.l*")); + } + + public void testRecurseField1Inclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startArray("authors") .startObject() .field("name", "John Doe") @@ -355,11 +755,74 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample("**.name")); + assertXContentBuilder(expected, sample(true, "**.name")); } - public void testRecurseField2() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testRecurseField1Exclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "**.name")); + } + + public void testRecurseField2Inclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startObject("properties") .startObject("language") .startObject("en") @@ -399,11 +862,77 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample("properties.**.name")); + assertXContentBuilder(expected, sample(true, "properties.**.name")); } - public void testRecurseField3() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testRecurseField2Exclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "properties.**.name")); + } + + + public void testRecurseField3Inclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startObject("properties") .startObject("language") .startObject("en") @@ -428,11 +957,82 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample("properties.*.en.**.name")); + assertXContentBuilder(expected, sample(true, "properties.*.en.**.name")); } - public void testRecurseField4() throws Exception { - XContentBuilder expected = newXContentBuilder().startObject() + public void testRecurseField3Exclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "properties.*.en.**.name")); + } + + + public void testRecurseField4Inclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() .startObject("properties") .startObject("language") .startObject("en") @@ -459,47 +1059,127 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample("properties.**.distributors.name")); + assertXContentBuilder(expected, sample(true, "properties.**.distributors.name")); + } + + public void testRecurseField4Exclusive() throws Exception { + XContentBuilder expected = newXContentBuilder(true).startObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); + + assertXContentBuilder(expected, sample(false, "properties.**.distributors.name")); } public void testRawField() throws Exception { - XContentBuilder expectedRawField = newXContentBuilder().startObject().field("foo", 0).startObject("raw").field("content", "hello world!").endObject().endObject(); - XContentBuilder expectedRawFieldFiltered = newXContentBuilder().startObject().field("foo", 0).endObject(); - XContentBuilder expectedRawFieldNotFiltered =newXContentBuilder().startObject().startObject("raw").field("content", "hello world!").endObject().endObject(); + XContentBuilder expectedRawField = newXContentBuilder(true).startObject().field("foo", 0).startObject("raw").field("content", "hello world!").endObject().endObject(); + XContentBuilder expectedRawFieldFiltered = newXContentBuilder(true).startObject().field("foo", 0).endObject(); + XContentBuilder expectedRawFieldNotFiltered = newXContentBuilder(true).startObject().startObject("raw").field("content", "hello world!").endObject().endObject(); - BytesReference raw = newXContentBuilder().startObject().field("content", "hello world!").endObject().bytes(); + BytesReference raw = newXContentBuilder(true).startObject().field("content", "hello world!").endObject().bytes(); // Test method: rawField(String fieldName, BytesReference content) - assertXContentBuilder(expectedRawField, newXContentBuilder().startObject().field("foo", 0).rawField("raw", raw).endObject()); - assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder("f*").startObject().field("foo", 0).rawField("raw", raw).endObject()); - assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder("r*").startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawField, newXContentBuilder(true).startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(true, "f*").startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(false, "r*").startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(true, "r*").startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(false, "f*").startObject().field("foo", 0).rawField("raw", raw).endObject()); // Test method: rawField(String fieldName, InputStream content) - assertXContentBuilder(expectedRawField, newXContentBuilder().startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); - assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder("f*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); - assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder("r*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawField, newXContentBuilder(true).startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(true, "f*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(false, "r*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(true, "r*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(false, "f*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); } + public void testArrays() throws Exception { // Test: Array of values (no filtering) - XContentBuilder expected = newXContentBuilder().startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject(); - assertXContentBuilder(expected, newXContentBuilder("t*").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder("tags").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + XContentBuilder expected = newXContentBuilder(true).startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject(); + assertXContentBuilder(expected, newXContentBuilder(true, "t*").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder(true, "tags").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder(false, "a").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); // Test: Array of values (with filtering) - assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilder("foo").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(true, "foo").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "t*").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "tags").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); // Test: Array of objects (no filtering) - expected = newXContentBuilder().startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject(); - assertXContentBuilder(expected, newXContentBuilder("t*").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder("tags").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + expected = newXContentBuilder(true).startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject(); + assertXContentBuilder(expected, newXContentBuilder(true, "t*").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder(true, "tags").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder(false, "a").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); // Test: Array of objects (with filtering) - assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilder("foo").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(true, "foo").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "t*").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "tags").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); // Test: Array of objects (with partial filtering) - expected = newXContentBuilder().startObject().startArray("tags").startObject().field("firstname", "ipsum").endObject().endArray().endObject(); - assertXContentBuilder(expected, newXContentBuilder("t*.firstname").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + expected = newXContentBuilder(true).startObject().startArray("tags").startObject().field("firstname", "ipsum").endObject().endArray().endObject(); + assertXContentBuilder(expected, newXContentBuilder(true, "t*.firstname").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder(false, "t*.lastname").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); } } From db4c6c96a3aa904005fc1985548cb0f8061e9201 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Wed, 3 Feb 2016 11:13:12 +0100 Subject: [PATCH 5/9] Minor refactoring and indentation fixes --- .../common/xcontent/XContentBuilder.java | 2 +- .../common/xcontent/smile/SmileXContent.java | 4 +- .../filtering/FilterPathBasedFilter.java | 17 +-- ...bstractFilteringJsonGeneratorTestCase.java | 135 +++++++++--------- 4 files changed, 75 insertions(+), 83 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 13e45af1d37..95cafaae0d0 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -83,7 +83,7 @@ public final class XContentBuilder implements BytesStream, Releasable { return new XContentBuilder(xContent, new BytesStreamOutput(), filters); } - public static XContentBuilder builder(XContent xContent, boolean inclusive, String[] filters) throws IOException { + public static XContentBuilder builder(XContent xContent, String[] filters, boolean inclusive) throws IOException { return new XContentBuilder(xContent, new BytesStreamOutput(), inclusive, filters); } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java index 285314e9b89..5340d34392d 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java @@ -68,8 +68,8 @@ public class SmileXContent implements XContent { } @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { - return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { + return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusive, filters); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index 03e0122e9d2..b55d426d822 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -92,18 +92,11 @@ public class FilterPathBasedFilter extends TokenFilter { @Override public TokenFilter includeProperty(String name) { TokenFilter filter = evaluate(name, filters); - if (inclusive) { - if (filter == MATCHING) { - return TokenFilter.INCLUDE_ALL; - } else if (filter == NO_MATCHING) { - return null; - } - } else { - if (filter == MATCHING) { - return null; - } else if (filter == NO_MATCHING) { - return TokenFilter.INCLUDE_ALL; - } + if (filter == MATCHING) { + return inclusive ? TokenFilter.INCLUDE_ALL : null; + } + if (filter == NO_MATCHING) { + return inclusive ? null : TokenFilter.INCLUDE_ALL; } return filter; } diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java index 13739473b16..d237c3bca4c 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java @@ -84,7 +84,7 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase } private XContentBuilder newXContentBuilder(boolean inclusive, String... filters) throws IOException { - return XContentBuilder.builder(getXContentType().xContent(), inclusive, filters); + return XContentBuilder.builder(getXContentType().xContent(), filters, inclusive); } /** @@ -456,73 +456,72 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleArrayExclusive() throws Exception { XContentBuilder expected = newXContentBuilder(true).startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") - .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() - .endObject() - .endObject() - .endObject(); - + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); assertXContentBuilder(expected, sample(false, "tags")); } From 0dfb55d72c6171ba689abd9aa6ad13f03359fa08 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Wed, 3 Feb 2016 12:11:27 +0100 Subject: [PATCH 6/9] exclude{Filters}, filters => filters, exclude --- .../common/xcontent/XContentBuilder.java | 8 +- .../common/xcontent/cbor/CborXContent.java | 4 +- .../xcontent/cbor/CborXContentGenerator.java | 6 +- .../common/xcontent/json/JsonXContent.java | 2 +- .../xcontent/json/JsonXContentGenerator.java | 4 +- .../common/xcontent/smile/SmileXContent.java | 2 +- .../smile/SmileXContentGenerator.java | 6 +- .../filtering/FilterPathBasedFilter.java | 6 +- .../common/xcontent/yaml/YamlXContent.java | 4 +- .../xcontent/yaml/YamlXContentGenerator.java | 6 +- ...bstractFilteringJsonGeneratorTestCase.java | 228 +++++++++++------- .../FilterPathGeneratorFilteringTests.java | 138 +++++------ 12 files changed, 229 insertions(+), 185 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index 95cafaae0d0..fe11cec5ba9 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -84,7 +84,7 @@ public final class XContentBuilder implements BytesStream, Releasable { } public static XContentBuilder builder(XContent xContent, String[] filters, boolean inclusive) throws IOException { - return new XContentBuilder(xContent, new BytesStreamOutput(), inclusive, filters); + return new XContentBuilder(xContent, new BytesStreamOutput(), filters, inclusive); } private XContentGenerator generator; @@ -112,7 +112,7 @@ public final class XContentBuilder implements BytesStream, Releasable { * {@link #close()} when the builder is done with. */ public XContentBuilder(XContent xContent, OutputStream bos, String[] filters) throws IOException { - this(xContent, bos, true, filters); + this(xContent, bos, filters, true); } /** @@ -122,9 +122,9 @@ public final class XContentBuilder implements BytesStream, Releasable { * inclusiveFilters is false, those matching will be excluded. Make sure to * call {@link #close()} when the builder is done with. */ - public XContentBuilder(XContent xContent, OutputStream bos, boolean inclusiveFilters, String[] filters) throws IOException { + public XContentBuilder(XContent xContent, OutputStream bos, String[] filters, boolean inclusive) throws IOException { this.bos = bos; - this.generator = xContent.createGenerator(bos, filters, inclusiveFilters); + this.generator = xContent.createGenerator(bos, filters, inclusive); } public XContentBuilder fieldCaseConversion(FieldCaseConversion fieldCaseConversion) { diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java index cecdb5a9c86..bc2bc81a2c0 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContent.java @@ -67,8 +67,8 @@ public class CborXContent implements XContent { } @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { - return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { + return new CborXContentGenerator(cborFactory.createGenerator(os, JsonEncoding.UTF8), os, filters, inclusive); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java index bf71fc9dc7a..9ec690f2d1f 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/cbor/CborXContentGenerator.java @@ -32,11 +32,11 @@ import java.io.OutputStream; public class CborXContentGenerator extends JsonXContentGenerator { public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - this(jsonGenerator, os, true, filters); + this(jsonGenerator, os, filters, true); } - public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { - super(jsonGenerator, os, inclusive, filters); + public CborXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String[] filters, boolean inclusive) { + super(jsonGenerator, os, filters, inclusive); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java index f29604eb61f..ee730f10b2a 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContent.java @@ -72,7 +72,7 @@ public class JsonXContent implements XContent { @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { - return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusive, filters); + return new JsonXContentGenerator(jsonFactory.createGenerator(os, JsonEncoding.UTF8), os, filters, inclusive); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index 1560ce701ba..c7eb38b5f33 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -72,10 +72,10 @@ public class JsonXContentGenerator implements XContentGenerator { private boolean prettyPrint = false; public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - this(jsonGenerator, os, true, filters); + this(jsonGenerator, os, filters, true); } - public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String... filters) { + public JsonXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String[] filters, boolean inclusive) { if (jsonGenerator instanceof GeneratorBase) { this.base = (GeneratorBase) jsonGenerator; } else { diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java index 5340d34392d..fc12e40f5a3 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContent.java @@ -69,7 +69,7 @@ public class SmileXContent implements XContent { @Override public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { - return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusive, filters); + return new SmileXContentGenerator(smileFactory.createGenerator(os, JsonEncoding.UTF8), os, filters, inclusive); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java index f479102b8ae..ac294c1db85 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/smile/SmileXContentGenerator.java @@ -32,11 +32,11 @@ import java.io.OutputStream; public class SmileXContentGenerator extends JsonXContentGenerator { public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - this(jsonGenerator, os, true, filters); + this(jsonGenerator, os, filters, true); } - public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { - super(jsonGenerator, os, inclusive, filters); + public SmileXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String[] filters, boolean inclusive) { + super(jsonGenerator, os, filters, inclusive); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index b55d426d822..f6a81e6fa3b 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -45,7 +45,7 @@ public class FilterPathBasedFilter extends TokenFilter { private final boolean inclusive; - public FilterPathBasedFilter(boolean inclusive, FilterPath[] filters) { + public FilterPathBasedFilter(FilterPath[] filters, boolean inclusive) { if (CollectionUtils.isEmpty(filters)) { throw new IllegalArgumentException("filters cannot be null or empty"); } @@ -54,7 +54,7 @@ public class FilterPathBasedFilter extends TokenFilter { } public FilterPathBasedFilter(boolean inclusive, String[] filters) { - this(inclusive, FilterPath.compile(filters)); + this(FilterPath.compile(filters), inclusive); } /** @@ -82,7 +82,7 @@ public class FilterPathBasedFilter extends TokenFilter { } if ((nextFilters != null) && (nextFilters.isEmpty() == false)) { - return new FilterPathBasedFilter(inclusive, nextFilters.toArray(new FilterPath[nextFilters.size()])); + return new FilterPathBasedFilter(nextFilters.toArray(new FilterPath[nextFilters.size()]), inclusive); } } return NO_MATCHING; diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java index ff4d805cd88..012781cca3f 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContent.java @@ -66,8 +66,8 @@ public class YamlXContent implements XContent { } @Override - public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusiveFilters) throws IOException { - return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, inclusiveFilters, filters); + public XContentGenerator createGenerator(OutputStream os, String[] filters, boolean inclusive) throws IOException { + return new YamlXContentGenerator(yamlFactory.createGenerator(os, JsonEncoding.UTF8), os, filters, inclusive); } @Override diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java index 9e2f5b7cbbd..f801401a229 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/yaml/YamlXContentGenerator.java @@ -32,11 +32,11 @@ import java.io.OutputStream; public class YamlXContentGenerator extends JsonXContentGenerator { public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String... filters) { - this(jsonGenerator, os, true, filters); + this(jsonGenerator, os, filters, true); } - public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, boolean inclusive, String[] filters) { - super(jsonGenerator, os, inclusive, filters); + public YamlXContentGenerator(JsonGenerator jsonGenerator, OutputStream os, String[] filters, boolean inclusive) { + super(jsonGenerator, os, filters, inclusive); } @Override diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java index d237c3bca4c..c060e6c6246 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java @@ -83,7 +83,15 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase } } - private XContentBuilder newXContentBuilder(boolean inclusive, String... filters) throws IOException { + private XContentBuilder newXContentBuilder() throws IOException { + return XContentBuilder.builder(getXContentType().xContent()); + } + + private XContentBuilder newXContentBuilder(String filter, boolean inclusive) throws IOException { + return XContentBuilder.builder(getXContentType().xContent(), new String[] { filter }, inclusive); + } + + private XContentBuilder newXContentBuilder(String[] filters, boolean inclusive) throws IOException { return XContentBuilder.builder(getXContentType().xContent(), filters, inclusive); } @@ -169,39 +177,46 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase /** * Instanciates a new XContentBuilder with the given filters and builds a * sample with it. - * * @param inclusive * Specifies if filters are inclusive or exclusive */ - private XContentBuilder sample(boolean inclusive, String... filters) throws IOException { - return sample(newXContentBuilder(inclusive, filters)); + private XContentBuilder sample(String filter, boolean inclusive) throws IOException { + return sample(newXContentBuilder(filter, inclusive)); + } + + private XContentBuilder sample(String[] filters, boolean inclusive) throws IOException { + return sample(newXContentBuilder(filters, inclusive)); + } + + private XContentBuilder sample() throws IOException { + return sample(newXContentBuilder()); } public void testNoFiltering() throws Exception { - XContentBuilder expected = sample(true); + XContentBuilder expected = sample(); - assertXContentBuilder(expected, sample(true)); - assertXContentBuilder(expected, sample(true, "*")); - assertXContentBuilder(expected, sample(true, "**")); - assertXContentBuilder(expected, sample(false, "xyz")); + assertXContentBuilder(expected, sample()); + assertXContentBuilder(expected, sample("*", true)); + assertXContentBuilder(expected, sample("**", true)); + assertXContentBuilder(expected, sample("xyz", false)); } public void testNoMatch() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject().endObject(); + XContentBuilder expected = newXContentBuilder().startObject().endObject(); - assertXContentBuilder(expected, sample(true, "xyz")); - assertXContentBuilder(expected, sample(false, "*")); - assertXContentBuilder(expected, sample(false, "**")); + assertXContentBuilder(expected, sample("xyz", true)); + assertXContentBuilder(expected, sample("*", false)); + assertXContentBuilder(expected, sample("**", false)); } public void testSimpleFieldInclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject().field("title", "My awesome book").endObject(); + XContentBuilder expected = newXContentBuilder().startObject().field("title", "My awesome book").endObject(); - assertXContentBuilder(expected, sample(true, "title")); + assertXContentBuilder(expected, sample("title", true)); } public void testSimpleFieldExclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("pages", 456) .field("price", 27.99) .field("timestamp", 1428582942867L) @@ -272,12 +287,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "title")); + assertXContentBuilder(expected, sample("title", false)); } public void testSimpleFieldWithWildcardInclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("price", 27.99) .startObject("properties") .field("weight", 0.8d) @@ -329,11 +344,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(true, "pr*")); + assertXContentBuilder(expected, sample("pr*", true)); } public void testSimpleFieldWithWildcardExclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("timestamp", 1428582942867L) @@ -356,20 +371,20 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endArray() .endObject(); - assertXContentBuilder(expected, sample(false, "pr*")); + assertXContentBuilder(expected, sample("pr*", false)); } public void testMultipleFieldsInclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .endObject(); - assertXContentBuilder(expected, sample(true, "title", "pages")); + assertXContentBuilder(expected, sample(new String[] { "title", "pages" }, true)); } public void testMultipleFieldsExclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("price", 27.99) .field("timestamp", 1428582942867L) .nullField("default") @@ -439,23 +454,23 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "title", "pages")); + assertXContentBuilder(expected, sample(new String[] { "title", "pages" }, false)); } public void testSimpleArrayInclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startArray("tags") .value("elasticsearch") .value("java") .endArray() .endObject(); - assertXContentBuilder(expected, sample(true, "tags")); + assertXContentBuilder(expected, sample("tags", true)); } public void testSimpleArrayExclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -523,12 +538,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "tags")); + assertXContentBuilder(expected, sample("tags", false)); } public void testSimpleArrayOfObjectsInclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startArray("authors") .startObject() .field("name", "John Doe") @@ -543,13 +558,13 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endArray() .endObject(); - assertXContentBuilder(expected, sample(true, "authors")); - assertXContentBuilder(expected, sample(true, "authors.*")); - assertXContentBuilder(expected, sample(true, "authors.*name")); + assertXContentBuilder(expected, sample("authors", true)); + assertXContentBuilder(expected, sample("authors.*", true)); + assertXContentBuilder(expected, sample("authors.*name", true)); } public void testSimpleArrayOfObjectsExclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -609,13 +624,13 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "authors")); - assertXContentBuilder(expected, sample(false, "authors.*")); - assertXContentBuilder(expected, sample(false, "authors.*name")); + assertXContentBuilder(expected, sample("authors", false)); + assertXContentBuilder(expected, sample("authors.*", false)); + assertXContentBuilder(expected, sample("authors.*name", false)); } public void testSimpleArrayOfObjectsPropertyInclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startArray("authors") .startObject() .field("lastname", "John") @@ -626,12 +641,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endArray() .endObject(); - assertXContentBuilder(expected, sample(true, "authors.lastname")); - assertXContentBuilder(expected, sample(true, "authors.l*")); + assertXContentBuilder(expected, sample("authors.lastname", true)); + assertXContentBuilder(expected, sample("authors.l*", true)); } public void testSimpleArrayOfObjectsPropertyExclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -701,12 +716,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "authors.lastname")); - assertXContentBuilder(expected, sample(false, "authors.l*")); + assertXContentBuilder(expected, sample("authors.lastname", false)); + assertXContentBuilder(expected, sample("authors.l*", false)); } public void testRecurseField1Inclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startArray("authors") .startObject() .field("name", "John Doe") @@ -754,11 +769,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(true, "**.name")); + assertXContentBuilder(expected, sample("**.name", true)); } public void testRecurseField1Exclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -817,11 +832,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "**.name")); + assertXContentBuilder(expected, sample("**.name", false)); } public void testRecurseField2Inclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startObject("properties") .startObject("language") .startObject("en") @@ -861,11 +876,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(true, "properties.**.name")); + assertXContentBuilder(expected, sample("properties.**.name", true)); } public void testRecurseField2Exclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -926,12 +941,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "properties.**.name")); + assertXContentBuilder(expected, sample("properties.**.name", false)); } public void testRecurseField3Inclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startObject("properties") .startObject("language") .startObject("en") @@ -956,11 +971,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(true, "properties.*.en.**.name")); + assertXContentBuilder(expected, sample("properties.*.en.**.name", true)); } public void testRecurseField3Exclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -1026,12 +1041,12 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "properties.*.en.**.name")); + assertXContentBuilder(expected, sample("properties.*.en.**.name", false)); } public void testRecurseField4Inclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .startObject("properties") .startObject("language") .startObject("en") @@ -1058,11 +1073,11 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(true, "properties.**.distributors.name")); + assertXContentBuilder(expected, sample("properties.**.distributors.name", true)); } public void testRecurseField4Exclusive() throws Exception { - XContentBuilder expected = newXContentBuilder(true).startObject() + XContentBuilder expected = newXContentBuilder().startObject() .field("title", "My awesome book") .field("pages", 456) .field("price", 27.99) @@ -1126,59 +1141,88 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase .endObject() .endObject(); - assertXContentBuilder(expected, sample(false, "properties.**.distributors.name")); + assertXContentBuilder(expected, sample("properties.**.distributors.name", false)); } public void testRawField() throws Exception { + XContentBuilder expectedRawField = newXContentBuilder().startObject().field("foo", 0).startObject("raw") + .field("content", "hello world!").endObject().endObject(); + XContentBuilder expectedRawFieldFiltered = newXContentBuilder().startObject().field("foo", 0).endObject(); + XContentBuilder expectedRawFieldNotFiltered = newXContentBuilder().startObject().startObject("raw").field("content", "hello world!") + .endObject().endObject(); - XContentBuilder expectedRawField = newXContentBuilder(true).startObject().field("foo", 0).startObject("raw").field("content", "hello world!").endObject().endObject(); - XContentBuilder expectedRawFieldFiltered = newXContentBuilder(true).startObject().field("foo", 0).endObject(); - XContentBuilder expectedRawFieldNotFiltered = newXContentBuilder(true).startObject().startObject("raw").field("content", "hello world!").endObject().endObject(); - - BytesReference raw = newXContentBuilder(true).startObject().field("content", "hello world!").endObject().bytes(); + BytesReference raw = newXContentBuilder().startObject().field("content", "hello world!").endObject().bytes(); // Test method: rawField(String fieldName, BytesReference content) - assertXContentBuilder(expectedRawField, newXContentBuilder(true).startObject().field("foo", 0).rawField("raw", raw).endObject()); - assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(true, "f*").startObject().field("foo", 0).rawField("raw", raw).endObject()); - assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(false, "r*").startObject().field("foo", 0).rawField("raw", raw).endObject()); - assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(true, "r*").startObject().field("foo", 0).rawField("raw", raw).endObject()); - assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(false, "f*").startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawField, newXContentBuilder().startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, + newXContentBuilder("f*", true).startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, + newXContentBuilder("r*", false).startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, + newXContentBuilder("r*", true).startObject().field("foo", 0).rawField("raw", raw).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, + newXContentBuilder("f*", false).startObject().field("foo", 0).rawField("raw", raw).endObject()); // Test method: rawField(String fieldName, InputStream content) - assertXContentBuilder(expectedRawField, newXContentBuilder(true).startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); - assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(true, "f*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); - assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder(false, "r*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); - assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(true, "r*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); - assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder(false, "f*").startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawField, + newXContentBuilder().startObject().field("foo", 0).rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder("f*", true).startObject().field("foo", 0) + .rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldFiltered, newXContentBuilder("r*", false).startObject().field("foo", 0) + .rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder("r*", true).startObject().field("foo", 0) + .rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); + assertXContentBuilder(expectedRawFieldNotFiltered, newXContentBuilder("f*", false).startObject().field("foo", 0) + .rawField("raw", new ByteArrayInputStream(raw.toBytes())).endObject()); } - public void testArrays() throws Exception { // Test: Array of values (no filtering) - XContentBuilder expected = newXContentBuilder(true).startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject(); - assertXContentBuilder(expected, newXContentBuilder(true, "t*").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder(true, "tags").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder(false, "a").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + XContentBuilder expected = newXContentBuilder().startObject().startArray("tags").value("lorem").value("ipsum").value("dolor") + .endArray().endObject(); + assertXContentBuilder(expected, newXContentBuilder("t*", true).startObject().startArray("tags").value("lorem").value("ipsum") + .value("dolor").endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder("tags", true).startObject().startArray("tags").value("lorem").value("ipsum") + .value("dolor").endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder("a", false).startObject().startArray("tags").value("lorem").value("ipsum") + .value("dolor").endArray().endObject()); // Test: Array of values (with filtering) - assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(true, "foo").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); - assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "t*").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); - assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "tags").startObject().startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilder("foo", true).startObject() + .startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilder("t*", false).startObject() + .startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); + assertXContentBuilder(newXContentBuilder().startObject().endObject(), newXContentBuilder("tags", false).startObject() + .startArray("tags").value("lorem").value("ipsum").value("dolor").endArray().endObject()); // Test: Array of objects (no filtering) - expected = newXContentBuilder(true).startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject(); - assertXContentBuilder(expected, newXContentBuilder(true, "t*").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder(true, "tags").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder(false, "a").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + expected = newXContentBuilder().startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject() + .field("firstname", "ipsum").endObject().endArray().endObject(); + assertXContentBuilder(expected, newXContentBuilder("t*", true).startObject().startArray("tags").startObject() + .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder("tags", true).startObject().startArray("tags").startObject() + .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder("a", false).startObject().startArray("tags").startObject() + .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); // Test: Array of objects (with filtering) - assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(true, "foo").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); - assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "t*").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); - assertXContentBuilder(newXContentBuilder(true).startObject().endObject(), newXContentBuilder(false, "tags").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(newXContentBuilder().startObject().endObject(), + newXContentBuilder("foo", true).startObject().startArray("tags").startObject().field("lastname", "lorem").endObject() + .startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(newXContentBuilder().startObject().endObject(), + newXContentBuilder("t*", false).startObject().startArray("tags").startObject().field("lastname", "lorem").endObject() + .startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(newXContentBuilder().startObject().endObject(), + newXContentBuilder("tags", false).startObject().startArray("tags").startObject().field("lastname", "lorem").endObject() + .startObject().field("firstname", "ipsum").endObject().endArray().endObject()); // Test: Array of objects (with partial filtering) - expected = newXContentBuilder(true).startObject().startArray("tags").startObject().field("firstname", "ipsum").endObject().endArray().endObject(); - assertXContentBuilder(expected, newXContentBuilder(true, "t*.firstname").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); - assertXContentBuilder(expected, newXContentBuilder(false, "t*.lastname").startObject().startArray("tags").startObject().field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + expected = newXContentBuilder().startObject().startArray("tags").startObject().field("firstname", "ipsum").endObject().endArray() + .endObject(); + assertXContentBuilder(expected, newXContentBuilder("t*.firstname", true).startObject().startArray("tags").startObject() + .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); + assertXContentBuilder(expected, newXContentBuilder("t*.lastname", false).startObject().startArray("tags").startObject() + .field("lastname", "lorem").endObject().startObject().field("firstname", "ipsum").endObject().endArray().endObject()); } } diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index ba5301c4160..c99a8abcc25 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -34,105 +34,105 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { public void testInclusiveFilters() throws Exception { final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; - assertResult(SAMPLE, true, "a", "{'a':0}"); - assertResult(SAMPLE, true, "b", "{'b':true}"); - assertResult(SAMPLE, true, "c", "{'c':'c_value'}"); - assertResult(SAMPLE, true, "d", "{'d':[0,1,2]}"); - assertResult(SAMPLE, true, "e", "{'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, true, "h", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "z", ""); + assertResult(SAMPLE, "a", true, "{'a':0}"); + assertResult(SAMPLE, "b", true, "{'b':true}"); + assertResult(SAMPLE, "c", true, "{'c':'c_value'}"); + assertResult(SAMPLE, "d", true, "{'d':[0,1,2]}"); + assertResult(SAMPLE, "e", true, "{'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "z", true, ""); - assertResult(SAMPLE, true, "e.f1", "{'e':[{'f1':'f1_value'}]}"); - assertResult(SAMPLE, true, "e.f2", "{'e':[{'f2':'f2_value'}]}"); - assertResult(SAMPLE, true, "e.f*", "{'e':[{'f1':'f1_value','f2':'f2_value'}]}"); - assertResult(SAMPLE, true, "e.*2", "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); + assertResult(SAMPLE, "e.f1", true, "{'e':[{'f1':'f1_value'}]}"); + assertResult(SAMPLE, "e.f2", true, "{'e':[{'f2':'f2_value'}]}"); + assertResult(SAMPLE, "e.f*", true, "{'e':[{'f1':'f1_value','f2':'f2_value'}]}"); + assertResult(SAMPLE, "e.*2", true, "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); - assertResult(SAMPLE, true, "h.i", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.j", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.j.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.j.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "*.i", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "*.i", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "*.i.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.*.j", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "*.i.j", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.*.j", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "*.i.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.*.j.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.*.k", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.j.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "*.i.j.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.*.j.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.*.k", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.j.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "*.i.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.*.j.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.*.k.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.j.*.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.i.j.k.*", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "*.i.j.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.*.j.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.*.k.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.j.*.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.i.j.k.*", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "h.*.j.*.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "**.l", "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h.*.j.*.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "**.l", true, "{'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, true, "**.*2", "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); + assertResult(SAMPLE, "**.*2", true, "{'e':[{'f2':'f2_value'},{'g2':'g2_value'}]}"); } public void testExclusiveFilters() throws Exception { final String SAMPLE = "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"; - assertResult(SAMPLE, false, "a", "{'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "b", "{'a':0,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "c", "{'a':0,'b':true,'d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "d", "{'a':0,'b':true,'c':'c_value','e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "e", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "h", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "z", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "a", false, "{'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "b", false, "{'a':0,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "c", false, "{'a':0,'b':true,'d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "d", false, "{'a':0,'b':true,'c':'c_value','e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "e", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "h", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "z", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "e.f1", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "e.f2", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "e.f*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "e.*2", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "e.f1", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "e.f2", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "e.f*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'g1':'g1_value','g2':'g2_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "e.*2", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); - assertResult(SAMPLE, false, "h.i", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.j", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.j.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.j.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.j", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.j.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.j.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "*.i", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "*.i", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "*.i.j", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.*.j", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "*.i.j", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.*.j", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "*.i.j.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.*.j.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.*.k", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.j.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "*.i.j.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.*.j.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.*.k", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.j.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "*.i.j.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.*.j.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.*.k.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.j.*.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.i.j.k.*", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "*.i.j.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.*.j.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.*.k.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.j.*.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.i.j.k.*", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "h.*.j.*.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "**.l", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "h.*.j.*.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); + assertResult(SAMPLE, "**.l", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value','f2':'f2_value'},{'g1':'g1_value','g2':'g2_value'}]}"); - assertResult(SAMPLE, false, "**.*2", "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); + assertResult(SAMPLE, "**.*2", false, "{'a':0,'b':true,'c':'c_value','d':[0,1,2],'e':[{'f1':'f1_value'},{'g1':'g1_value'}],'h':{'i':{'j':{'k':{'l':'l_value'}}}}}"); } public void testInclusiveFiltersWithDots() throws Exception { - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b.c", "{'b':{'c':'c_value'}}"); - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", true, "b\\.c", "{'b.c':'value'}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b.c", true, "{'b':{'c':'c_value'}}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b\\.c", true, "{'b.c':'value'}"); } public void testExclusiveFiltersWithDots() throws Exception { - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", false, "b.c", "{'a':0,'b.c':'value'}"); - assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", false, "b\\.c", "{'a':0,'b':{'c':'c_value'}}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b.c", false, "{'a':0,'b.c':'value'}"); + assertResult("{'a':0,'b.c':'value','b':{'c':'c_value'}}", "b\\.c", false, "{'a':0,'b':{'c':'c_value'}}"); } - private void assertResult(String input, boolean inclusive, String filter, String expected) throws Exception { + private void assertResult(String input, String filter, boolean inclusive, String expected) throws Exception { try (BytesStreamOutput os = new BytesStreamOutput()) { try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), new FilterPathBasedFilter(inclusive, new String[] { filter }), true, true)) { From 6f915549c655b660510035f93bb3db239a26dc56 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Wed, 3 Feb 2016 12:18:05 +0100 Subject: [PATCH 7/9] Fixed typo in javadoc --- .../elasticsearch/common/xcontent/XContentBuilder.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java index fe11cec5ba9..a82a747624b 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/XContentBuilder.java @@ -117,10 +117,10 @@ public final class XContentBuilder implements BytesStream, Releasable { /** * Constructs a new builder using the provided xcontent, an OutputStream and - * some filters. If filters are specified and inclusiveFilters is true, only - * those values matching a filter will be written to the output stream. If - * inclusiveFilters is false, those matching will be excluded. Make sure to - * call {@link #close()} when the builder is done with. + * some filters. If {@code filters} are specified and {@code inclusive} is + * true, only those values matching a filter will be written to the output + * stream. If {@code inclusive} is false, those matching will be excluded. + * Make sure to call {@link #close()} when the builder is done with. */ public XContentBuilder(XContent xContent, OutputStream bos, String[] filters, boolean inclusive) throws IOException { this.bos = bos; From 42a44b19aaa9a47e0d00a8cbb303d634f7924f40 Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Wed, 3 Feb 2016 12:23:14 +0100 Subject: [PATCH 8/9] Fixed parameter order in FilterPathBasedFilter constructor --- .../common/xcontent/json/JsonXContentGenerator.java | 2 +- .../xcontent/support/filtering/FilterPathBasedFilter.java | 2 +- .../support/filtering/FilterPathGeneratorFilteringTests.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java index c7eb38b5f33..96cfd63f753 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/json/JsonXContentGenerator.java @@ -87,7 +87,7 @@ public class JsonXContentGenerator implements XContentGenerator { this.filter = null; } else { this.filter = new FilteringGeneratorDelegate(jsonGenerator, - new FilterPathBasedFilter(inclusive, filters), true, true); + new FilterPathBasedFilter(filters, inclusive), true, true); this.generator = this.filter; } diff --git a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index f6a81e6fa3b..69e4e79110d 100644 --- a/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/core/src/main/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -53,7 +53,7 @@ public class FilterPathBasedFilter extends TokenFilter { this.filters = filters; } - public FilterPathBasedFilter(boolean inclusive, String[] filters) { + public FilterPathBasedFilter(String[] filters, boolean inclusive) { this(FilterPath.compile(filters), inclusive); } diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java index c99a8abcc25..dd2fe42eb8e 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/FilterPathGeneratorFilteringTests.java @@ -135,7 +135,7 @@ public class FilterPathGeneratorFilteringTests extends ESTestCase { private void assertResult(String input, String filter, boolean inclusive, String expected) throws Exception { try (BytesStreamOutput os = new BytesStreamOutput()) { try (FilteringGeneratorDelegate generator = new FilteringGeneratorDelegate(JSON_FACTORY.createGenerator(os), - new FilterPathBasedFilter(inclusive, new String[] { filter }), true, true)) { + new FilterPathBasedFilter(new String[] { filter }, inclusive), true, true)) { try (JsonParser parser = JSON_FACTORY.createParser(replaceQuotes(input))) { while (parser.nextToken() != null) { generator.copyCurrentStructure(parser); From 42833b0f71ceb6cf8c20ad831c2e83772ddb769c Mon Sep 17 00:00:00 2001 From: Konrad Beiske Date: Wed, 3 Feb 2016 12:37:38 +0100 Subject: [PATCH 9/9] Adjusted indentation --- ...bstractFilteringJsonGeneratorTestCase.java | 1214 ++++++++--------- 1 file changed, 607 insertions(+), 607 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java index c060e6c6246..e3d8735e05e 100644 --- a/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java +++ b/core/src/test/java/org/elasticsearch/common/xcontent/support/filtering/AbstractFilteringJsonGeneratorTestCase.java @@ -217,75 +217,75 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleFieldExclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("title", false)); } @@ -349,27 +349,27 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleFieldWithWildcardExclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") - .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .endObject(); + .field("title", "My awesome book") + .field("pages", 456) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .endObject(); assertXContentBuilder(expected, sample("pr*", false)); } @@ -385,74 +385,74 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testMultipleFieldsExclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample(new String[] { "title", "pages" }, false)); } @@ -460,10 +460,10 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleArrayInclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() .endObject(); assertXContentBuilder(expected, sample("tags", true)); @@ -471,72 +471,72 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleArrayExclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("tags", false)); } @@ -544,19 +544,19 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleArrayOfObjectsInclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") - .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .endObject(); + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .endObject(); assertXContentBuilder(expected, sample("authors", true)); assertXContentBuilder(expected, sample("authors.*", true)); @@ -565,64 +565,64 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleArrayOfObjectsExclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("authors", false)); assertXContentBuilder(expected, sample("authors.*", false)); @@ -647,74 +647,74 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testSimpleArrayOfObjectsPropertyExclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("firstname", "Doe") - .endObject() - .startObject() - .field("name", "William Smith") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() - .endObject() - .endObject() - .endObject(); + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() + .endObject() + .endObject() + .endObject() + .endObject(); assertXContentBuilder(expected, sample("authors.lastname", false)); assertXContentBuilder(expected, sample("authors.l*", false)); @@ -774,63 +774,63 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testRecurseField1Exclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("lastname", "John") - .field("firstname", "Doe") + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() - .startObject() - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("**.name", false)); } @@ -881,65 +881,65 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testRecurseField2Exclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("properties.**.name", false)); } @@ -947,99 +947,99 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testRecurseField3Inclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .startObject("properties") - .startObject("language") - .startObject("en") - .startArray("distributors") - .startObject() - .field("name", "The Book Shop") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .endObject() - .startObject() - .field("name", "address #2") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Sussex Books House") - .endObject() - .endArray() + .startObject("properties") + .startObject("language") + .startObject("en") + .startArray("distributors") + .startObject() + .field("name", "The Book Shop") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .endObject() + .startObject() + .field("name", "address #2") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Sussex Books House") + .endObject() + .endArray() + .endObject() .endObject() .endObject() - .endObject() - .endObject(); + .endObject(); assertXContentBuilder(expected, sample("properties.*.en.**.name", true)); } public void testRecurseField3Exclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .field("name", "La Maison du Livre") - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .startObject() - .field("name", "Thetra") - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .field("name", "La Maison du Livre") + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .startObject() + .field("name", "Thetra") + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("properties.*.en.**.name", false)); } @@ -1078,68 +1078,68 @@ public abstract class AbstractFilteringJsonGeneratorTestCase extends ESTestCase public void testRecurseField4Exclusive() throws Exception { XContentBuilder expected = newXContentBuilder().startObject() - .field("title", "My awesome book") - .field("pages", 456) - .field("price", 27.99) - .field("timestamp", 1428582942867L) - .nullField("default") - .startArray("tags") - .value("elasticsearch") - .value("java") - .endArray() - .startArray("authors") - .startObject() - .field("name", "John Doe") - .field("lastname", "John") - .field("firstname", "Doe") + .field("title", "My awesome book") + .field("pages", 456) + .field("price", 27.99) + .field("timestamp", 1428582942867L) + .nullField("default") + .startArray("tags") + .value("elasticsearch") + .value("java") + .endArray() + .startArray("authors") + .startObject() + .field("name", "John Doe") + .field("lastname", "John") + .field("firstname", "Doe") + .endObject() + .startObject() + .field("name", "William Smith") + .field("lastname", "William") + .field("firstname", "Smith") + .endObject() + .endArray() + .startObject("properties") + .field("weight", 0.8d) + .startObject("language") + .startObject("en") + .field("lang", "English") + .field("available", true) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Hampton St") + .field("city", "London") + .endObject() + .startObject() + .field("name", "address #2") + .field("street", "Queen St") + .field("city", "Stornoway") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() - .startObject() - .field("name", "William Smith") - .field("lastname", "William") - .field("firstname", "Smith") - .endObject() - .endArray() - .startObject("properties") - .field("weight", 0.8d) - .startObject("language") - .startObject("en") - .field("lang", "English") - .field("available", true) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Hampton St") - .field("city", "London") - .endObject() - .startObject() - .field("name", "address #2") - .field("street", "Queen St") - .field("city", "Stornoway") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() - .startObject("fr") - .field("lang", "French") - .field("available", false) - .startArray("distributors") - .startObject() - .startArray("addresses") - .startObject() - .field("name", "address #1") - .field("street", "Rue Mouffetard") - .field("city", "Paris") - .endObject() - .endArray() - .endObject() - .endArray() - .endObject() + .startObject("fr") + .field("lang", "French") + .field("available", false) + .startArray("distributors") + .startObject() + .startArray("addresses") + .startObject() + .field("name", "address #1") + .field("street", "Rue Mouffetard") + .field("city", "Paris") + .endObject() + .endArray() + .endObject() + .endArray() .endObject() .endObject() - .endObject(); + .endObject() + .endObject(); assertXContentBuilder(expected, sample("properties.**.distributors.name", false)); }