Elasticsearch 5.0 SearchTemplate query Error

We are trying to use the search template and get the search results. Can anyone suggest a solution for this.

Attached the error what we are getting

package elastic5;
.
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.mustache.TemplateQueryBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class ElasticTest {

public static void main(String[] args) {
	// TODO Auto-generated method stub

	// on startup

	TransportClient client;
	Settings settings = Settings.builder()
	        .put("cluster.name", "elasticsearch").build();
	try {
		client = new PreBuiltTransportClient(settings)
		        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
		
		
		
		Map<String, Object> template_params = new HashMap<>();
		template_params.put("query_string", "aaaaa");
		
		QueryBuilder qb = new TemplateQueryBuilder(
			    "template_gender",                  
			    ScriptService.ScriptType.STORED,      
			    template_params);
		
		SearchResponse response = client.prepareSearch()
	                .setIndices("twitter")
	                .setTypes("canidate")
	                .setQuery(qb).execute().actionGet();
		
		
		
		
		
		System.out.println(response.getHits());
		// on shutdown
		System.out.println("SUCEESSS");
		System.out.println(client.nodeName());
		client.close();
			
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		
	}
	
}

}

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

Searched and we could not find any solution for this can somebody help us on this ?

Can you post the actual content of the stored template you are using (the one referred to by "template_gender" above?

My suspicion is that something goes wrong when that is compiled to an actual ES query.

Please find the below stored template
{
"lang": "mustache",
"_id": "template_gender",
"found": true,
"template": "{"query":{"match":{"tittle":"{{query_string}}"}}}"
}

Fixed the issue the problem here is the "template": "{"query":{"match":{"tittle":"{{query_string}}"}}}" "query:" should be omitted.

1 Like

Great to hear you could fix your issue.

Isabel

How to run this query from java API POST /_search/template/lookup_temp
{
"template": {
"match":
{
"(city_key":"({{nme}})^50 OR (name:({{nmewoe}}))^30 )"
}

    }

}

Getting error when running this template query from java client API

What kind of error does the Java API give you?

No Error but getting zero results {"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}

Even when i have data in the name which i use to query.

Can you provide some suggestions on this

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