Skip to content
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

Provision to add condition to remove bucket in aggregation #9124

Closed
Vineeth-Mohan opened this issue Jan 1, 2015 · 1 comment
Closed

Provision to add condition to remove bucket in aggregation #9124

Vineeth-Mohan opened this issue Jan 1, 2015 · 1 comment

Comments

@Vineeth-Mohan
Copy link

In bucket aggregation , its a common use case to invalidate or rather remove some of the buckets based on some conditions.
Sample use cases are

  1. For term aggregation bucket nested with stats aggregation , remove all buckets with same stats min and stats max as same.
  2. Terms aggregation nested over stats - Remove all buckets having average less than 10

So basically as we do sorting based on path conditions , we should also be able to drop buckets.
Eg:

{
    "aggs" : {
        "countries" : {
            "terms" : {
                "field" : "address.country",
                "drop" : { "condition" : "females.height_stats.avg > 10 "  }
            },
            "aggs" : {
                "females" : {
                    "filter" : { "term" : { "gender" : { "female" }}},
                    "aggs" : {
                        "height_stats" : { "stats" : { "field" : "height" }}
                    }
                }
            }
        }
    }
}

The above aggregation will drop all country whose average female height is more than 10.

@clintongormley
Copy link

Hi @Vineeth-Mohan

We're planning on supporting bucket filtering with #8110, eg :

{
  "aggs": {
    "countries": {
      "terms": {
        "field": "address.country"
      },
      "having": {
        "script": "_bucket.female.height_stats.avg > min_height",
        "params": {
          "min_height": 10
        }
      },
      "aggs": {
        "females": {
          "filter": {
            "term": {
              "gender": "female"
            }
          },
          "aggs": {
            "height_stats": {
              "stats": {
                "field": "height"
              }
            }
          }
        }
      }
    }
  }
}

Or, for an example that refers to two separate aggregations, eg only include countries where the average female height is greater than the average height overall:

{
  "aggs": {
    "female_avg_height_above_overall_agv": {
      "trim": {
        "buckets": "address.country",
        "script": "_bucket.female.avg_height.value > avg_height.value"
      }
    },
    "avg_height": {
      "avg": {
        "field": "height"
      }
    },
    "countries": {
      "terms": {
        "field": "address.country"
      },
      "aggs": {
        "female": {
          "terms": {
            "term": {
              "gender": "female"
            }
          },
          "aggs": {
            "avg_height": {
              "avg": {
                "field": "height"
              }
            }
          }
        }
      }
    }
  }
}

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