# Elasticsearch .net conditional sort

**URL:** <https://discuss.elastic.co/t/elasticsearch-net-conditional-sort/120368>\
**Category:** Elasticsearch\
**Created:** [February 18, 2018, 8:50pm UTC](https://discuss.elastic.co/t/elasticsearch-net-conditional-sort/120368 "2018-02-18T20:50:42Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 21, 2018, 12:00am UTC](https://discuss.elastic.co/t/elasticsearch-net-conditional-sort/120368/4 "2018-02-21T00:00:33Z")

</div>

Glad you got it working 😄

> [@Orhun\_Karapinar](#):
>
> can I use the same trick for date range as well?

Not for a `range` query, no. You could write a [`script` query](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/query-dsl-script-query.html) to do this but similarly to script sorting, the performance of the query would not be as good as using the `range` query, but the performance might be acceptable for your use case. You can write a compound query however that combines

- a `range` query on `shipment_date` with a `term` query on the `sale*` `_index` (both as `filter` clauses in a `bool` query)

with

- a `range` query on `created_date` with a `term` query on the `order*` `_index` (both as `filter` clauses in a `bool` query)

both as `should` clauses in a `bool` query. [Here's something similar as an example](https://discuss.elastic.co/t/multi-index-search-multiple-queries-within-the-query/119564/2).

---

_[View the full topic](https://discuss.elastic.co/t/elasticsearch-net-conditional-sort/120368)._
