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 specific 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 returned results to ensure the diversity of the result types.

Prerequisites

Usage notes

  • If you use the collapse feature, you can perform paging only by specifying the Offset and Limit parameters instead of tokens.

  • 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 results to return is determined by the sum of the Offset and Limit values. 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 rows cannot be queried.

Parameters

Parameter

Description

TableName

The name of the data table.

IndexName

The name of the search index.

Query

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

Collapse

The collapse parameter, including the FieldName parameter.

FieldName: 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 data 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 specific data, set the Limit parameter to 0.

Sample code

The following sample code provides an example on how to query the rows in which the value of user_id matches "00002" and then collapse the result set based on the value of the product_name column:

func QueryWithCollapse(client *tablestore.TableStoreClient, tableName string, indexName string) {
    searchRequest := &tablestore.SearchRequest{}
    searchRequest.SetTableName(tableName)       // Specify the name of the table. 
    searchRequest.SetIndexName(indexName)       // Specify the name of the search index. 

    searchQuery := search.NewSearchQuery()
    searchQuery.SetQuery(&search.MatchQuery{    // Specify the query conditions. 
        FieldName:          "user_id",
        Text:               "00002",
    })
    searchQuery.SetCollapse(&search.Collapse{
        FieldName: "product_name",              // Collapse the result set based on the value of the product_name column. 

    })
    searchQuery.SetOffset(0)
    searchQuery.SetLimit(100)
    searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{ReturnAll:true}) // Return all columns in the rows that meet the query conditions. 
    searchRequest.SetSearchQuery(searchQuery)

    searchResponse, err := client.Search(searchRequest)   // Perform the query. 
    if err != nil {
        fmt.Println("Failed to search with error:", err)
        return
    }
    for _, row := range searchResponse.Rows {           // Display the rows that are returned. 
        jsonBody, err := json.Marshal(row)
        if err != nil {
            panic(err)
        }
        fmt.Println("Row: ", string(jsonBody))
    }
}

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 Perform a parallel scan.