# Order Terms aggregations buckets by top hits sub aggregation

**URL:** <https://discuss.elastic.co/t/order-terms-aggregations-buckets-by-top-hits-sub-aggregation/182410>\
**Category:** Elasticsearch\
**Created:** [May 23, 2019, 11:06am UTC](https://discuss.elastic.co/t/order-terms-aggregations-buckets-by-top-hits-sub-aggregation/182410 "2019-05-23T11:06:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PPraveenKumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ppraveenkumar/32/46712_2.png) [@PPraveenKumar](https://discuss.elastic.co/u/PPraveenKumar)\
**Post date:** [May 23, 2019, 11:06am UTC](https://discuss.elastic.co/t/order-terms-aggregations-buckets-by-top-hits-sub-aggregation/182410/1 "2019-05-23T11:06:56Z")

</div>

I have a query,

Context:  
These is one Tour Packages Index, with one of property "set\_url", multiple packages with same set\_url are called family packages.

Requirement:  
Getting seo packages in sorting order of any field lets say duration (indexed as type: integer), but sorting will be based on min priced package per family and wants result in size of 20.

What I am trying to do:

1. Apply some filters

2. apply terms top level aggregation on key set\_url, making family packages buckets, Now I want to order these buckets across based on min prices family package that comes from top hits aggregation and top hits aggregation is metric aggregation according of ES docs.

3. These is sub aggregation of type top hits that does sorting on price on ascending order and take one size to pick min prices family package.

Current Query:

{  
index: "seo\_packages",  
type: "seo\_package",  
body: {  
size: 0,  
query: {  
bool: {  
filter: {  
and: [{  
terms: {  
package\_type: ["seo"]  
}  
}, {  
terms: {  
categories: ["tour"]  
}  
}, {  
term: {  
is\_published: true  
}  
},  
{  
terms: {  
destination\_names: ["Kerala"]  
}  
}  
],  
},  
must\_not: [{  
term: {  
price\_expression: 0  
}  
}]  
}  
},  
aggs: {  
group\_by\_set\_url: {  
terms: {  
field: "set\_url",  
size: 10,  
order: { "min\_price\_expressions. **how to access doc source property duration here**" =\> "asc" }  
},  
aggs: {  
min\_price\_expressions: {  
top\_hits: {  
sort: [{  
price\_expression: {  
order: "asc"  
}  
}],  
\_source: {  
includes: [:id, : **duration**]  
},  
size: 1  
},  
}  
}  
}  
}  
}  
}

What should be order clause here to access top hits sub aggregation document's property?

Thanks in advance.

---

<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:** [June 20, 2019, 11:06am UTC](https://discuss.elastic.co/t/order-terms-aggregations-buckets-by-top-hits-sub-aggregation/182410/2 "2019-06-20T11:06:58Z")

</div>

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