Sorting in template queries


(John D. Ament) #1

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.


(Martijn Van Groningen) #2

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

Use put template api:

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:

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

(John D. Ament) #3

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.


(system) #4