Usage of setBucketsPaths


(Patrick Kik) #1

I'm writing a pipeline aggregation in Java. This is part of my aggregation definition via the REST interface:

"active_users": {
  "bucket_script": {
    "buckets_path": {
      "added": "users_added",
      "removed": "users_removed"
    },
    "script": "added - removed"
  }
}

In Java I can write:

.subAggregation(
  PipelineAggregatorBuilders.bucketScript("active_users")
    .setBucketsPathsMap(new HashMap() {{
      put("added", "users_added");
      put("removed", "users_removed");
    }})
  .script(new Script("added - removed"))
)

But can the setBucketsPaths method be used? Something like:

.subAggregation(
  PipelineAggregatorBuilders.bucketScript("active_users")
    .setBucketsPaths(
      "added", "users_added",
      "removed", "users_removed"
    )
  .script(new Script("added - removed"))
)

(Colin Goodheart-Smithe) #2

No, you couldn't write t like that. The setBucketPaths() method on the Bucket_script agg is for anonymous bucketsPaths, each bucketpath in the array or params list is assigned to a variable in the script named _value where is the index of the variable in the array. So you could do this instead:

.subAggregation(
  PipelineAggregatorBuilders.bucketScript("active_users")
    .setBucketsPaths("users_added", "users_removed")
  .script(new Script("_value0 - _value1"))
)

Hope that helps


(Patrick Kik) #3

Exactly what I wanted! Thanks!


(system) #4