From 5d159369efffbfd56a391a01e3d3da5364707df3 Mon Sep 17 00:00:00 2001 From: David Pilato Date: Sat, 15 Feb 2014 11:00:57 +0100 Subject: [PATCH] Source filtering with wildcards broken when given multiple patterns ``` curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{ "user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch", "retweeted": false }' ``` No source fields delivered: ``` curl -XGET 'http://localhost:9200/twitter/tweet/1?_source=*.id,retweeted&pretty=yes' ``` `retweeted` returned: ``` curl -XGET 'http://localhost:9200/twitter/tweet/1?_source=retweeted,*.id&pretty=yes' ``` Closes #5132. Closes #5133. --- .../xcontent/support/XContentMapValues.java | 4 ++-- .../search/source/SourceFetchingTests.java | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java b/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java index ab316e9f31a..2d178c20e86 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java +++ b/src/main/java/org/elasticsearch/common/xcontent/support/XContentMapValues.java @@ -175,7 +175,7 @@ public class XContentMapValues { break; } pathIsPrefixOfAnInclude = true; - break; + continue; } if (include.startsWith(path)) { if (include.length() == path.length()) { @@ -184,7 +184,7 @@ public class XContentMapValues { } else if (include.length() > path.length() && include.charAt(path.length()) == '.') { // include might may match deeper paths. Dive deeper. pathIsPrefixOfAnInclude = true; - break; + continue; } } if (Regex.simpleMatch(include, path)) { diff --git a/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java b/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java index de30d6c7c35..88cc4d8589a 100644 --- a/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java +++ b/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java @@ -81,5 +81,26 @@ public class SourceFetchingTests extends ElasticsearchIntegrationTest { } + /** + * Test Case for #5132: Source filtering with wildcards broken when given multiple patterns + * https://github.com/elasticsearch/elasticsearch/issues/5132 + */ + @Test + public void testSourceWithWildcardFiltering() { + createIndex("test"); + ensureGreen(); + client().prepareIndex("test", "type1", "1").setSource("field", "value").get(); + refresh(); + + SearchResponse response = client().prepareSearch("test").setFetchSource(new String[]{"*.notexisting","field"}, null).get(); + assertThat(response.getHits().getAt(0).getSourceAsString(), notNullValue()); + assertThat(response.getHits().getAt(0).getSource().size(), equalTo(1)); + assertThat((String) response.getHits().getAt(0).getSource().get("field"), equalTo("value")); + + response = client().prepareSearch("test").setFetchSource(new String[]{"field.notexisting.*","field"}, null).get(); + assertThat(response.getHits().getAt(0).getSourceAsString(), notNullValue()); + assertThat(response.getHits().getAt(0).getSource().size(), equalTo(1)); + assertThat((String) response.getHits().getAt(0).getSource().get("field"), equalTo("value")); + } }