Order Terms aggregations buckets by top hits sub aggregation

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.

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