All Products
Search
Document Center

Tablestore:Collapse query results

Last Updated:Aug 11, 2026

Use the Tablestore SDK for Go to collapse query results by a field and return one row for each distinct field value.

Prerequisites

Install the Tablestore Go SDK and initialize a client.

Description

Collapse can be combined with any query type. It groups query results by a field value and returns the first sorted row from each group. TotalCount remains the number of matched rows before collapse, and the total number of groups after collapse cannot be obtained. Sorting and aggregation must be enabled for the collapse field. Long, Double, and Keyword fields are supported, but array fields are not supported.

Important
  • A collapse query supports only Offset- and Limit-based pagination and does not support token-based pagination. Offset + Limit cannot exceed 100,000, so up to 100,000 groups can be returned.

  • If aggregation and collapse are used together, aggregation is calculated over the result set before collapse.

searchQuery := search.NewSearchQuery().
    SetQuery(&search.MatchAllQuery{}).
    SetCollapse(&search.Collapse{FieldName: "category"}).
    SetGetTotalCount(true)

response, err := client.Search(&tablestore.SearchRequest{
    TableName:   "example_table",
    IndexName:   "example_index",
    SearchQuery: searchQuery,
})
if err != nil {
    log.Fatal(err)
}

fmt.Println(response.TotalCount)
fmt.Println(response.Rows)

Parameters

Name

Type

Description

FieldName (required)

string

The field used to collapse results.

Response

Name

Type

Description

TotalCount

int64

The total number of matched rows. The value depends on SetGetTotalCount.

Rows

[]*tablestore.Row

The rows returned by the current query. The number does not exceed the value specified by SetLimit.

SearchHits

[]*tablestore.SearchHit

The search hits. Read this field when you use highlighting, nested inner hits, or relevance scores.

IsAllSuccess

bool

Indicates whether all index partitions were queried. If the value is false, partial results are returned and TotalCount may be less than the actual number of matched rows.

AggregationResults

search.AggregationResults

The aggregation results.

GroupByResults

search.GroupByResults

The grouping results.