# SearchTemplate API necessary jars

**URL:** <https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310>\
**Category:** Elasticsearch\
**Created:** [November 28, 2016, 7:24am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310 "2016-11-28T07:24:25Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 7:24am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/1 "2016-11-28T07:24:25Z")

</div>

As the elasticsearch version 5 splits the jars into to different module. When we try to execute a search template not able to set the Template in the SearchRequestBuilder. What are the jars required for executing a search template. The searchTemplate has QueryString what would be the best approach to execute the query ?

For Ex: This is my search template

{  
"template":{"query":{"query\_string":{"query":"((cust \_nme\_new\"{{name}}\")^50 OR (ceo\_Name:({{name}}))^30  
}}

Can somebody suggest us on this ?

---

<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:** [November 28, 2016, 7:45am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/2 "2016-11-28T07:45:54Z")

</div>

Are you using a TransportClient?

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 7:47am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/3 "2016-11-28T07:47:38Z")

</div>

Yes. Please find the code below TransportClient client = new PreBuiltTransportClient(settings)  
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));  
// Passing the template params to the template query  
Map\<String, Object\> template\_params = new HashMap\<\>();  
template\_params.put("cust nmenew", "Newyork");

```
	SearchRequestBuilder searchBuilder = client.prepareSearch("match");
        SearchResponse response = searchBuilder.setSearchType(SearchType.QUERY_AND_FETCH)
                        .setTemplate(new Template("lookup_temp", ScriptType.INDEXED,
                                        MustacheScriptEngineService.NAME, null, template_params)).setFrom(0)
                        .execute().actionGet();
```

---

<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:** [November 28, 2016, 8:14am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/4 "2016-11-28T08:14:07Z")

</div>

Please format your code using `</>` icon. It will make your post more readable.

You did not mention your error but I guess it's all about `MustacheScriptEngineService.NAME`? Am I correct?

I don't believe you need to add anything on the client side as the client does not do any transformation but just pass the query to the coordinating node.

So what is exactly your error?

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 9:01am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/5 "2016-11-28T09:01:27Z")

</div>

Error we are getting :  
Failed to execute phase [query\_fetch], all shards failed; shardFailures {[IhhQQOkrSaKlWju\_L\_ZRNg][twitter][0]: RemoteTransportException[[IhhQQOk][127.0.0.1:9300][indices:data/read/search[phase/query+fetch]]]; nested: ParsingException[no [query] registered for [query]]; }org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1033)  
at org.elasticsearch.transport.TransportService$6.onFailure(TransportService.java:559)  
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.onFailure(ThreadContext.java:490)  
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:39)  
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)  
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)  
at java.lang.Thread.run(Thread.java:745)  
Caused by: RemoteTransportException[[IhhQQOk][127.0.0.1:9300][indices:data/read/search[phase/query+fetch]]]; nested: ParsingException[no [query] registered for [query]];  
Caused by: ParsingException[no [query] registered for [query]]

---

<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:** [November 28, 2016, 9:42am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/6 "2016-11-28T09:42:18Z")

</div>

I looked [at the documentation](https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.0/java-specialized-queries.html#java-query-dsl-template-query) and it looks like a bit outdated.

I'm going to fully reproduce your problem and will come with results later on.  
If you fix it in the meantime please post your findings here 🙂

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 9:44am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/7 "2016-11-28T09:44:44Z")

</div>

Okay sure. We are also searching for the fix. Please update us if you find the solution.

---

<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:** [November 28, 2016, 12:06pm UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/8 "2016-11-28T12:06:30Z")

</div>

So here is what I found until now:

```auto
Map<String, Object> template_params = new HashMap<>();
template_params.put("param_gender", "male");

SearchResponse sr = new SearchTemplateRequestBuilder(client).setScript(
        "{\n" +
                " \"template\" : {\n" +
                " \"query\" : {\n" +
                " \"match\" : {\n" +
                " \"gender\" : \"{{param_gender}}\"\n" +
                " }\n" +
                " }\n" +
                " }\n" +
                "}"
).setScriptType(ScriptService.ScriptType.INLINE)
        .setRequest(new SearchRequest(INDEX))
        .setScriptParams(template_params)
        .get().getResponse();

```

Let me know if it helps.

Note that you don't need to add any specific other lib to run that.

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 12:44pm UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/9 "2016-11-28T12:44:39Z")

</div>

We tried this method to convert the below query but we don't know how to do for the OR query with queryString option

{  
"template":{"query":{"query\_string":{"query":"((cust nmenew\"{{name}}\")^50 OR (ceo\_Name:({{name}}))^30  
}}

Can you help us in converting the above query into to script.

---

<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:** [November 28, 2016, 12:56pm UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/10 "2016-11-28T12:56:53Z")

</div>

May be try with an example without any template first. Then, when working, convert it to a template.

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 12:58pm UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/11 "2016-11-28T12:58:58Z")

</div>

We already tried your approach but we are unable to convert the same with OR conditions. How to do this ?

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 28, 2016, 1:04pm UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/12 "2016-11-28T13:04:24Z")

</div>

We are getting response for the above query by removing the template key from the query. After that we tried this query  
"{\n" +   
" "query" : {\n"+  
"(("cust nmenew":"{{param\_gender}}") AND ("ceo\_Name":"{{param\_gender}}"))" +  
" }\n" +  
"}"

We are getting error  
UncategorizedExecutionException[Failed execution]; nested: IOException[Unexpected character ('(' (code 40)): was expecting double-quote to start field name

Can you help us on this error ?

---

<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:** [November 28, 2016, 4:37pm UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/13 "2016-11-28T16:37:58Z")

</div>

I ended up proposing some changes in documentation:

> <https://github.com/elastic/elasticsearch/pull/21831>

Have a look at this part specifically:

> <https://github.com/elastic/elasticsearch/blob/f8cf9f790bf797b12b2251f28b5c487e9c3d94f0/docs/java-api/search.asciidoc#search-template>

I hope this helps.

---

<div class="post-metadata">

**Author:** ![banu](https://avatars.discourse-cdn.com/v4/letter/b/2acd7d/32.png) [@banu](https://discuss.elastic.co/u/banu)\
**Post date:** [November 29, 2016, 8:43am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/14 "2016-11-29T08:43:04Z")

</div>

How to parse the below query string in elasticsearch 5.

{  
"template":{"query":{"query\_string":{"query":"((cust nmenew"{{name}}")^50 OR (ceo\_Name:({{name}}))^30  
}}

---

<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:** [December 27, 2016, 8:43am UTC](https://discuss.elastic.co/t/searchtemplate-api-necessary-jars/67310/15 "2016-12-27T08:43:31Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
