![](/img/trans.png)
[英]How to get unique documents in elastic search based on a field, and 'group by' the result based on other fields
[英]How to group documents by field in elastic search using spring
我有用戶文檔,其中包含各種字段,包括addressId。 我想要具有不同addressId的用戶列表。 如果兩個用戶具有相同的addressId,則返回任何一個用戶並跳過另一個。
我正在嘗試通過addressId對用戶進行分組,但無法正常工作。
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).
addAggregation(AggregationBuilders.terms("addressId").field("addressId").size(1)).
withPageable(new PageRequest(page, size)).build();
您需要添加大小為1的熱門匹配 subaggregation
。每個addressId字段將返回一個文檔(在您的情況下為用戶)。
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(boolQueryBuilder)
.addAggregation(AggregationBuilders
.terms("addressId")
.field("addressId")
.size(Integer.MAX_VALUE)
.subaggregation(AggregationBuilders.topHits("justOneUserPerAddressId)
.setSize(1)
)
.withPageable(new PageRequest(page, size)).build();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.