All Products
Search
Document Center

Tablestore:Collapse (distinct)

Last Updated:Dec 19, 2024

You can use the collapse (distinct) 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. Data of the specific type is displayed only once in the query results to ensure the diversity of the result types.

Prerequisites

Usage notes

  • If you use the collapse (distinct) feature, you can perform pagination only by specifying the offset and limit parameters.

  • 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 results of a query, the total number of results that are returned is determined by the sum of the values of the offset and limit parameters. A maximum of 100,000 results can be returned.

  • The total number of rows in the response indicates the number of rows that meet the query conditions before you use the collapse (distinct) feature. After the result set is collapsed, the total number of distinct values cannot be queried.

Parameters

Parameter

Description

query

The query type. You can set this parameter to any query type.

collapse

Collapses the result set based on the column that is specified by the field_name field.

field_name: the name of the column based on which the result set is collapsed. Only columns whose values are of the INTEGER, FLOATING-POINT, and KEYWORD types are supported.

offset

The position from which the current query starts.

limit

The maximum number of rows that you want the current query to return.

To query only the number of rows that meet the query conditions without querying specific data of the rows, set the limit parameter to 0.

get_total_count

Specifies whether to return the total number of rows that meet the query conditions. The default value of this parameter is false, which specifies that the total number of rows that meet the query conditions is not returned.

If you set this parameter to true, the query performance is compromised.

table_name

The name of the data table.

index_name

The name of the search index.

columns_to_get

Specifies whether to return all columns of each row that meets the query conditions. You can configure the return_type and column_names fields for this parameter.

  • If you set the return_type field to ColumnReturnType.SPECIFIED, use the column_names field to specify the columns that you want to return.

  • If you set the return_type field to ColumnReturnType.ALL, all columns are returned.

  • If you set the return_type field to ColumnReturnType.NONE, only the primary key columns are returned.

Example

The following sample code provides an example on how to query the rows in which the value of the price column matches 1000. In this example, the result set is collapsed based on the value of the product_name column.

# Construct a query condition to query all products whose value of the price field is 1000. 
query = TermQuery('price', 1000)

# Collapse the result set based on the "product_name" column. 
collapse = Collapse('product_name')
search_response = client.search('<TABLE_NAME>', '<SEARCH_INDEX_NAME>',
                                SearchQuery(query, limit=100, get_total_count = False, collapse_field = collapse),
                                columns_to_get = ColumnsToGet(return_type = ColumnReturnType.ALL_FROM_INDEX))
print('request_id : %s' % search_response.request_id)
print('is_all_succeed : %s' % search_response.is_all_succeed)
print('total_count : %s' % search_response.total_count)
print('rows : %s' % search_response.rows)

FAQ

References

  • When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, Boolean query, KNN vector query, nested query, and exists query. You can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements.

    You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.

    You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).

  • If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.

  • If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.