# Sort results by inner hits (min/max)

**URL:** <https://discuss.elastic.co/t/sort-results-by-inner-hits-min-max/342359>\
**Category:** Elasticsearch\
**Created:** [September 5, 2023, 4:11pm UTC](https://discuss.elastic.co/t/sort-results-by-inner-hits-min-max/342359 "2023-09-05T16:11:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![LeoAdamek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leoadamek/32/125346_2.png) [@LeoAdamek](https://discuss.elastic.co/u/LeoAdamek)\
**Post date:** [September 5, 2023, 4:11pm UTC](https://discuss.elastic.co/t/sort-results-by-inner-hits-min-max/342359/1 "2023-09-05T16:11:18Z")

</div>

I'm using a join field and an `has_child` filter in my search to join products with their various configuration permutations.  
There's a single level join from a product to a configuration. When a user searches for something the `has_child` filter is used to return only the matching products with only the matching configurations.

The configurations have different prices and I'm wondering how to sort the products by the price, and how to set the sort mode (e.g. "min" or "max") so that if a user sorts by "Price (Low-High)" it sorts by "configurations.price" using the "min" mode "asc".

I've had a look around various docs and I can't see how to do this, I only found some unanswered posts here but really need to get this working.

Here's an example query which would need to be sorted:

```auto
GET /tours/_search
{
  "sort": [
   // What to put here to sort the results by the inner date or price fields?
  ],
  "query": {
    "bool": {
      "should": [
        {
          "match_all": {}
        }
      ],
      "filter": [
        {
          "match": {
            "organization_id": "01h90c44kh5h92qykyqdjh5htn"
          }
        },
        {
          "has_child": {
            "inner_hits": {
              "size": 100,
              "sort": {
                "dates": "asc"
              }
            },
            "type": "departure",
            "query": {
              "bool": {
                "should": [
                  {
                    "match_all": {}
                  }
                ],
                "filter": [
                  {
                    "term": {
                      "published": true
                    }
                  },
                  {
                    "bool": {
                      "should": [
                        {
                          "bool": {
                            "filter": [
                              {
                                "term": {
                                  "kind": "fixed"
                                }
                              },
                              {
                                "range": {
                                  "dates": {
                                    "gte": "now",
                                    "relation": "within"
                                  }
                                }
                              }
                            ]
                          }
                        },
                        {
                          "bool": {
                            "filter": [
                              {
                                "term": {
                                  "kind": "fixed_duration"
                                }
                              },
                              {
                                "range": {
                                  "dates": {
                                    "gte": "now",
                                    "relation": "intersects"
                                  }
                                }
                              }
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "exists": {
                      "field": "lead_prices.GBP"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

```

---

<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 3, 2023, 4:12pm UTC](https://discuss.elastic.co/t/sort-results-by-inner-hits-min-max/342359/2 "2023-10-03T16:12:01Z")

</div>

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