# Sorting in template queries

**URL:** <https://discuss.elastic.co/t/sorting-in-template-queries/39296>\
**Category:** Elasticsearch\
**Created:** [January 15, 2016, 3:07am UTC](https://discuss.elastic.co/t/sorting-in-template-queries/39296 "2016-01-15T03:07:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_D\_Ament](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_d_ament/32/764_2.png) [@John\_D\_Ament](https://discuss.elastic.co/u/John_D_Ament)\
**Post date:** [January 15, 2016, 3:07am UTC](https://discuss.elastic.co/t/sorting-in-template-queries/39296/1 "2016-01-15T03:07:35Z")

</div>

I have the following query, which works fine in sense against my cluster.

```
{
  "_source": [
    "record.opened",
    "record.type"
  ],
  "sort": [
    {"record.type":"desc"},
    "record.opened"
  ],
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      }
    }
  }
}

```

It's a straight forward query. I attempted to store this query as a template in my cluster and then invoke it. When I do that, I get an error:

```
org.elasticsearch.ElasticsearchIllegalArgumentException: Unable to find script in : {
"_source": [
"record.opened",
"record.type"
],
"sort": [
{"record.type":"desc"},
"record.opened"
],
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
}
}

```

If I change my sort stanza to become

```
  "sort": [
    "record.type",
    "record.opened"
  ],

```

The query works fine as a template. We're on elasticsearch 1.7.3. Any ideas why sorting desc may not work? I've also tried with the explicit order attribute.

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [January 15, 2016, 9:18am UTC](https://discuss.elastic.co/t/sorting-in-template-queries/39296/2 "2016-01-15T09:18:28Z")

</div>

How did you store the template? If you store it like this it should work:

Use put template api:

```auto
PUT /_search/template/1
{
  "template": {
    "_source": [
      "record.opened",
      "record.type"
    ],
    "sort": [
      {
        "record.type": "desc"
      },
      "record.opened"
    ],
    "query": {
      "filtered": {
        "query": {
          "match_all": {}
        }
      }
    }
  }
}

```

Then use search api with template:

```auto
GET my-index/_search/template
{
    "template": {
        "id": "1"
    },
    "params": {
    }
}

```

---

<div class="post-metadata">

**Author:** ![John\_D\_Ament](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_d_ament/32/764_2.png) [@John\_D\_Ament](https://discuss.elastic.co/u/John_D_Ament)\
**Post date:** [January 15, 2016, 12:47pm UTC](https://discuss.elastic.co/t/sorting-in-template-queries/39296/3 "2016-01-15T12:47:23Z")

</div>

Hi Martjin,

Thanks for the tip. We already have a pretty well defined process for uploading our templates, so I don't think its that. I did however try to switch template syntax, but it gave the same result.

---

<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, 2017, 11:24pm UTC](https://discuss.elastic.co/t/sorting-in-template-queries/39296/4 "2017-07-05T23:24:07Z")

</div>


