-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Scriptable Metrics Aggregation #7075
Scriptable Metrics Aggregation #7075
Conversation
Looking forward to reading the docs :) |
@clintongormley yep. Long way from finished yet as need to write tests and read the docs. Put the PR up as @brwe wanted to have a look at it and so others could have a play around with it but its not ready for review yet |
+1! So cool, can't wait to give this a try. Thanks! |
Following gist can be followed as an example of using the scriptable metrics aggregation. |
@@ -156,4 +157,8 @@ public static TopHitsBuilder topHits(String name) { | |||
public static GeoBoundsBuilder geoBounds(String name) { | |||
return new GeoBoundsBuilder(name); | |||
} | |||
|
|||
public static ScriptBuilder script(String name) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the name should reflect the fact that this is a metric aggregation to leave some namespace if we have a scripted bucket agg some day?
I left some comments but my major concern is about the client: do we need to expose it to the scripts? I'm worried that it could allow to do crazy things? |
+1 on not exposing it... we shouldn't encourage doing crazy things with scripts |
One note: expression scripts can only be used for "search", they do not allow "executable". It looks like only the map phase uses search, and the rest executable? |
Yeah you are right. Actually since the expression language doesn't support assignment (i think, correct me if I am wrong) I don't think you would be able to use it even in the map script since the map script has to update some state in the params. Also, currently it is assumed that the same language is used for all the scripts in a single aggregations. I think the use cases for this aggregation are almost always going to need more complex scripts than the Lucene expression language can provide, so I see them mostly using groovy and maybe other languages through plugins |
@@ -18,6 +18,8 @@ | |||
*/ | |||
package org.elasticsearch.search.aggregations; | |||
|
|||
import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricParser; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put this import with the other ones?
Left some comments but it looks great in general! |
"aggs": { | ||
"profit": { | ||
"scripted_metric": { | ||
"init_script" : "aggregation['transactions'] = []", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use a shorter variable than aggregation
in the script. How about _agg
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
@jpountz I made the code changes you suggested. I also updated the docs with a worked example and left some replies to your comments. Let me know what you think. |
It looks good in general, docs are great. In my opinion, the only things that need to be done before merging it would be:
|
LGTM |
A metrics aggregation which runs specified scripts at the init, collect, combine, and reduce phases Closes #5923
Great News! I am trying to use a scripted metric aggregation inside a date_histogram aggregation but unfortunately it seems to take docs outside the date-bucket into consideration, but this is not what I expected.. |
{ |
@diegoasth I have tried to reproduce the issue your are seeing in the gist below [1], in both 1.4 and in the master branch, without success. Could you take a look at the gist and try it on your system to see if it reproduces your issue. If not, could you create a similar gist to illustrate the steps to reproduce the issue you are seeing? |
Hi @colings86 , thanks or your attention. in deed, our gist does not reproduce the issue, so I created another one that shows it. My goal is to use a scripted_metric inside a date_histogram (let me know if it is not possible). I expect that the scripts (init_script, map_script, combine_script and reduce_script) consider only the documents in that specific date-bucket. For the sake of comparison, I used the native SUM aggregation inside the date_histogram and it worked fine, returning the expected results per bucket. |
@diegoasth this is indeed a bug. Thanks for raising it and for providing the steps to reproduce it. I'll look into getting it fixed |
@diegoasth I have raised the following issue for this bug: |
thank you very much. The ability to wite my custom aggregations (inside ES) is a very awaited feature. |
A metrics aggregation which runs specified scripts at the init, collect, combine, and reduce phases
Closes #5923