SearchTemplate API necessary jars


(banupriya) #1

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 ?


(David Pilato) #2

Are you using a TransportClient?


(banupriya) #3

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

(David Pilato) #4

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?


(banupriya) #5

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


(David Pilato) #6

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:


(banupriya) #7

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


(David Pilato) #8

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.


(banupriya) #9

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.


(David Pilato) #10

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


(banupriya) #11

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


(banupriya) #12

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 ?


(David Pilato) #13

I ended up proposing some changes in documentation:

Have a look at this part specifically:

I hope this helps.


(banupriya) #14

How to parse the below query string in elasticsearch 5.

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


(system) #15

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