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.


  • 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.


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.


private static void UseCollapse(SyncClient client){
        SearchQuery searchQuery = new SearchQuery();
        MatchQuery matchQuery = new MatchQuery();

        Collapse collapse = new Collapse("product_name"); // Collapse the result set by product name.

        SearchRequest searchRequest = new SearchRequest("order","order_index",searchQuery);

        SearchResponse response =;
        System.out.println(response.getRows().size()); // Display the names of the products.