Apply condition on value (count) greater than some value in aggregation

I am looking a way where I can apply condition on value return in result. Here is my query.

  {
"size": 0,
"aggs": {
    "by_brand": {
        "terms": {
            "field": "brand_name.keyword",
            "size": 10000,
            "order": {
                "_key": "asc"
            },
            "min_doc_count": 50
        },
        "aggs": {
            "product": {
                "cardinality": {
                    "field": "product_id"
                }
            }
        }
    }
},
"query": {
    "bool": {
        "filter": {
            "script": {
                "script": {
                    "source": "doc['image_src.keyword'].size()!=0",
                    "lang": "painless"
                }
            }
        }
    }
}
} 

and my result is as follow

{
    "took" : 150,
    "timed_out" : false,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "skipped" : 0,
        "failed" : 0
    },
    "hits" : {
        "total" : {
        "value" : 10000,
        "relation" : "gte"
        },
        "max_score" : null,
        "hits" : [ ]
    },
    "aggregations" : {
        "by_brand" : {
        "doc_count_error_upper_bound" : 0,
        "sum_other_doc_count" : 0,
        "buckets" : [
            {
            "key" : "4SI3NNA",
            "doc_count" : 432,
            "product" : {
                "value" : 119
            }
            },
            {
            "key" : "525 America",
            "doc_count" : 706,
            "product" : {
                "value" : 83
            }
            },
            {
            "key" : "7 For All Mankind",
            "doc_count" : 3558,
            "product" : {
                "value" : 555
            }
            },
            {
            "key" : "AG",
            "doc_count" : 3657,
            "product" : {
                "value" : 464
            }
            },
            {
            "key" : "AGOLDE",
            "doc_count" : 501,
            "product" : {
                "value" : 85
            }
            },
            {
            "key" : "AS by DF",
            "doc_count" : 997,
            "product" : {
                "value" : 324
            }
            }
        ]
        }
    }
    }

I like to filter out those result which have product->value less then 50.

    {
        "key" : "AS by DF",
        "doc_count" : 997,
        "product" : {
            "value" : 324
        }
     }

Thanks in advance
Regards

A filter using a range aggregation should work I think.

"NAME":{  
 "filter":{  
  "range":{  
   "feild_name":{  
    "gt":50
   }
  }
 }
}

Value I needed is cardinality value and base on this value I like to filter our product. Range is base on filed in document.

First I aggregate my document base on unique brand name with "term" and then calculate number of products by using "cardinality". Now I like to filter out products which have less then 50 products.

ok I found a solution let me share it here. I add "bucket_selector" in my query to fulfill my requirements here is my updated query.

    {
        "size": 0,
        "aggs": {
            "by_brand": {
                "terms": {
                    "field": "brand_name.keyword",
                    "size": 10000,
                    "order": {
                        "_key": "asc"
                    }
                },
                "aggs": {
                    "product": {
                        "cardinality": {
                            "field": "product_id"
                        }
                    },
                    "brand_bucket_filter": {
                        "bucket_selector": {
                            "buckets_path": {
                                "totalProducts": "product"
                            },
                            "script": "params.totalProducts > 50"
                        }
                    }
                }
            }
        },
        "query": {
            "bool": {
                "filter": {
                    "script": {
                        "script": {
                            "source": "doc['image_src.keyword'].size()!=0",
                            "lang": "painless"
                        }
                    }
                }
            }
        }
    }

it will filter out products which have count less then 50.

Regards

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.