如何在jest elasticsearch中使用maxAggregation

时间:2017-03-08 14:51:08

标签: java elasticsearch jest

我想让最大的id形式成为aptitude对象的子域,

{
"mappings": {
    "aptitude": {
        "dynamic": "strict",
        "properties": {
            "id": {
                "type": "long"
            },
            "es": {
                "type": "text"
            },
            "en": {
                "type": "text"
            },
            "behaviors": {
                "properties": {
                    "id": {
                        "type": "long"
                    },
                    "es": {
                        "type": "text"
                    },
                    "en": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

你可以看到aptitude有一系列的行为,而这些行为又有一个id,afaik我应该使用来自Jest的maxAggregation,但是找不到一个在java中如何做到这一点的体面的例子,有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我找到了这样的方式:

String query = "{\n"
            +"    \"query\" : {\n"
            +"        \"match\" : {\"id\":" + aptitudeId + "}\n"
            +"    },\n"
            +"    \"aggs\" : {\n"
            +"        \"max1\" : {\n"
            +"            \"max\" : {\n"
            +"                \"field\" : \"behaviors.id\"\n"
            +"            }\n"
            +"        }\n"
            +"    }\n"
            +"}";

我正在寻找来自jest的聚合构建器,但通过查询来实现它更容易。

回报如下:

Search search = new Search.Builder(query)
            .addIndex(aptitudeIndexName)
            .addType(aptitudeTypeName)
            .build();

    try {
        SearchResult result = client.execute(search);


        MaxAggregation max1 = result.getAggregations().getMaxAggregation("max1");
        Double max = max1.getMax();
        return max.longValue() + 1;//so it would add 1 to the current maximum id