# Sorting on missing value with default fallback

**URL:** <https://discuss.elastic.co/t/sorting-on-missing-value-with-default-fallback/105021>\
**Category:** Elasticsearch\
**Created:** [October 24, 2017, 9:00am UTC](https://discuss.elastic.co/t/sorting-on-missing-value-with-default-fallback/105021 "2017-10-24T09:00:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![supernovae](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@supernovae](https://discuss.elastic.co/u/supernovae)\
**Post date:** [October 24, 2017, 9:00am UTC](https://discuss.elastic.co/t/sorting-on-missing-value-with-default-fallback/105021/1 "2017-10-24T09:00:40Z")

</div>

Hi, is there a way in elasticsearch to "fallback" on a default field value when sorting on a missing field?

For instance, let assume I have a field `A` and a field `B` in my mapping, which are mutually exclusive so that each document in my index has either `A` or `B` defined. I can sort singularly on both of them, but is there a way of combining both at the same time? Something like, sort on field `A` if present, otherwise sort on field `B`.

I tried using inline script but without luck so far. Is there even a way of achieving what I would like to do?

Thank you.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [October 27, 2017, 5:14pm UTC](https://discuss.elastic.co/t/sorting-on-missing-value-with-default-fallback/105021/2 "2017-10-27T17:14:08Z")

</div>

You can specify multiple sort values: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#\_missing\_values](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values)

The sorts are applied sequentially. E.g. this:

```auto
GET /my_index/my_type/_search
{
    "sort" : [
        { "field_a" : {"order" : "asc"}},
        { "field_b" : {"order" : "desc"}},
        "_score"
    ],
    "query" : {
        "term" : { "user" : "foo" }
    }
}

```

will first sort documents by `field_a` ascending. By default, all documents missing `field_a` will be sorted to the last position (you can [change that behavior if you want](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values)). Next, all ties are sorted by `field_b` descending, which includes the documents that were lacking `field_a` (because they are all tied for last). Finally, any remaining ties are sorted by their natural full-text score.

---

<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:** [November 24, 2017, 5:14pm UTC](https://discuss.elastic.co/t/sorting-on-missing-value-with-default-fallback/105021/3 "2017-11-24T17:14:28Z")

</div>

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