SearchTemplate API necessary jars

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 ?

Are you using a TransportClient?

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();

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?

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]]

I looked at the documentation 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 :slight_smile:

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

So here is what I found until now:

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.

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.

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

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

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 ?

I ended up proposing some changes in documentation:

Have a look at this part specifically:

I hope this helps.

How to parse the below query string in elasticsearch 5.

{
"template":{"query":{"query_string":{"query":"((cust nmenew"{{name}}")^50 OR (ceo_Name:({{name}}))^30
}}

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