elasticsearch,elasticsearch-jest,Java,elasticsearch,Elasticsearch Jest" /> elasticsearch,elasticsearch-jest,Java,elasticsearch,Elasticsearch Jest" />

Java 如何在jest elasticsearch中使用maxAggregation

Java 如何在jest elasticsearch中使用maxAggregation,java,elasticsearch,elasticsearch-jest,Java,elasticsearch,Elasticsearch Jest,我想从能力倾向对象的子字段中获取最大id { "mappings": { "aptitude": { "dynamic": "strict", "properties": { "id": { "type": "long" }, "es": { "type": "text" },

我想从能力倾向对象的子字段中获取最大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"
            +"}";
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
我在jest中研究聚合构建器,但通过查询进行聚合要容易得多

返回结果如下所示:

String query = "{\n"
            +"    \"query\" : {\n"
            +"        \"match\" : {\"id\":" + aptitudeId + "}\n"
            +"    },\n"
            +"    \"aggs\" : {\n"
            +"        \"max1\" : {\n"
            +"            \"max\" : {\n"
            +"                \"field\" : \"behaviors.id\"\n"
            +"            }\n"
            +"        }\n"
            +"    }\n"
            +"}";
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