如何在 jest elasticsearch 中使用 maxAggregation

how to use maxAggregation in jest elasticsearch

我想从 aptitude 对象的子字段中获取最大 ID,

{
"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,据我所知,我应该使用 Jest 的 maxAggregation,但无法在 java 中找到如何做到这一点的合适示例,有人可以帮忙吗?

我找到了这样的方法:

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

我一直在研究 jest 的聚合构建器,但通过查询进行操作要容易得多。

return 看起来像这样:

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