# Search template not working with terms query

**URL:** <https://discuss.elastic.co/t/search-template-not-working-with-terms-query/99408>\
**Category:** Elasticsearch\
**Created:** [September 5, 2017, 11:09am UTC](https://discuss.elastic.co/t/search-template-not-working-with-terms-query/99408 "2017-09-05T11:09:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Narmatha](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Narmatha](https://discuss.elastic.co/u/Narmatha)\
**Post date:** [September 5, 2017, 11:09am UTC](https://discuss.elastic.co/t/search-template-not-working-with-terms-query/99408/1 "2017-09-05T11:09:48Z")

</div>

I have an index with a long field and i am just trying to use search template to use terms query but it throws exception.

"pid": {  
"type": "long"  
}  
Search Template: PUT /\_search/template/article\_query\_template

{  
"template": {  
"query": {  
"terms": {  
"pid": "{{articleId}}"  
}  
}  
}  
}  
Search Query :

POST test2\*/\_search

{  
"query": {  
"template": {  
"id": "article\_query\_template",  
"params" : {  
"articleId" : [1, 2]  
}  
}  
}  
}  
Exception : reason": "[terms] query does not support [pid]".

Its working without template. How to fix this issue.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 6, 2017, 5:06pm UTC](https://discuss.elastic.co/t/search-template-not-working-with-terms-query/99408/2 "2017-09-06T17:06:32Z")

</div>

Since you are passing a json array, you need to escape it:

```auto
GET _search/template
{
  "template": """{
    "query": {
      "terms": {
        "pid": {{#toJson}}articleId{{/toJson}}
      }
    }
  }""",
  "params": {
    "articleId": [
      1,
      2
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Narmatha](https://avatars.discourse-cdn.com/v4/letter/n/d26b3c/32.png) [@Narmatha](https://discuss.elastic.co/u/Narmatha)\
**Post date:** [September 7, 2017, 9:29am UTC](https://discuss.elastic.co/t/search-template-not-working-with-terms-query/99408/3 "2017-09-07T09:29:30Z")

</div>

> [@jpountz](#):
>
> {{#toJson}}articleId{{/toJson}}

It shows error without quotes {{#toJson}}articleId{{/toJson}}.

Also array search thows exception "reason": "For input string: ""articleId""".

The field is of type long.

---

<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:** [October 5, 2017, 9:30am UTC](https://discuss.elastic.co/t/search-template-not-working-with-terms-query/99408/4 "2017-10-05T09:30:06Z")

</div>

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