Skip to content

Cannot sort on scripted_metric aggregations #9620

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
blingwang opened this issue Feb 10, 2015 · 1 comment
Closed

Cannot sort on scripted_metric aggregations #9620

blingwang opened this issue Feb 10, 2015 · 1 comment

Comments

@blingwang
Copy link

I am currently using version 1.4.1. I created a scripted metric aggregations and want to sort the buckets by the scripted metric:

"aggs": {
        "rating_summary": {
            "terms": { "field": "_parent", "order": { "review_score_avg.value": "desc" } },
            "aggs": {
                "total_rating_count": { "sum": { "field": "rating_count" } },
                "total_rating_score": { "sum": { "field": "rating_total" } },
                "review_score_avg": {
                    "scripted_metric": {
                        "init_script" : "_agg['rating_count'] = []; _agg['rating_score'] = [];",
                        "map_script" : "_agg.rating_count.add(doc['rating_count'].value); _agg.rating_score.add(doc['rating_total'].value);", 
                        "combine_script" : "total_rating_count = 0; for (c in _agg.rating_count) { total_rating_count += c }; total_rating_score = 0; for (s in _agg.rating_score) { total_rating_score += s }; return [total_rating_score, total_rating_count];",
                        "reduce_script" : "total_score = 0; total_count = 0; for (a in _aggs) { def(score, count) = a; total_score += score; total_count += count; }; return (total_score + 0.0) / total_count;"
                    }
                }
            }
        }
    }

However, I got this error:
AggregationExecutionException[Invalid terms aggregation order path [review_score_avg.value]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end.

It seems the ScriptedMetricAggregator extends MetricsAggregator, but not NumericMetricAggregator in the current version, thus can not be used as an order path.

@colings86
Copy link
Contributor

This is indeed a limitation of the current implementation of script_metric aggregation. The issue is tracked here #8486. Closing in favour of that existing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants