# Search template

**URL:** <https://discuss.elastic.co/t/search-template/183698>\
**Category:** Elasticsearch\
**Created:** [May 31, 2019, 10:05am UTC](https://discuss.elastic.co/t/search-template/183698 "2019-05-31T10:05:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![skowron-line](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skowron-line/32/43528_2.png) [@skowron-line](https://discuss.elastic.co/u/skowron-line)\
**Post date:** [May 31, 2019, 10:05am UTC](https://discuss.elastic.co/t/search-template/183698/1 "2019-05-31T10:05:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![saravananselvamohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saravananselvamohan/32/47407_2.png) [@saravananselvamohan](https://discuss.elastic.co/u/saravananselvamohan)\
**Post date:** [June 3, 2019, 8:10am UTC](https://discuss.elastic.co/t/search-template/183698/2 "2019-06-03T08:10:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![skowron-line](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skowron-line/32/43528_2.png) [@skowron-line](https://discuss.elastic.co/u/skowron-line)\
**Post date:** [June 4, 2019, 5:42am UTC](https://discuss.elastic.co/t/search-template/183698/3 "2019-06-04T05:42:49Z")

</div>

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  
> }

---

<div class="post-metadata">

**Author:** ![saravananselvamohan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saravananselvamohan/32/47407_2.png) [@saravananselvamohan](https://discuss.elastic.co/u/saravananselvamohan)\
**Post date:** [June 7, 2019, 2:53am UTC](https://discuss.elastic.co/t/search-template/183698/4 "2019-06-07T02:53:44Z")

</div>

![Search%20Template](https://us1.discourse-cdn.com/elastic/original/3X/7/b/7bccff5d7d509c54be139dbaac2492bb645021aa.png)

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

---

<div class="post-metadata">

**Author:** ![skowron-line](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/skowron-line/32/43528_2.png) [@skowron-line](https://discuss.elastic.co/u/skowron-line)\
**Post date:** [June 7, 2019, 4:32am UTC](https://discuss.elastic.co/t/search-template/183698/5 "2019-06-07T04:32:27Z")

</div>

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.

---

<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, 2019, 4:32am UTC](https://discuss.elastic.co/t/search-template/183698/6 "2019-07-05T04:32:32Z")

</div>

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