Hi,
I'm looking into the new pipeline aggregations for computing things like ratios per term bucket (e.g. click-through rate). I can compute the CTR per item using something like this:
q = {
    "query": {
        "match_all" : {}
    },
    "aggs" : {
        "ctr" : {
            "terms" : {
                "field" : "item_id",
                "size"  : 10,
            },
            "aggs": {
                "numer": {
                  "filter": {
                    "term": {
                      "event": "click"
                    }
                  }
                },
                "denom": {
                  "filter": {
                    "term": {
                      "event": "impression"
                    }
                  }
                },
                "ratio": {
                    "bucket_script": {
                        "buckets_path": {
                          "numer_total": "numer>_count",
                          "denom_total": "denom>_count"
                        },
                        "script": "numer_total / denom_total"
                    }
                }
            }
        }
    }
}
Now, say I wanted to rank results by CTR, I try this:
q = {
    "query": {
        "match_all" : {}
    },
    "aggs" : {
        "ctr" : {
            "terms" : {
                "field" : "item_id",
                "size"  : 10,
                "order": {
                    "ratio" : "desc" 
                }
            },
            "aggs": {
                "numer": {
                  "filter": {
                    "term": {
                      "event": "click"
                    }
                  }
                },
                "denom": {
                  "filter": {
                    "term": {
                      "event": "impression"
                    }
                  }
                },
                "ratio": {
                    "bucket_script": {
                        "buckets_path": {
                          "numer_total": "numer>_count",
                          "denom_total": "denom>_count"
                        },
                        "script": "numer_total / denom_total"
                    }
                }
            }
        }
    }
}
But it doesn't seem that the ratio aggregation can be used for sorting, even though it is a single-valued metric?
I get this error:
RemoteTransportException[[Madame Menace][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: AggregationExecutionException[Invalid term-aggregator order path [ratio]. Unknown aggregation [ratio]];
Caused by: AggregationExecutionException[Invalid term-aggregator order path [ratio]. Unknown aggregation [ratio]]
	at org.elasticsearch.search.aggregations.support.AggregationPath.validate(AggregationPath.java:293)
	at org.elasticsearch.search.aggregations.bucket.terms.InternalOrder.validate(InternalOrder.java:145)
	at org.elasticsearch.search.aggregations.bucket.terms.InternalOrder.validate(InternalOrder.java:138)
	at org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator.<init>(TermsAggregator.java:143)
...
Is this not supported? Or is there another way to achieve the same result? I previously tried to use the result of a Scripted Metric Agg to sort, but that is not supported either.