2

我进行了弹性搜索查询,获取每个 oid 名称并将其计数除以 1000。(可以更改数字(1000))

    "aggregations" : {
        "agg_oid" : {
          "terms" : {
            "field" : "oid",
            "order" : {
              "_count" : "desc"
            }
          },
          "aggregations" : {
              "agg_values" : {
                  "bucket_script": {
                        "buckets_path": {
                          "count": "_count"
                        },
                        "script": "count / 1000"
                    }
              }
          }
        }
      }

它运行良好,并尝试使用 java api(目前使用 jest)来实现。

AggregationBuilders
    .terms( "agg_oid")
    .field( "oid")
    .subAggregation(
        AggregationBuilders
             // bucket script and path for count
    );

如何在java中实现'bucket_script'?如果没有,有没有办法通过java api中的计算来查询聚合的计数?

4

1 回答 1

5

这样的事情应该做:

Map<String, String> bucketsPathsMap = new HashMap<>();
bucketsPathsMap.put("count", "_count");
Script script = new Script("count / 1000");

BucketScriptPipelineAggregationBuilder bs = PipelineAggregatorBuilders
    .bucketScript("agg_values", bucketsPathsMap, script);

TermsAggregationBuilder oid = AggregationBuilders.terms("agg_oid")
    .field("oid")
    .order(InternalOrder.COUNT_DESC).
    subAggregation(bs);
于 2016-07-27T03:46:19.440 回答