# Template Query vs. Search Template

**URL:** <https://discuss.elastic.co/t/template-query-vs-search-template/20517>\
**Category:** Elasticsearch\
**Created:** [October 31, 2014, 2:13pm UTC](https://discuss.elastic.co/t/template-query-vs-search-template/20517 "2014-10-31T14:13:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukas\_Vlcek1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lukas_vlcek1/32/819_2.png) [@Lukas\_Vlcek1](https://discuss.elastic.co/u/Lukas_Vlcek1)\
**Post date:** [October 31, 2014, 2:13pm UTC](https://discuss.elastic.co/t/template-query-vs-search-template/20517/1 "2014-10-31T14:13:00Z")

</div>

Hi,

I am bit confused about what is the distinction between Template Query [1]  
and Search Template [2]. To me there seem to be some overlap in searching  
capabilities in both. Is the main difference that the template query is a  
query type while the search template adds a new REST end point that can be  
used for search template management? Is there any other difference?

Is Mustache templating functionality the same in both APIs? The [1] refers  
to [2] saying "[search template] allows you to template an entire search  
request" - is this something template query can not do?

[1]  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-template-query.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-template-query.html)  
[2]  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html)

Thanks,  
Lukas

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAO9cvUYh62fLpJL3uLuYLaEGGiNX96o1UquGbBKGjETZT7\_8vg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAO9cvUYh62fLpJL3uLuYLaEGGiNX96o1UquGbBKGjETZT7_8vg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Sofiane\_Cherchalli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sofiane_cherchalli/32/1129_2.png) [@Sofiane\_Cherchalli](https://discuss.elastic.co/u/Sofiane_Cherchalli)\
**Post date:** [October 31, 2014, 11:12pm UTC](https://discuss.elastic.co/t/template-query-vs-search-template/20517/2 "2014-10-31T23:12:30Z")

</div>

Basically, both query and search template achieve the same results. The  
difference is in the "how" they achieve it.

Search template separates the search template from the search request. The  
search template is stored once under the .script index, and all the search  
request reference the template name and provide value binding for the  
template. The search template allows you to save bandwidth.

In the query tamplate you send both the template and value binding in the  
same request. It consumes more bandwidth, but doesn't require you to store  
anything on ES before sending the request.

Example:

PUT blogs/post/1  
{  
"title": "title1",  
"desc": "desc1",  
"visible": true  
}

PUT blogs/post/2  
{  
"title": "title2",  
"desc": "desc2",  
"visible": false  
}

PUT blogs/post/3  
{  
"title": "title2",  
"desc": "desc2",  
"visible": true  
}

## Template query

GET /blogs/post/_search  
{  
"query": {  
"template": {  
"query": {  
"filtered": {  
"query": {  
"match_{{template}}": {}  
},  
"filter": {  
"bool": {  
"must": {  
"term": {  
"visible": "{{visible}}"  
}  
}  
}  
}  
}  
},  
"params" : {  
"template": "all",  
"visible": true  
}  
}  
}  
}

## 

## Search template

# Store the tamplate under .scripts index

POST /_search/template/query\_template  
{  
"query": {  
"filtered": {  
"query": {  
"match_{{template}}": {}  
},  
"filter": {  
"bool": {  
"must": {  
"term": {  
"visible": "{{visible}}"  
}  
}  
}  
}  
}  
}  
}

# Run the query

GET /blogs/post/\_search/template  
{  
"template": {  
"id": "query\_template"  
},  
"params" : {  
"template": "all",  
"visible": true  
}  
}

## 

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5811dc41-3b41-485f-994a-80facd80ab03%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5811dc41-3b41-485f-994a-80facd80ab03%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Stephan\_Beutel](https://avatars.discourse-cdn.com/v4/letter/s/d26b3c/32.png) [@Stephan\_Beutel](https://discuss.elastic.co/u/Stephan_Beutel)\
**Post date:** [January 31, 2016, 3:09pm UTC](https://discuss.elastic.co/t/template-query-vs-search-template/20517/3 "2016-01-31T15:09:38Z")

</div>

Hi, I try to convert a template query to a search query to use the csv converter.

My template query looks like this:  
curl -XGET 'localhost:9200/logstash-\*/\_search/template?pretty' -d '  
{  
"file": "yJmeterResult"  
}'

Regrading to QueryDSL documentation, the search query for the template should look like this:  
([https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html))  
curl -XGET 'localhost:9200/logstash-\*/\_search' -d '  
{  
"query": {  
"template": {  
"file": "yJmeterResult"  
}  
}  
}'

But I only get an error:  
RemoteTransportException[[Everyman][localhost/127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: SearchParseException[failed to parse search source [  
{  
"query": {  
"template": {  
"file": "yJmeterResult"  
}  
}  
}]]; nested: QueryParsingException[[\_na] query malformed, no field after start\_object]; }

Any hints how the search query must look like to execute the template?

Thanks for help.

---

<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:** [July 5, 2017, 11:20pm UTC](https://discuss.elastic.co/t/template-query-vs-search-template/20517/4 "2017-07-05T23:20:05Z")

</div>


