ElasticSearch汇总查询

时间:2018-09-20 12:51:25

标签: elasticsearch

我想获得一个新字段,该字段告诉我group_by_gender聚合内任何地方的max_yes_percentage和min_yes_percentage(较小的值要减去另一个值)之间的区别。

我尝试使用bucket_script进行减法,但是由于无法将org.elasticsearch.search.aggregations.bucket.filter.InternalFilter强制转换为org.elasticsearch.search.aggregations.InternalMultiBucketAggregation,因此无法正常工作。那是有道理的,基本上我没有其他线索可以做到这一点。

我的查询是:

                POST /vote_votes/_search
                {
                  "query": {
                        "nested" : {
                            "path" : "vote",
                            "score_mode" : "none",
                            "query": {
                                "bool": {
                                    "filter": {
                                        "term": {
                                          "vote.id": 14
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "size": 0,
                    "aggs": {
                        "group_by_gender": {
                            "filter": {
                                "range": {
                                    "id": {
                                        "gte": 0
                                    }
                                }
                            },
                            "aggs": {
                                "gender": {
                                    "terms": {
                                        "field": "gender"
                                    },
                                    "aggs": {
                                        "rated_yes": {
                                            "filter": {
                                                "term": {
                                                    "rate": "yes"
                                                }
                                            }
                                        },
                                        "rated_no": {
                                            "filter": {
                                                "term": {
                                                    "rate": "no"
                                                }
                                            }
                                        },
                                        "yes_percentage": {
                                            "bucket_script": {
                                                "buckets_path": {
                                                  "ratedYes": "rated_yes._count",
                                                  "ratedNo": "rated_no._count"
                                                },
                                                "script": """

                                                return (params.ratedYes/(params.ratedYes+params.ratedNo)) * 100;

                                                """
                                            }
                                        }
                                    } 
                                },
                                "max_yes_percentage": {
                                    "max_bucket": {
                                        "buckets_path": "gender>yes_percentage",
                                        "gap_policy": "skip"
                                    }
                                },
                                "min_yes_percentage": {
                                    "min_bucket": {
                                      "buckets_path": "gender>yes_percentage",
                                      "gap_policy": "skip"
                                    }
                                }
                            }
                        }
                    }
                }

响应结果:

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
      },
      "hits": {
        "total": 503,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "group_by_gender": {
          "doc_count": 503,
          "gender": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": "female",
                "doc_count": 259,
                "rated_yes": {
                  "doc_count": 135
                },
                "rated_no": {
                  "doc_count": 124
                },
                "yes_percentage": {
                  "value": 52.123552123552116
                }
              },
              {
                "key": "male",
                "doc_count": 244,
                "rated_yes": {
                  "doc_count": 121
                },
                "rated_no": {
                  "doc_count": 123
                },
                "yes_percentage": {
                  "value": 49.59016393442623
                }
              }
            ]
          },
          "max_yes_percentage": {
            "value": 52.123552123552116,
            "keys": [
              "female"
            ]
          },
          "min_yes_percentage": {
            "value": 49.59016393442623,
            "keys": [
              "male"
            ]
          }
        }
      }
    }

0 个答案:

没有答案