# Elasticsearch 6.2.2 conditional sorting

**URL:** <https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144>\
**Category:** Elasticsearch\
**Created:** [March 15, 2018, 4:55pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144 "2018-03-15T16:55:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [March 15, 2018, 4:55pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144/1 "2018-03-15T16:55:05Z")

</div>

I'm on Elasticsearch 6.2.2.  
Could I do differents sorts based on a field of an index?

#Here I want to sort by age if code=child or by city if code=adult

```
GET index1/_search
{
  "query": {
    "match_all": {}
    },
    "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "source": "if (ctx._source.code ==~ params.foo) {{doc['age'].value} if (ctx._source.code ==~ params.bar) {doc['city'].value} else {ctx.op = 'noop'}",
            "params": {
           "foo": "child"
           "bar": "adult"
            }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [March 16, 2018, 9:22am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144/2 "2018-03-16T09:22:05Z")

</div>

Did I missed a detail or is it impossible to do in Elasticsearch?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [March 16, 2018, 9:36am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144/3 "2018-03-16T09:36:34Z")

</div>

You can may be do that with:

- function score query
- scripted score with painless

---

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [March 20, 2018, 9:46am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144/4 "2018-03-20T09:46:29Z")

</div>

Thanks for the answers @dadoonet  
I get how I can sort by scoring but not how the scoring could be influenced by one of the field of the said index.  
Could something like this work (retaking the above example)?

```
"function_score": {
          "query": { "match_all": {} },
          "boost": "5", 
          "functions": [
 {
                  "filter": { "match": { "code": "child" } },
                    "sort": [
                    { "age" : "desc" }]
  }
    { 
       another filter with another sort
   }
},

```

There is an error that said that I should give a query for the sort since I extracted it to put it in the filter.

Moreover do you know why the example in the first message...:

```
GET index1/_search
{
  "query": {
    "match_all": {}
    },
    "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "source": "if (ctx._source.code ==~ params.foo) {{doc['age'].value} if (ctx._source.code ==~ params.bar) {doc['city'].value} else {ctx.op = 'noop'}",
            "params": {
           "foo": "child",
           "bar": "adult"
            }
      }
    }
  }
} 

```

...would fail with the error :

```
  "type": "script_exception",
  "reason": "compile error",
  "script_stack": [
    "... ype_ent ==~ params.foo) {{doc['nom'].value} if (ct ...",
    " ^---- HERE"

```

Thanks, merci.

---

<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:** [April 17, 2018, 9:46am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-2-conditional-sorting/124144/5 "2018-04-17T09:46:52Z")

</div>

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