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

You can use the collapse function to deduplicate data based on the collapsed columns in most scenarios. However, this function is only supported for columns whose values are of the INTEGER and Double types. Only the first 10,000 sorted results are returned.


  • If you use the collapse function, you must implement paging by specifying the offset and size instead of next_token.
  • If you aggregate and collapse a result set at the same time, the result set will be 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 function. After the result set is collapsed, you cannot query the total number of the returned groups .


fieldName: specifies the name of the column based on which the result set is collapsed. Only columns whose values are of the INTEGER and DOUBLE 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 the product name.

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

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