如何在bucket_script中使用父级的同级存储桶路径?

时间:2018-11-21 06:10:30

标签: elasticsearch elasticsearch-dsl

我想在存储桶脚本(像这样的DSL)中使用父级的同级存储桶路径:

{
  "size": 0, 
  "aggs": {
    "car_type": {
      "terms": {
        "field": "screenName",
        "size": 10
      },
      "aggs": {
        "active_num": {
          "terms": {
            "field": "activeNum",
            "size": 10
          },
          "aggs": {
            "active_count": {
              "value_count": {
                "field": "activeNum"
              }
            },
            "result" : {
              "bucket_script": {
                "buckets_path": {
                  "count1" : "car_type>all_count",
                  "count2" : "active_count"
            },
                "script": "params.count2/params.count1"
              }
            }
          }
        },
        "all_count": {
          "value_count": {
            "field": "activeNum"
          }
        }
      }
    }
  }
}

我想在all_count中使用result,但是es会引发异常:

  

未找到路径[car_type> all_count]的聚合

然后,我更改使用bucket_script的位置,如下所示:

{
  "size": 0, 
  "aggs": {
    "car_type": {
      "terms": {
        "field": "screenName",
        "size": 10
      },
      "aggs": {
        "active_num": {
          "terms": {
            "field": "activeNum",
            "size": 10
          },
          "aggs": {
            "active_count": {
              "value_count": {
                "field": "activeNum"
              }
            }
          }
        },
        "all_count": {
          "value_count": {
            "field": "activeNum"
          }
        },
        "result" : {
          "bucket_script": {
             "buckets_path": {
                "count1" : "all_count",
                "count2" : "active_num>active_count"
              },
              "script": "params.count2/params.count1"
          }
        }
      }
    }
  }
}

但是我又得到一个例外:

  

buckets_path必须引用数字值或单值数字度量聚合,已获取:java.lang.Object []

我已经找到了官方网站页面,但是却一无所获。 如何使用此bucket_path?

1 个答案:

答案 0 :(得分:0)

使用以下方法可以解决一些问题:

{
  "size": 0, 
  "aggs": {
    "car_type": {
      "terms": {
        "field": "screenName",
        "size": 10
      },
      "aggs": {
        "active_num": {
          "filter": {
            "term": {
            "activeNum": "1"
            }
          },
          "aggs": {
            "active_count": {
              "value_count": {
                "field": "activeNum"
              }
            }
          }
        },
        "all_count": {
          "value_count": {
            "field": "activeNum"
          }
        },
        "result" : {
          "bucket_script": {
             "buckets_path": {
                "count1" : "all_count",
                "count2" : "active_num>active_count"
              },
              "script": "params.count2/params.count1"
          }
        }
      }
    }
  }
}

但是仍然存在一些问题,该方法只能获取一个值的结果,不能获取该字段的每个值。 那么,还有其他想法吗?