# Elasticsearch error when executing a template query from java client

**URL:** <https://discuss.elastic.co/t/elasticsearch-error-when-executing-a-template-query-from-java-client/67157>\
**Category:** Elasticsearch\
**Created:** [November 25, 2016, 6:34am UTC](https://discuss.elastic.co/t/elasticsearch-error-when-executing-a-template-query-from-java-client/67157 "2016-11-25T06:34:03Z")\
**Posts on this page:** 4\
**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 25, 2016, 6:34am UTC](https://discuss.elastic.co/t/elasticsearch-error-when-executing-a-template-query-from-java-client/67157/1 "2016-11-25T06:34:03Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [November 25, 2016, 10:08am UTC](https://discuss.elastic.co/t/elasticsearch-error-when-executing-a-template-query-from-java-client/67157/2 "2016-11-25T10:08:05Z")

</div>

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`?

---

<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, 5:55am UTC](https://discuss.elastic.co/t/elasticsearch-error-when-executing-a-template-query-from-java-client/67157/3 "2016-11-28T05:55:25Z")

</div>

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

---

<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 26, 2016, 5:55am UTC](https://discuss.elastic.co/t/elasticsearch-error-when-executing-a-template-query-from-java-client/67157/4 "2016-12-26T05:55:25Z")

</div>

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