# Template queries with params in Java API. Bug?

**URL:** <https://discuss.elastic.co/t/template-queries-with-params-in-java-api-bug/21102>\
**Category:** Elasticsearch\
**Created:** [December 5, 2014, 10:32am UTC](https://discuss.elastic.co/t/template-queries-with-params-in-java-api-bug/21102 "2014-12-05T10:32:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke\_Wilson\_Mawer](https://avatars.discourse-cdn.com/v4/letter/l/278dde/32.png) [@Luke\_Wilson\_Mawer](https://discuss.elastic.co/u/Luke_Wilson_Mawer)\
**Post date:** [December 5, 2014, 10:32am UTC](https://discuss.elastic.co/t/template-queries-with-params-in-java-api-bug/21102/1 "2014-12-05T10:32:09Z")

</div>

Hi,

I am trying to figure out issues with these kinds of queries when using the  
Java API.

curl [http://10.10.9.13:9200/global6/user/\_search/template](http://10.10.9.13:9200/global6/user/_search/template) '{  
"template": {  
"query": {  
"term": {  
"{{field}}" : "{{value}}"  
}  
},  
"aggs" : {  
"{{field}}" :  
{ "terms" : { "field": "{{field}}"} }  
}  
},  
"params": {  
"field" : "twuser.twuser.name",  
"value" : "alexander"  
}  
}'

Unfortunately, the params don’t seem to work in template queries when used  
via the Java API (without params the template works). I am building the  
query like this (thanks Colin G):

getSearchRequestBuilder().setTemplateSource(Resources.toString(url,  
Charset.forName("UTF-8"))).setTemplateParams(templateParams);

I think the problem lies here:

```
     org.elasticsearch.action.search.SearchRequest
    if (out.getVersion().onOrAfter(Version.V_1_1_0)) {
        out.writeBytesReference(templateSource);
        out.writeOptionalString(templateName);

        booleanexistTemplateParams = templateParams != null;
        out.writeBoolean(existTemplateParams);
        if (existTemplateParams) {
            out.writeGenericValue(templateParams);
        }
    }
}

```

As far as I can see the params don’t get added as a json map, although I’m  
not entirely familiar with the format of transport messages.

Logs from the slow log on the server show an empty space where the  
placeholder should be (e.g. "{{value}}" in the template above looks like  
“”).

Is this a bug? If so happy to produce a patch. If not, any workaround would  
be welcome - I’m currently just doing the templating myself.

Kind regards,

Luke

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/187eed4b-0ac4-42e0-9dfe-c2027b667b13%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/187eed4b-0ac4-42e0-9dfe-c2027b667b13%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 5, 2014, 12:01pm UTC](https://discuss.elastic.co/t/template-queries-with-params-in-java-api-bug/21102/2 "2014-12-05T12:01:26Z")

</div>

It was just documented some days ago: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html#java-search-template)

Could this help you? If not, please tell me what is missing or send a PR. 🙂

David

> Le 5 déc. 2014 à 11:32, Luke Wilson-Mawer [lukewilsonmawer@gmail.com](mailto:lukewilsonmawer@gmail.com) a écrit :
> 
> Hi,
> 
> I am trying to figure out issues with these kinds of queries when using the Java API.
> 
> curl [http://10.10.9.13:9200/global6/user/\_search/template](http://10.10.9.13:9200/global6/user/_search/template) '{  
> "template": {  
> "query": {  
> "term": {  
> "{{field}}" : "{{value}}"  
> }  
> },  
> "aggs" : {  
> "{{field}}" :  
> { "terms" : { "field": "{{field}}"} }  
> }  
> },  
> "params": {  
> "field" : "twuser.twuser.name",  
> "value" : "alexander"  
> }  
> }'
> 
> Unfortunately, the params don’t seem to work in template queries when used via the Java API (without params the template works). I am building the query like this (thanks Colin G):
> 
> getSearchRequestBuilder().setTemplateSource(Resources.toString(url, Charset.forName("UTF-8"))).setTemplateParams(templateParams);
> 
> I think the problem lies here:
> 
> ```
> org.elasticsearch.action.search.SearchRequest
> if (out.getVersion().onOrAfter(Version.V_1_1_0)) {
> out.writeBytesReference(templateSource);
> out.writeOptionalString(templateName);
> 
> booleanexistTemplateParams = templateParams != null;
> out.writeBoolean(existTemplateParams);
> if (existTemplateParams) {
> out.writeGenericValue(templateParams);
> }
> }
> }
> 
> ```
> 
> As far as I can see the params don’t get added as a json map, although I’m not entirely familiar with the format of transport messages.
> 
> Logs from the slow log on the server show an empty space where the placeholder should be (e.g. "{{value}}" in the template above looks like “”).
> 
> Is this a bug? If so happy to produce a patch. If not, any workaround would be welcome - I’m currently just doing the templating myself.
> 
> Kind regards,
> 
> ## Luke
> 
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/187eed4b-0ac4-42e0-9dfe-c2027b667b13%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/187eed4b-0ac4-42e0-9dfe-c2027b667b13%40googlegroups.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/25673D45-D5CB-4C2B-B149-7F2B8CFDD73A%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/25673D45-D5CB-4C2B-B149-7F2B8CFDD73A%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:45am UTC](https://discuss.elastic.co/t/template-queries-with-params-in-java-api-bug/21102/3 "2017-07-06T00:45:36Z")

</div>


