# Sorting all hits by a nested field value

**URL:** <https://discuss.elastic.co/t/sorting-all-hits-by-a-nested-field-value/202336>\
**Category:** Elasticsearch\
**Created:** [October 4, 2019, 11:41am UTC](https://discuss.elastic.co/t/sorting-all-hits-by-a-nested-field-value/202336 "2019-10-04T11:41:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bvarga](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bvarga](https://discuss.elastic.co/u/bvarga)\
**Post date:** [October 4, 2019, 11:41am UTC](https://discuss.elastic.co/t/sorting-all-hits-by-a-nested-field-value/202336/1 "2019-10-04T11:41:10Z")

</div>

Hi!

Is it possible to sort a query result by the value of a nested field, across all hits?

To give you an example, let's say I am storing sellers and their products. A document stores the information of a single seller, including a list of their products as nested documents. So the mapping would be something like this:

```
{
  "mapping": {
    "properties": {
      "name": {
        "type": "text"
      },
      "products": {
        "type": "nested",
        "properties": {
          "name": {
            "type": "text"
          },
          "price": {
            "type": "integer"
          }
        }
      }
    }
  }
}

```

My question is, how do I retrieve all products and their vendors, listed from most expensive to cheapest? If I were working with a relational database, products and sellers would be stored in separate tables and my SQL query would look like this:

```
SELECT products.name, products.price, sellers.name
FROM products INNER JOIN sellers ON products.seller_id=sellers.id
ORDER BY products.price DESC;

```

Is it possible to achieve the same thing in Elasticsearch, with the mapping shown above? I am aware of nested queries, but so far I have only been able to sort the inner hits within each document. So I can get a result like this:

```
Seller A
	- Product 3, $100
	- Product 1, $80
	- Product 2, $10
Seller B
	- Product 5, $90
	- Product 4, $5

```

But what I need is this:

```
Product 3, Seller A, $100
Product 5, Seller B, $90
Product 1, Seller A, $80
Product 2, Seller A, $10
Product 4, Seller B, $5

```

Is this possible? Any help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [October 4, 2019, 12:26pm UTC](https://discuss.elastic.co/t/sorting-all-hits-by-a-nested-field-value/202336/2 "2019-10-04T12:26:09Z")

</div>

Why not store it as 5 separate documents (as in the results you are looking for) instead of using a single nested one?

---

<div class="post-metadata">

**Author:** ![bvarga](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bvarga](https://discuss.elastic.co/u/bvarga)\
**Post date:** [October 4, 2019, 1:00pm UTC](https://discuss.elastic.co/t/sorting-all-hits-by-a-nested-field-value/202336/3 "2019-10-04T13:00:27Z")

</div>

Thanks for the suggestion! In the simple example I have given, this would indeed be feasible. In my  
actual use-case however, "flattening" the mapping would introduce a **huge** redundancy. I have hundreds of fields, several of them are nested or vector-valued. (Let's say the sellers not only have names, but also addresses, phone numbers, etc... repeating all of these for every product would really blow up the size of the index.)

---

<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:** [November 1, 2019, 1:00pm UTC](https://discuss.elastic.co/t/sorting-all-hits-by-a-nested-field-value/202336/4 "2019-11-01T13:00:28Z")

</div>

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