# Sort multiple doc types, but different field names

**URL:** <https://discuss.elastic.co/t/sort-multiple-doc-types-but-different-field-names/37812>\
**Category:** Elasticsearch\
**Created:** [December 23, 2015, 3:18am UTC](https://discuss.elastic.co/t/sort-multiple-doc-types-but-different-field-names/37812 "2015-12-23T03:18:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_George](https://avatars.discourse-cdn.com/v4/letter/b/58956e/32.png) [@Ben\_George](https://discuss.elastic.co/u/Ben_George)\
**Post date:** [December 23, 2015, 3:18am UTC](https://discuss.elastic.co/t/sort-multiple-doc-types-but-different-field-names/37812/1 "2015-12-23T03:18:41Z")

</div>

I have two documents that both have date fields that I want to sort by, but they have different names.

I can use unmatched type in the sort eg:

```
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "doctType1DateField": {
        "unmapped_type": "date"
      }
    },
    {
      "doctType2DateField": {
        "unmapped_type": "date"
      }
    }
  ]
}

```

However this will put all the docType1 records (in order) followed by docType2 records (in order).

I know I could add mappings so I end up with a field in both documents with same name, but I want to know if I can achieve a true sort in this case using just a query.

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [December 24, 2015, 5:18pm UTC](https://discuss.elastic.co/t/sort-multiple-doc-types-but-different-field-names/37812/2 "2015-12-24T17:18:25Z")

</div>

You can use script-based sorting:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#\_script\_based\_sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting)

It will have a small cost performance, so avoid it if you are scoring large  
volumes of documents. You can always sort normally and then apply the sort  
script on the top n documents using the rescorer:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-rescore.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-rescore.html)

Ivan

---

<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:28pm UTC](https://discuss.elastic.co/t/sort-multiple-doc-types-but-different-field-names/37812/3 "2017-07-05T23:28:43Z")

</div>


