You can use the collapse feature to collapse the result set based on a specified column when the results of a query contain large amounts of data of a specific type. The data of the corresponding type is displayed only once in the query results to ensure the diversity of types in the results.

You can use the collapse feature to obtain distinct values based on collapsed columns in most scenarios. However, this feature is supported only for columns whose values are of the LONG, DOUBLE, and KEYWORD data types. Only the first 10,000 sorted results are returned.

Precautions

  • If you use the collapse feature, you must implement paging by specifying offset and size instead of next_token.
  • If you aggregate and collapse a result set at the same time, the result set is aggregated before it is collapsed.
  • If you collapse the query results, the total number of groups of the returned results is determined by the sum of the offset and size values. A maximum of 10,000 groups can be returned.
  • The total number of rows returned in the results indicates the number of returned rows before you use the collapse feature. After the result set is collapsed, you cannot query the total number of the returned groups.

Parameters

Parameter Description
fieldName Specifies the name of the column based on which the result set is collapsed. Only columns whose values are of the LONG, DOUBLE, and KEYWORD data types are supported.

Examples

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"); // Collapse the result set by product name.

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

        SearchResponse response = client.search(searchRequest);
        System.out.println(response.getTotalCount());    
        System.out.println(response.getRows().size()); // Display the names of the products.
    }