Scripting aggregation order


(Ivan Brusic) #1

Would it be possible to script the sort order of aggregations? Let me
explain with a contrived example:

{
"aggs": {
"agg1": {
"terms": {
"field": "somefield",
"order": {
"avg1": "desc"
},
"size" : 100
},
"aggs": {
"avg1": {
"avg": {
"field": "someotherfield"
}
}
}
}
}

The above example will return an aggregation where the buckets are sorted
based on the average of some other field of the bucketized documents.
Pretty easy. What I would actually want is to do some more complex math
such as the average of the bucketized documents times the number of
documents in the bucket.

The metric aggregation script, as far as I can tell, does not have access
to information of the bucket it is in.

{ "script" : "doc['someotherfield'].value * ?bucket.count?" }

The aggregation order also does not offer script based sorting.

    "order": {
      "_script": {
        "script": "bucket.avg * bucket.count",
        "type": "number",
        "order": "desc"
      }
    }

Very pseudo code example above. My hope is that perhaps I missed the fact
that Elasticsearch does support one of these two scenarios. If not, are
there any workarounds to fine-tune sort order? Secondary sorting would also
help.

Cheers,

Ivan

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Adrien Grand) #2

Hi Ivan,

This is not supported but there is an open issue that aims at making this
kind of things possible

On Tue, Jul 29, 2014 at 8:50 PM, Ivan Brusic ivan@brusic.com wrote:

Would it be possible to script the sort order of aggregations? Let me
explain with a contrived example:

{
"aggs": {
"agg1": {
"terms": {
"field": "somefield",
"order": {
"avg1": "desc"
},
"size" : 100
},
"aggs": {
"avg1": {
"avg": {
"field": "someotherfield"
}
}
}
}
}

The above example will return an aggregation where the buckets are sorted
based on the average of some other field of the bucketized documents.
Pretty easy. What I would actually want is to do some more complex math
such as the average of the bucketized documents times the number of
documents in the bucket.

The metric aggregation script, as far as I can tell, does not have access
to information of the bucket it is in.

{ "script" : "doc['someotherfield'].value * ?bucket.count?" }

The aggregation order also does not offer script based sorting.

    "order": {
      "_script": {
        "script": "bucket.avg * bucket.count",
        "type": "number",
        "order": "desc"
      }
    }

Very pseudo code example above. My hope is that perhaps I missed the fact
that Elasticsearch does support one of these two scenarios. If not, are
there any workarounds to fine-tune sort order? Secondary sorting would also
help.

Cheers,

Ivan

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
Adrien Grand

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6tMy7AYLv4XBRW_kDii1N2kvPibtawUMWQVbZ5s7bkBg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Ivan Brusic) #3

Thanks Adrien. What about secondary sorting? The issue talks more
about collation,
so I am not sure if it is more an umbrella issue for all aggregation
sorting concepts. Should I open an issue, comment on the existing one, or
just leave you be? :slight_smile:

Cheers,

Ivan

On Tue, Jul 29, 2014 at 12:50 PM, Adrien Grand <
adrien.grand@elasticsearch.com> wrote:

Hi Ivan,

This is not supported but there is an open issue that aims at making this
kind of things possible
https://github.com/elasticsearch/elasticsearch/issues/6917

On Tue, Jul 29, 2014 at 8:50 PM, Ivan Brusic ivan@brusic.com wrote:

Would it be possible to script the sort order of aggregations? Let me
explain with a contrived example:

{
"aggs": {
"agg1": {
"terms": {
"field": "somefield",
"order": {
"avg1": "desc"
},
"size" : 100
},
"aggs": {
"avg1": {
"avg": {
"field": "someotherfield"
}
}
}
}
}

The above example will return an aggregation where the buckets are sorted
based on the average of some other field of the bucketized documents.
Pretty easy. What I would actually want is to do some more complex math
such as the average of the bucketized documents times the number of
documents in the bucket.

The metric aggregation script, as far as I can tell, does not have access
to information of the bucket it is in.

{ "script" : "doc['someotherfield'].value * ?bucket.count?" }

The aggregation order also does not offer script based sorting.

    "order": {
      "_script": {
        "script": "bucket.avg * bucket.count",
        "type": "number",
        "order": "desc"
      }
    }

Very pseudo code example above. My hope is that perhaps I missed the fact
that Elasticsearch does support one of these two scenarios. If not, are
there any workarounds to fine-tune sort order? Secondary sorting would also
help.

Cheers,

Ivan

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
Adrien Grand

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6tMy7AYLv4XBRW_kDii1N2kvPibtawUMWQVbZ5s7bkBg%40mail.gmail.com
https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6tMy7AYLv4XBRW_kDii1N2kvPibtawUMWQVbZ5s7bkBg%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAW2OeHOruNhgqc-72zbKUCUqsyE3Ff1zOfCpHTMmu0%3Dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Jörg Prante) #4

Unicode collations know the concept of secondary sorting and much more, see
http://userguide.icu-project.org/collation/concepts

Jörg

On Tue, Jul 29, 2014 at 11:43 PM, Ivan Brusic ivan@brusic.com wrote:

Thanks Adrien. What about secondary sorting? The issue talks more about collation,
so I am not sure if it is more an umbrella issue for all aggregation
sorting concepts. Should I open an issue, comment on the existing one, or
just leave you be? :slight_smile:

Cheers,

Ivan

On Tue, Jul 29, 2014 at 12:50 PM, Adrien Grand <
adrien.grand@elasticsearch.com> wrote:

Hi Ivan,

This is not supported but there is an open issue that aims at making this
kind of things possible
https://github.com/elasticsearch/elasticsearch/issues/6917

On Tue, Jul 29, 2014 at 8:50 PM, Ivan Brusic ivan@brusic.com wrote:

Would it be possible to script the sort order of aggregations? Let me
explain with a contrived example:

{
"aggs": {
"agg1": {
"terms": {
"field": "somefield",
"order": {
"avg1": "desc"
},
"size" : 100
},
"aggs": {
"avg1": {
"avg": {
"field": "someotherfield"
}
}
}
}
}

The above example will return an aggregation where the buckets are
sorted based on the average of some other field of the bucketized
documents. Pretty easy. What I would actually want is to do some more
complex math such as the average of the bucketized documents times the
number of documents in the bucket.

The metric aggregation script, as far as I can tell, does not have
access to information of the bucket it is in.

{ "script" : "doc['someotherfield'].value * ?bucket.count?" }

The aggregation order also does not offer script based sorting.

    "order": {
      "_script": {
        "script": "bucket.avg * bucket.count",
        "type": "number",
        "order": "desc"
      }
    }

Very pseudo code example above. My hope is that perhaps I missed the
fact that Elasticsearch does support one of these two scenarios. If not,
are there any workarounds to fine-tune sort order? Secondary sorting would
also help.

Cheers,

Ivan

--
You received this message because you are subscribed to the Google
Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQCLxU%3DFTddW%3DesWLWHz0G-SO_oN7Zb4OE4o%3DVdwyRw%2BkQ%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
Adrien Grand

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6tMy7AYLv4XBRW_kDii1N2kvPibtawUMWQVbZ5s7bkBg%40mail.gmail.com
https://groups.google.com/d/msgid/elasticsearch/CAL6Z4j6tMy7AYLv4XBRW_kDii1N2kvPibtawUMWQVbZ5s7bkBg%40mail.gmail.com?utm_medium=email&utm_source=footer
.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups
"elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAW2OeHOruNhgqc-72zbKUCUqsyE3Ff1zOfCpHTMmu0%3Dw%40mail.gmail.com
https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQAW2OeHOruNhgqc-72zbKUCUqsyE3Ff1zOfCpHTMmu0%3Dw%40mail.gmail.com?utm_medium=email&utm_source=footer
.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAKdsXoF6TcnL-65g1dCaoaTyALpb%2BX7rgaV_GWCGX-5bFecLDw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(Gchen) #5

Hi, is there any update on support this requirement? We also need to deal with this kind of sorting based on arithmetical expression result of multiple metrics. Thanks!


(system) #6