# Sort by multiple values based on nested document

**URL:** https://discuss.elastic.co/t/sort-by-multiple-values-based-on-nested-document/156456
**Category:** Elasticsearch
**Created:** [November 13, 2018, 11:13am UTC](https://discuss.elastic.co/t/sort-by-multiple-values-based-on-nested-document/156456 "2018-11-13T11:13:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![pyk](https://avatars.discourse-cdn.com/v4/letter/p/c4cdca/32.png) [@pyk](https://discuss.elastic.co/u/pyk)
#### Post date: [November 13, 2018, 11:13am UTC](https://discuss.elastic.co/t/sort-by-multiple-values-based-on-nested-document/156456/1 "2018-11-13T11:13:24Z")

</div>

I'm tryin to move my sorting mechanism from application to elasticseach but with no result. All i want to do is sort returned docs in elastic by nested item price and available quantity. Just simple as sql-type order like

> order by Quantity desc, Price asc

Here is shortened elasticsearch return in Kibana

```
"_index": "product_index",
"_id": "1",        
"_source": {                       
    "id": 1
    },
    "sort": [          
          5.9
        ],
        "inner_hits": {
          "variants": {
            "hits": {
              "total": 2,
              "hits": [
                {
                  "_nested": {
                    "field": "variants",
                    "offset": 2
                  },
                  "_score": 0,
                  "_source": {                    
                    "offer_state": 3,
                    "stock_quantity": 2,                    
                    "gross_price": 5.9,                                     
                  }
                },
                {
                  "_nested": {
                    "field": "variants",
                    "offset": 1
                  },
                  "_score": 0,
                  "_source": {                    
                    "offer_state": 3,
                    "stock_quantity": 5,                    
                    "gross_price": 21.9,                    
                  }
                }
              ]
            }
          }
        }

```

and sort query passed to elastic

```
"sort": [
    {
      "variants.gross_price": {
        "order": "asc",
        "nested_filter": {
          "bool": {
            "filter": [
              {
                "term": {
                  "variants.offer_state": {
                    "value": 3
                  }
                }
              },
              {
                "range": {
                  "variants.gross_price": {
                    "gt": "0"
                  }
                }
              }              
            ]
          }
        },
        "nested_path": "variants"
      }
    }
  ]

```

How can i include cascade sorting based on nested document? My expected result is 21.9 in returned "sort": []. Thanks for reply.

---

<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: [December 20, 2018, 6:54am UTC](https://discuss.elastic.co/t/sort-by-multiple-values-based-on-nested-document/156456/3 "2018-12-20T06:54:00Z")

</div>

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