From fda1576d55fdd56fd44df6d4608dca74d33d1536 Mon Sep 17 00:00:00 2001 From: Reuben Sutton Date: Tue, 28 Oct 2014 14:20:38 +0000 Subject: [PATCH] Fix SearchRequest.templateParams so that it is a Map so that it can take more data-types than just strings, to support Arrays. --- .../action/search/SearchRequest.java | 8 ++-- .../action/search/SearchRequestBuilder.java | 2 +- .../internal/ShardSearchLocalRequest.java | 7 ++-- .../search/internal/ShardSearchRequest.java | 2 +- .../internal/ShardSearchTransportRequest.java | 2 +- .../index/query/TemplateQueryTest.java | 39 +++++++++++++++++-- 6 files changed, 45 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/search/SearchRequest.java b/src/main/java/org/elasticsearch/action/search/SearchRequest.java index 76a5c9f9a46..63641539bd5 100644 --- a/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -74,7 +74,7 @@ public class SearchRequest extends ActionRequest implements Indic private boolean templateSourceUnsafe; private String templateName; private ScriptService.ScriptType templateType; - private Map templateParams = Collections.emptyMap(); + private Map templateParams = Collections.emptyMap(); private BytesReference source; private boolean sourceUnsafe; @@ -452,7 +452,7 @@ public class SearchRequest extends ActionRequest implements Indic /** * Template parameters used for rendering */ - public void templateParams(Map params) { + public void templateParams(Map params) { this.templateParams = params; } @@ -473,7 +473,7 @@ public class SearchRequest extends ActionRequest implements Indic /** * Template parameters used for rendering */ - public Map templateParams() { + public Map templateParams() { return templateParams; } @@ -579,7 +579,7 @@ public class SearchRequest extends ActionRequest implements Indic templateType = ScriptService.ScriptType.readFrom(in); } if (in.readBoolean()) { - templateParams = (Map) in.readGenericValue(); + templateParams = (Map) in.readGenericValue(); } } diff --git a/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java b/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java index 16b45bf34d6..ecb979e55f6 100644 --- a/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java @@ -958,7 +958,7 @@ public class SearchRequestBuilder extends ActionRequestBuilder templateParams) { + public SearchRequestBuilder setTemplateParams(Map templateParams) { request.templateParams(templateParams); return this; } diff --git a/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java b/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java index 10c69ff0057..60860a966af 100644 --- a/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java +++ b/src/main/java/org/elasticsearch/search/internal/ShardSearchLocalRequest.java @@ -80,7 +80,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest { private BytesReference templateSource; private String templateName; private ScriptService.ScriptType templateType; - private Map templateParams; + private Map templateParams; private Boolean queryCache; private long nowInMillis; @@ -188,7 +188,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest { } @Override - public Map templateParams() { + public Map templateParams() { return templateParams; } @@ -236,7 +236,7 @@ public class ShardSearchLocalRequest implements ShardSearchRequest { templateType = ScriptService.ScriptType.readFrom(in); } if (in.readBoolean()) { - templateParams = (Map) in.readGenericValue(); + templateParams = (Map) in.readGenericValue(); } } if (in.getVersion().onOrAfter(ParsedScrollId.SCROLL_SEARCH_AFTER_MINIMUM_VERSION)) { @@ -302,4 +302,3 @@ public class ShardSearchLocalRequest implements ShardSearchRequest { return out.bytes().copyBytesArray(); } } - diff --git a/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java b/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java index 0fac9680e69..4d104dc2799 100644 --- a/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java +++ b/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java @@ -58,7 +58,7 @@ public interface ShardSearchRequest { ScriptService.ScriptType templateType(); - Map templateParams(); + Map templateParams(); BytesReference templateSource(); diff --git a/src/main/java/org/elasticsearch/search/internal/ShardSearchTransportRequest.java b/src/main/java/org/elasticsearch/search/internal/ShardSearchTransportRequest.java index 31275721f43..0ec8bc99450 100644 --- a/src/main/java/org/elasticsearch/search/internal/ShardSearchTransportRequest.java +++ b/src/main/java/org/elasticsearch/search/internal/ShardSearchTransportRequest.java @@ -135,7 +135,7 @@ public class ShardSearchTransportRequest extends TransportRequest implements Sha } @Override - public Map templateParams() { + public Map templateParams() { return shardSearchLocalRequest.templateParams(); } diff --git a/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java b/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java index f938f15b7f7..24351d95d86 100644 --- a/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java +++ b/src/test/java/org/elasticsearch/index/query/TemplateQueryTest.java @@ -205,7 +205,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { index("test", "type", "5", jsonBuilder().startObject().field("otherField", "foo").endObject()); refresh(); - Map templateParams = Maps.newHashMap(); + Map templateParams = Maps.newHashMap(); templateParams.put("mySize", "2"); templateParams.put("myField", "theField"); templateParams.put("myValue", "foo"); @@ -260,7 +260,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { indexRandom(true,builders); - Map templateParams = Maps.newHashMap(); + Map templateParams = Maps.newHashMap(); templateParams.put("fieldParam", "foo"); SearchResponse searchResponse = client().prepareSearch("test").setTypes("type"). @@ -306,7 +306,6 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { " }" + "}")); - indexRandom(true, builders); builders.clear(); @@ -319,7 +318,7 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { indexRandom(true,builders); - Map templateParams = Maps.newHashMap(); + Map templateParams = Maps.newHashMap(); templateParams.put("fieldParam", "foo"); SearchResponse searchResponse = client().prepareSearch("test").setTypes("type"). @@ -368,4 +367,36 @@ public class TemplateQueryTest extends ElasticsearchIntegrationTest { sr = client().prepareSearch().setQuery(query).get(); assertHitCount(sr, 4); } + + @Test + public void testIndexedTemplateWithArray() throws Exception { + createIndex(ScriptService.SCRIPT_INDEX); + ensureGreen(ScriptService.SCRIPT_INDEX); + List builders = new ArrayList<>(); + + String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}"; + + builders.add(client().prepareIndex(ScriptService.SCRIPT_INDEX, "mustache", "4").setSource(jsonBuilder().startObject().field("template", multiQuery).endObject())); + + indexRandom(true,builders); + + builders.clear(); + + builders.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}")); + builders.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}")); + builders.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}")); + builders.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}")); + builders.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}")); + + indexRandom(true,builders); + + Map arrayTemplateParams = new HashMap<>(); + String[] fieldParams = {"foo","bar"}; + arrayTemplateParams.put("fieldParam", fieldParams); + + SearchResponse searchResponse = client().prepareSearch("test").setTypes("type"). + setTemplateName("/mustache/4").setTemplateType(ScriptService.ScriptType.INDEXED).setTemplateParams(arrayTemplateParams).get(); + assertHitCount(searchResponse, 5); + } + }