# Sorting by 2 fields failing

**URL:** <https://discuss.elastic.co/t/sorting-by-2-fields-failing/101464>\
**Category:** Elasticsearch\
**Created:** [September 22, 2017, 9:51am UTC](https://discuss.elastic.co/t/sorting-by-2-fields-failing/101464 "2017-09-22T09:51:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Declan\_Mungovan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/declan_mungovan/32/22412_2.png) [@Declan\_Mungovan](https://discuss.elastic.co/u/Declan_Mungovan)\
**Post date:** [September 22, 2017, 9:51am UTC](https://discuss.elastic.co/t/sorting-by-2-fields-failing/101464/1 "2017-09-22T09:51:49Z")

</div>

I'm using a Nest SearchDescriptor to retrieve different document types from an index. The documents are: contact and invoice. Currently the search descriptor sorts on two different fields. I've simplified the search descriptor to show how the sorting works:

```
     .Sort(st => st.Field(f => f.Field("_score").Descending()))
     .Sort(st => st.Field(f2 => f2.Field("invoiceDate").Descending().IgnoreUnmappedFields()))

```

The document type contact doesn't contain a field for "invoiceDate" (only the invoice document does). I find that when this second sort is added it messes up with the sorting for score with contacts (but not invoices that contains this information) i.e. just having the single \_score sorting returns the highest ranking document in contacts, but adding the secondary sorting field ("invoiceDate") now results in the highest score contact document not being returned in order.

I also tried adding the .MissingLast() property and multiple different combinations of properties on the SearchDescriptor but to no avail. When I replace the secondary search field with one that is actually in contact (e.g. contactName, or even an empty string) then I find that the correctly ordered results are returned.

I had asked this question on stackoverflow but there was no solution found:

> <https://stackoverflow.com/questions/46222510/secondary-sorting-not-working-correctly-for-elasticsearch-documents>

---

<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:** [October 20, 2017, 9:52am UTC](https://discuss.elastic.co/t/sorting-by-2-fields-failing/101464/2 "2017-10-20T09:52:46Z")

</div>

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