使用collapse去重后,返回结果可以按照设定的字段折叠,示例如下。

private static void UseCollapse(SyncClient client){
        SearchQuery searchQuery = new SearchQuery();
        MatchQuery matchQuery = new MatchQuery();
        matchQuery.setFieldName("user_id");
        matchQuery.setText("00002");

        searchQuery.setQuery(matchQuery);
        Collapse collapse = new Collapse("product_name"); //根据产品名去重

        searchQuery.setGetTotalCount(true);
        searchQuery.setCollapse(collapse);
        SearchRequest searchRequest = new SearchRequest("order","order_index",searchQuery);

        SearchResponse response = client.search(searchRequest);
        System.out.println(response.getTotalCount());
        System.out.println(response.getRows().size());   //有几种产品会返回几个产品名
    }