Template Query vs. Search Template


(Lukáš Vlček) #1

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
[2]
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAO9cvUYh62fLpJL3uLuYLaEGGiNX96o1UquGbBKGjETZT7_8vg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Sofiane Cherchalli) #2

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/5811dc41-3b41-485f-994a-80facd80ab03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Templates for Filters
(Stephan Beutel) #3

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


(system) #4