Search template

Why is this working

GET _search/template
{
  "source": "{ \"query\": { \"terms\": {{#toJson}}statuses{{/toJson}} }}",
  "params": {
    "statuses" : {
        "status": [ "pending", "published" ]
    }
  }
}

but this is not

GET _search/template
{
  "source": {
    "query": {
      "terms": "{{#toJson}}statuses{{/toJson}}"
    }
  },
  "params": {
    "statuses": {
      "status": [
        "pending",
        "published"
      ]
    }
  }
}

and Im getting

{
        "type": "parsing_exception",
        "reason": "[terms] query malformed, no start_object after query name",
        "line": 1,
        "col": 19
      }

GET _search/template
{
"source": "{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}",
"params": {
"statuses" : {
"status": [ "pending", "published" ]
}
}
}
The above query will work perfectly. It will be given as sample structure how it will be rendered.

Syntax of Converting parameters to JSON:
{
"source": "{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}",
"params": {
"statuses" : {
"status": [ "pending", "published" ]
}
}
}

How syntax will be rendered after converted to JSON:
params": {
"statuses": {
"status": [
"pending",
"published"

So using this structure inside our Query DSL giving error as malformed query.

On my elastic 6.6.2 this query

GET _search/template
{
"source": "{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}",
"params": {
"statuses" : {
"status": [ "pending", "published" ]
}
}
}

gives me error

{
"error": {
"root_cause": [
{
"type": "json_parse_exception",
"reason": "Unexpected character ('q' (code 113)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@30e5dc17; line: 2, column: 16]"
}
],
"type": "json_parse_exception",
"reason": "Unexpected character ('q' (code 113)): was expecting comma to separate Object entries\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@30e5dc17; line: 2, column: 16]"
},
"status": 500
}

Kindly use this query in this syntax it seems working.
GET _search/template
{
"source": "{ "query": { "terms": {{#toJson}}statuses{{/toJson}} }}",
"params": {
"statuses" : {
"status": [ "pending", "published" ]
}
}
}

This is not the same query, the one that You execute has escaped quotes.
But ok, I think it's not possible to do this in such way.

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