是否可以在Java中使用bucket_script?

I read the Aggregations official documentation, it says

Elasticsearch提供了一个完整的Java API来处理聚合。请参阅汇总指南。

但是,我不能仅将以下查询转换为Java代码。

{
  “size”: 0,
  “aggs”: {
    “mallBucket”: {
      “terms”: {
        “field”: “mallId”,
        “size”: 20,
        “min_doc_count”: 3,
        “shard_size”: 10000
      },
      “aggs”: {
        “totalOrderCount”: {
          “value_count”: {
            “field”: “orderSn”
          }
        },
        “filteredCoupon”: {
          “filter”: {
            “terms”: {
              “tags”: [
                “hello”,
                “cool”
              ]
            }
          },
          “aggs”: {
            “couponCount”: {
              “value_count”: {
                “field”: “orderSn”
              }
            }
          }
        },
        “countRatio”: {
          “bucket_script”: {
            “buckets_path”: {
              “orderCount”: “totalOrderCount”,
              “couponCount”: “filteredCoupon>couponCount”
            },
            “script”: “params.couponCount/params.orderCount”
          }
        },
        “ratio_bucket_sort”: {
          “bucket_sort”: {
            “sort”: [
              {
                “countRatio”: {
                  “order”: “desc”
                }
              }
            ],
            “size”: 20
          }
        }
      }
    }
  }
}

搜索模板

GET _render/template
{
  "source": {
      "size": 0,
      "aggs": {
        "mallBucket": {
          "terms": {
            "field": "mallId",
            "size": "{{topCount}}",
            "min_doc_count": "{{minOrderCount}}",
            "shard_size": 10000
          },
          "aggs": {
            "totalOrderCount": {
              "value_count": {
                "field": "orderSn"
              }
            },
            "filteredCoupon": {
              "filter": {
               "terms": "{{couponTags}}"
              },
              "aggs": {
                "couponCount": {
                  "value_count": {
                    "field": "orderSn"
                  }
                }
              }
            },
            "countRatio": {
              "bucket_script": {
                "buckets_path": {
                  "orderCount": "totalOrderCount",
                  "couponCount": "filteredCoupon>couponCount"
                },
                "script": "params.couponCount/params.orderCount"
              }
            },
            "ratio_bucket_sort": {
              "bucket_sort": {
                "sort": [
                  {
                    "countRatio": {
                      "order": "desc"
                    }
                  }
                ],
                "size": "{{topCount}}"
              }
            }
          }
        }
      }
  },
  "params": {
    "topCount": 5,
    "minOrderCount": 3,
    "couponTags": ["hello", "cool"]
  }
}

terms cannot be used here as expected, only term can be used instead, have no idea

评论