From c7bb784b086c915cf6b46e7c6f59fcbf66af6c95 Mon Sep 17 00:00:00 2001 From: Britta Weber Date: Thu, 24 Apr 2014 19:58:06 +0200 Subject: [PATCH] Fix TemplateQueryParser swallows additional parameters Request parameters such as "size" and "fields" were ignored when placed after the template query in the reqest. closes #5933 --- .../index/query/TemplateQueryParser.java | 1 - .../index/query/TemplateQueryTest.java | 40 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/index/query/TemplateQueryParser.java b/src/main/java/org/elasticsearch/index/query/TemplateQueryParser.java index b92a56cde08..5427917823c 100644 --- a/src/main/java/org/elasticsearch/index/query/TemplateQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/TemplateQueryParser.java @@ -74,7 +74,6 @@ public class TemplateQueryParser implements QueryParser { final QueryParseContext context = new QueryParseContext(parseContext.index(), parseContext.indexQueryParser); context.reset(qSourceParser); Query result = context.parseInnerQuery(); - parser.nextToken(); return result; } } diff --git a/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java b/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java index 423866a0968..a3a88601ee5 100644 --- a/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java +++ b/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java @@ -24,6 +24,8 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.test.ElasticsearchIntegrationTest; import org.junit.Before; import org.junit.Test; @@ -35,6 +37,8 @@ import java.util.Map; import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; +import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; /** @@ -71,6 +75,42 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { assertHitCount(sr, 2); } + @Test + public void testTemplateInBodyWithSize() throws IOException { + String request = "{\n" + + " \"size\":0,"+ + " \"query\": {\n" + + " \"template\": {\n" + + " \"query\": {\"match_{{template}}\": {}},\n" + + " \"params\" : {\n" + + " \"template\" : \"all\"\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + SearchResponse sr = client().prepareSearch().setSource(request) + .execute().actionGet(); + assertNoFailures(sr); + assertThat(sr.getHits().hits().length, equalTo(0)); + request = "{\n" + + " \"query\": {\n" + + " \"template\": {\n" + + " \"query\": {\"match_{{template}}\": {}},\n" + + " \"params\" : {\n" + + " \"template\" : \"all\"\n" + + " }\n" + + " }\n" + + " },\n" + + " \"size\":0"+ + "}"; + + sr = client().prepareSearch().setSource(request) + .execute().actionGet(); + assertNoFailures(sr); + assertThat(sr.getHits().hits().length, equalTo(0)); + + } + @Test public void testTemplateWOReplacementInBody() throws IOException { Map vars = new HashMap<>();