# Multiple nested sorts dont work

**URL:** <https://discuss.elastic.co/t/multiple-nested-sorts-dont-work/26539>\
**Category:** Elasticsearch\
**Created:** [July 30, 2015, 3:15am UTC](https://discuss.elastic.co/t/multiple-nested-sorts-dont-work/26539 "2015-07-30T03:15:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![harpreet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harpreet/32/3413_2.png) [@harpreet](https://discuss.elastic.co/u/harpreet)\
**Post date:** [July 30, 2015, 3:15am UTC](https://discuss.elastic.co/t/multiple-nested-sorts-dont-work/26539/1 "2015-07-30T03:15:37Z")

</div>

we have a situation where treatment has a price and hospital may or may not want to display it.  
so there is a `price` field PLUS a `lp_low_priority` field.  
value of `lp_low_priority` is 1 (true) when `price` is not set(price is\_null).

Vendor doc is saved with its nested products.  
when user searches for treatment he get list of hospitals with minimum price of the treatment.  
now the sort works fine.  
BUT i want the hospital with that treatment with the `lp_low_priority` = `1` to come at last.

```
    {
      "properties":{
        .....
          
          "treatments":{
              "properties":{
                  "child_of":{
                      "type":"integer"
                  },
                  "hospital_id":{
                      "type":"integer"
                  },
                  "id":{
                      "type":"integer"
                  },
                  "keywords":{
                      "type":"string"
                  },
              "procedure_count": {
                "type": "long"
              },
                  "status":{
                      "type":"string",
                 "index":"not_analyzed"
                  },
                  "treatment_id":{
                      "type":"integer"
                  },
                  "treatment_name":{
                      "type":"string"
                  },
                  "treatment_price":{
                      "type":"integer"
                  },
                  "treatment_slug":{
                      "type":"string",
                      "index":"not_analyzed"
                  },
                  "treatment_status":{
                      "type":"string",
                 "index":"not_analyzed"
                  },
              "low_priority": {
                "type": "integer"
              },
              "lowest_price": {
                "type": "integer"
              },
              "highest_price": {
                "type": "integer"
              },
              "lp_low_priority": {  
                "type": "integer"
              },
              "hp_low_priority": {  
                "type": "integer"
              }
              }
          }
      }
  }

```

the code to search it is like

```
 {
          "sort": [
            {
              "treatments.lowest_price": {
                "nested_filter": {
                  "term": {
                    "treatments.treatment_slug": "heart-surgery"
                  }
                },
                "mode": "avg",
                "order": "asc"
              }
            },
            {
              "treatments.lp_low_priority": {
                "order": "asc",
                "nested_filter": {
                  "term": {
                    "treatments.treatment_slug": "heart-surgery"
                  }
                },
                "mode": "max"
              }
            }
          ],
          "query": {
            "filtered": {
              "filter": [
                {
                  "term": {
                    "treatments.treatment_slug": "heart-surgery"
                  }
                },
                {
                  "term": {
                    "treatments.status": "active"
                  }
                },
                {
                  "term": {
                    "treatments.treatment_status": "active"
                  }
                },
                {
                  "term": {
                    "hospital_status": "active"
                  }
                },
                {
                  "terms": {
                    "location.country": [
                      "India"
                    ]
                  }
                }
              ]
            }
          }
        }

```

the result is way too weird.  
if I only use

```
 {
              "sort": [
                {
                  "treatments.lowest_price": {
                    "nested_filter": {
                      "term": {
                        "treatments.treatment_slug": "heart-surgery"
                      }
                    },
                    "mode": "avg",
                    "order": "asc"
                  }
                }

The sorting is in order but then you see the `lp_low_priority` come first in order, which is OK(but not the requirement).

```

can i use multiple sorts?

---

<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:58pm UTC](https://discuss.elastic.co/t/multiple-nested-sorts-dont-work/26539/2 "2017-07-05T23:58:11Z")

</div>


