Elasticsearch error when executing a template query from java client

The template which we are using is
{
"template": "{"size":"{{no_of_candidates}}{{^no_of_candidates}}25{{/no_of_candidates}}","query":{"query_string":{"query":"((norm_custnme_exct:\"{{nme}}\")^50 OR (ceo.ceoName:({{nme}}))^30 OR (norm_cust_nme_word:\"{{nme}}\")^30 OR (norm_custnme:({{nme}}) AND city_key:\"{{City}}\" ) ^40 OR (norm_custnme:({{nme}}))^30 OR (tradeStyles.acronym:\"{{acronym}}\")^5 OR (norm_cust_nme:\"{{nme}}\")^5 OR (physicalAddresses.streetName:\"{{strNme}}\" AND city_key:\"{{phyCity}}\")^30 OR (zip_key:\"{{postCode}}\")^6 OR (physicalAddresses.streetName:\"{{strNme}}\")^6 OR (physicalAddresses.streetNumber:\"{{strNbr}}\" AND physicalAddresses.streetName:\"{{strNme}}\")^15 OR (phone_key:\"{{phone}}\")^50 OR (mailAddresses.poBox:\"{{poBox}}\")^6 ) "}},"sort":[{"_score":{"order":"desc"}}],"aggs":{"exctNme":{"filter":{"term":{"norm_custnme":"{{exctNme}}"}}}}}"
}

When trying to execute the query from the java API

	//Creating a transport client instance to establish remote connection
	TransportClient client;
	// Applying the cluster name to the settings
	Settings settings = Settings.builder()
	        .put("cluster.name", "elasticsearch").build();
	try {
				
		
		
		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("phyCity", "Newyork");			
		
				
		
		
		// Building the query
		@SuppressWarnings("deprecation")
		QueryBuilder qb = new TemplateQueryBuilder(
			    "lookup_temp",                  
			    ScriptService.ScriptType.STORED,      
			    template_params);
		
		// Executing the template query
		SearchResponse response = client.prepareSearch()
	                .setIndices("match")
	                .setTypes("candidate")
	                .setQuery(qb).execute().actionGet();

		System.out.println(response);
		client.close();
			
	} catch (Exception e) {
		e.printStackTrace();
		
	}

Getting error Caused by: RemoteTransportException[[IhhQQOk][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: ElasticsearchParseException[Failed to derive xcontent];
Caused by: ElasticsearchParseException[Failed to derive xcontent].

What is the error we are getting? Could somebody help on this .

It looks like you are mixing the deprecated query template with search templates. The API you are using is about query templates, but such templates are only about the query, yet your template includes other parameters like size?

We tried removing the size parameter though we are getting the same error

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