# 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:** 1\
**Showing post:** 2

<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).

---

_[View the full topic](https://discuss.elastic.co/t/template-query-vs-search-template/20517)._
