# Sort grouped search result by formula

**URL:** <https://discuss.elastic.co/t/sort-grouped-search-result-by-formula/198223>\
**Category:** Elasticsearch\
**Created:** [September 5, 2019, 10:31am UTC](https://discuss.elastic.co/t/sort-grouped-search-result-by-formula/198223 "2019-09-05T10:31:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuriiH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yuriih/32/52227_2.png) [@yuriiH](https://discuss.elastic.co/u/yuriiH)\
**Post date:** [September 5, 2019, 10:31am UTC](https://discuss.elastic.co/t/sort-grouped-search-result-by-formula/198223/1 "2019-09-05T10:31:17Z")

</div>

Hello.  
I am trying to implement query which will sort results by the formula.

For example, we have the next entities:  
{  
"price":"1000",  
"zip":"77777",  
"field1":"1",  
"field2":"5"  
},  
{  
"price":"2222",  
"zip":"77777",  
"field1":"2",  
"field2":"5"  
},  
{  
"price":"1111",  
"zip":"77777",  
"field1":"1",  
"field2":"5"  
}

The first one I need to group result by field1 and field2 and then calculate the average price for each group.

Then I need to divide the price of each doc by average price value and sort documents based on this value.

Now, my query without sorting looks like:

```
    POST /entities/_search {
        "size": 0,
        "query": {
            "term": {
                "zip": {
                    "value": "77777"
                }
            }
        },
        "aggs": {
            "my composite": {
                "composite": {
                    "size": 500,
                    "sources": [{
                            "field1_term": {
                                "terms": {
                                    "field": "field1"
                                }
                            }
                        },
                        {
                            "field2_term": {
                                "terms": {
                                    "field": "field2"
                                }
                            }
                        }
                    ]
                },
                "aggs": {
                    "avg_price_per_group": {
                        "avg": {
                            "field": "price"
                        }
                    },
                    "results_per_group": {
                        "top_hits": {
                            "size": 100,
                            "_source": {
                                "include": ["entity_id", "price"]
                            }
                        }
                    }
                }
            }
        }
    }

```

Is it possible to do this somehow?

---

<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, 2019, 10:31am UTC](https://discuss.elastic.co/t/sort-grouped-search-result-by-formula/198223/2 "2019-10-03T10:31:28Z")

</div>

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