【问题标题】:how to use maxAggregation in jest elasticsearch如何在 jest elasticsearch 中使用 maxAggregation
【发布时间】:2017-07-29 04:59:43
【问题描述】:

我想从 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,afaik 我应该使用 Jest 的 maxAggregation,但是找不到一个像样的例子来说明如何在 java 中做到这一点,有人可以帮忙吗?

【问题讨论】:

    标签: java elasticsearch elasticsearch-jest


    【解决方案1】:

    我找到了这样的方法:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2015-05-06
      • 2015-10-16
      相关资源
      最近更新 更多