2017-03-08 79 views
0

我想要得到的maximun ID形成資質對象的一個​​分支,如何使用maxAggregation開玩笑elasticsearch

{ 
"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" 
        } 
       } 
      } 
     } 
    } 
} 

正如你所看到的資質有行爲誰又將有一個id的數組, afaik我應該使用Jest的maxAggregation,但無法找到一個體面的例子,如何在java中做到這一點,有人可以幫忙嗎?

回答

0

,我發現這樣的方式:

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