# Through sorting by different fields

**URL:** <https://discuss.elastic.co/t/through-sorting-by-different-fields/41864>\
**Category:** Elasticsearch\
**Created:** [February 16, 2016, 10:02am UTC](https://discuss.elastic.co/t/through-sorting-by-different-fields/41864 "2016-02-16T10:02:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![M.ax](https://avatars.discourse-cdn.com/v4/letter/m/e19b73/32.png) [@M.ax](https://discuss.elastic.co/u/M.ax)\
**Post date:** [February 16, 2016, 10:02am UTC](https://discuss.elastic.co/t/through-sorting-by-different-fields/41864/1 "2016-02-16T10:02:47Z")

</div>

I'd like get some sorted docs from elasticsearch. The problem is i'd like to get docs sorted by different fields, but I want to have results in one resultset.  
For example:  
First doc from elastic:

```
0: {
     "_index": "documents"
     "_type": "order"
     "_id": "282"
     "_score": 1
     "_source": {
        "id": "282"
        ...
        "bookingsCost": 2047.21
        "annulmentsCost": 133.56
        "sendToPublisher": 578.05
     }
}

```

Another doc:

```
1: {
     "_index": "documents"
     "_type": "collectedOrdersReport"
     "_id": "287"
     "_score": 1
     "_source": {
        "id": "287"
        ...
        "bookingsCost": 4611.22
        "annulmentsCost": 0
        "sendToPublisher": 4611.22
        "collectedTotalCostWithVat": 272.74
        "collectedTotalVat": 41.61
     }
}

```

Docs with type `order` (like first one) should be sorted by field `sendToPublisher`, but docs with type `collectedOrdersReport` (second) are sorted by field `collectedTotalCostWithVat`. Is there a way to get docs both types in one resultset with a through sorting by different fields?

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [February 17, 2016, 5:38pm UTC](https://discuss.elastic.co/t/through-sorting-by-different-fields/41864/2 "2016-02-17T17:38:28Z")

</div>

Try [script based sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting). Try using `doc['_type']` to get the type and then using the field. You might also want to compare that to just trying to get one of the fields and if it isn't found then trying the other field. In some cases that might be faster.

---

<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:15pm UTC](https://discuss.elastic.co/t/through-sorting-by-different-fields/41864/3 "2017-07-05T23:15:37Z")

</div>


