弹性搜索中的脚本java api

时间:2016-07-27 01:02:55

标签: elasticsearch jest

我创建了弹性搜索查询,获取每个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中的计算来查询聚合的计数?

1 个答案:

答案 0 :(得分:2)

这样的事情应该做:

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);