The collapse feature deduplicates a query result set by a specified column so that each unique value appears only once in the returned results. Use collapse to keep query results diverse when a single category dominates the result set.
The collapse feature can achieve deduplication (Distinct) in most scenarios, equivalent to deduplicating by the collapse column. However, it applies only to columns of the integer, floating-point number, and Keyword types, does not support columns of the array type, and can return only the first 100,000 sorted results.
Usage limits
Before you use the collapse feature, note the following constraints:
-
Supported column types — The collapse feature applies only to columns of the integer, floating-point number, and Keyword types. Columns of the array type are not supported.
-
Pagination — The collapse feature supports pagination only by using offset and limit. Token-based pagination is not supported.
-
Aggregation — When you use statistical aggregation together with the collapse feature, the aggregation applies only to the result set before the collapse operation.
-
Group count limit — After the collapse operation, the total number of groups returned depends on the maximum value of offset plus limit. A maximum of 100,000 groups can be returned.
-
Total count — The total number of rows in the response is the number of matched rows before the collapse operation. You cannot obtain the total number of groups after the collapse operation.
Parameters
The collapse feature is provided by the Search operation and is implemented by using the collapse parameter. The following table describes the parameters used in a collapse query.
| Parameter | Description |
| query | Any query type. |
| collapse | The collapse settings, which include the fieldName setting. fieldName: the name of the column by which the result set is collapsed. This parameter applies only to columns of the integer, floating-point number, and Keyword types. Columns of the array type are not supported. |
| offset | The position from which the current query starts. |
| limit | The maximum number of rows to return for the current query. If you want to obtain only the number of rows and do not need the data, set limit to 0. No rows are returned. |
| getTotalCount | Specifies whether to return the total number of matched rows. The default value is false, which specifies that the total number of matched rows is not returned. Returning the total number of matched rows affects query performance. |
| tableName | The name of the data table. |
| indexName | The name of the search index. |
| columnsToGet | Specifies whether to return all columns. This parameter includes the returnAll and columns settings. The default value of returnAll is false, which specifies that not all columns are returned. In this case, you can use columns to specify the columns to return. If you do not use columns to specify the columns to return, only the primary key columns are returned. If you set returnAll to true, all columns are returned. |
Usage
You can use the command line interface or an SDK to collapse results when you query data.
Use an Alibaba Cloud account or a RAM user with the required permissions for Table Store operations. To grant permissions to a RAM user, see Grant permissions to a RAM user by using a RAM policy.
If you use an SDK or a command-line tool, create an AccessKey for your Alibaba Cloud account or RAM user if you do not have one.
You have created a data table.
A Search Index has been created for the data table.
If you use an SDK, initialize the Tablestore Client.
If you use the command-line tool, download and start the tool, then configure the connection to your instance and select the target table. For more information, see Download the command-line tool, Start the tool and configure connection information, and Data table operations.
Run the search command in the command line interface to query data by using a search index, and configure the Collapse parameter in the query conditions to use the collapse feature. For more information, see Search indexes.
-
Run the
searchcommand to query data in the table by using a search index and return all indexed columns.
search -n search_index --return_all_indexed
-
Enter the query conditions as prompted. The following code provides an example:
{
"Offset": -1,
"Limit": 10,
"Collapse": {
"FieldName": "product_name"
},
"Sort": null,
"GetTotalCount": true,
"Token": null,
"Query": {
"Name": "MatchQuery",
"Query": {
"FieldName": "user_id",
"Text": "00002",
"MinimumShouldMatch": 1
}
}
}
The following table describes the key fields in the example:
| Field | Description |
| Offset | The position from which the query starts. A value of -1 indicates that no offset is applied. |
| Limit | The maximum number of rows to return. |
| Collapse.FieldName | The name of the column by which results are deduplicated. In this example, product_name is used as the collapse column. Replace this value with the name of your target column. |
| Query | The query conditions. This example uses a MatchQuery on the user_id column. Replace the field name and query text with your own values. |
After the query returns results, verify that the collapse feature works as expected by checking that the values in the collapse column are unique across the returned rows.
Use an SDK
You can use the Java SDK, Go SDK, Python SDK, Node.js SDK, .NET SDK, or PHP SDK to collapse results when you query data. The following example uses the Java SDK to describe how to use the collapse feature.
The following example queries all rows, collapses the results by the category field, and sorts rows by the price field in descending order. The row with the highest price in each category is returned.
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(new MatchAllQuery());
searchQuery.setLimit(10);
searchQuery.setCollapse(new Collapse("category"));
searchQuery.setSort(new Sort(
Arrays.asList(new FieldSort("price", SortOrder.DESC))));
SearchRequest.ColumnsToGet columnsToGet =
new SearchRequest.ColumnsToGet();
columnsToGet.setColumns(Arrays.asList("category", "price"));
SearchRequest request =
new SearchRequest("example_table", "example_index", searchQuery);
request.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(request);
System.out.println(response.getRows());
Data query
In VCU mode (formerly Reserved mode), querying data by using a search index consumes the compute resources of VCUs. In CU mode (formerly Pay-As-You-Go mode), querying data by using a search index consumes read throughput. For more information, see Search index billing.
Querying data by using a search index consumes read throughput. For more information, see Billable items of search indexes.
Using the collapse feature when you query data does not affect the existing billing rules.
FAQ
References
Search Index supports various query types for multi-dimensional data queries, including term query, terms query, match all query, match query, phrase match query, range query, prefix query, suffix query, wildcard query, token-based wildcard query, boolean query, geo query, nested query, vector search, and exists query.
When you query data, you can sort and paginate the result set or perform collapsing (deduplication).
For data analysis, such as finding the maximum or minimum value, calculating a sum, or counting rows, you can use the statistical aggregation or SQL query features.
To quickly export data regardless of the result set order, you can use the Parallel Scan feature.