Use the Tablestore SDK for Go to sort results by field value, relevance score, primary key, or geographic distance and paginate by using tokens.
Prerequisites
Install the Tablestore Go SDK and initialize a client.
Description
You can configure one or more sorters. Results are compared in sorter-list order. If no sorter is configured, the IndexSort configured when the search index was created is used. If IndexSort is not configured, primary key sorting is used by default. IndexSort supports only PrimaryKeySort and FieldSort. After the index is created, you can change IndexSort by dynamically modifying the schema. ScoreSort sorts by BM25 relevance score and takes effect only when explicitly configured.
When a non-primary-key sorter is specified, the server appends a primary key sorter by default so that rows with equal sort values have a deterministic order. DisableDefaultPkSorter defaults to false. Do not disable this behavior for token-based pagination. If sort values are duplicated and primary key sorting is disabled, rows may be skipped.
A search index that contains a Nested field does not support IndexSort. To paginate a query on such an index, explicitly configure sorting in the query request. Otherwise, the server does not return NextToken even if more data is available.
When you use ScoreSort, FuzzyKeyword fields do not participate in sorting, and Weight has no effect on FuzzyKeyword fields.
searchQuery := search.NewSearchQuery().
SetQuery(&search.MatchAllQuery{}).
SetLimit(10).
SetSort(&search.Sort{Sorters: []search.Sorter{
&search.FieldSort{
FieldName: "price",
Order: search.SortOrder_DESC.Enum(),
},
}})
for {
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_index",
SearchQuery: searchQuery,
})
if err != nil {
log.Fatal(err)
}
for _, row := range response.Rows {
fmt.Println(row)
}
if len(response.NextToken) == 0 {
break
}
searchQuery.SetToken(response.NextToken)
}
Parameters
Sort configuration
|
Name |
Type |
Description |
|
Sorters (required) |
[]search.Sorter |
The sorters. FieldSort, ScoreSort, PrimaryKeySort, and GeoDistanceSort are supported and take effect in list order. DocSort can be explicitly configured only in NestedQuery.InnerHits and cannot be used as a top-level Search sorter. |
|
DisableDefaultPkSorter (optional) |
*bool |
Specifies whether to disable the primary key sorter that the server appends. Default value: false. This parameter is supported by Tablestore SDK for Go 1.7.16 and later. |
Field-value sorting
|
Name |
Type |
Description |
|
FieldName (required) |
string |
The field used for sorting. Sorting and aggregation must be enabled for the field. |
|
Order (optional) |
*search.SortOrder |
The sort order. Valid values are search.SortOrder_ASC and search.SortOrder_DESC. Default value: ascending. |
|
Mode (optional) |
*search.SortMode |
The value selection method for a multi-valued field. Valid values are search.SortMode_Min, search.SortMode_Max, and search.SortMode_Avg. |
|
NestedFilter (optional) |
*search.NestedFilter |
The sort configuration for a Nested child field. Specify this parameter only when you sort by a Nested child field. |
|
MissingValue (optional) |
interface{} |
The sort value used when the fields specified by FieldName and MissingFields are all missing. search.FirstWhenMissing places missing values first regardless of sort order. search.LastWhenMissing or nil places them last. You can also specify a custom value of the field type for sorting. |
|
MissingField (optional) |
*string |
A fallback sort field. This parameter is deprecated. Use MissingFields. |
|
MissingFields (optional) |
[]string |
Fallback sort fields. If the field specified by FieldName is missing, the first existing field in list order is used for sorting. You can specify up to three fields. They must have the same type as the field specified by FieldName, and sorting and aggregation must be enabled for all fields. This parameter is supported by Tablestore SDK for Go 1.9.0 and later. |
Nested filter
|
Name |
Type |
Description |
|
Path (required) |
string |
The path of the Nested field. |
|
Filter (required) |
search.Query |
The query that filters the Nested child rows used for sorting. Use MatchAllQuery to include all child rows. |
Score, primary key, and document-order sorting
|
Name |
Type |
Description |
|
ScoreSort.Order (optional) |
*search.SortOrder |
The BM25 relevance-score order. search.NewScoreSort() sorts scores in descending order by default. |
|
PrimaryKeySort.Order (optional) |
*search.SortOrder |
The primary key order. search.NewPrimaryKeySort() sorts primary keys in ascending order by default. |
|
DocSort.SortOrder (optional) |
*search.SortOrder |
The document order within NestedQuery.InnerHits. Default value: ascending. DocSort is supported by Tablestore SDK for Go 1.7.12 and later. |
Geo-distance sorting
|
Name |
Type |
Description |
|
FieldName (required) |
string |
The name of the Geo-point field. |
|
Points (required) |
[]string |
The target points used to calculate distances. Use the |
|
Order (optional) |
*search.SortOrder |
The distance sort order. search.SortOrder_ASC sorts from nearest to farthest, and search.SortOrder_DESC sorts from farthest to nearest. |
|
Mode (optional) |
*search.SortMode |
The value selection method when multiple distances exist. Valid values are search.SortMode_Min, search.SortMode_Max, and search.SortMode_Avg. |
|
GeoDistanceType (optional) |
*search.GeoDistanceType |
The distance calculation method. search.GeoDistanceType_ARC calculates distance on a sphere and provides higher accuracy. search.GeoDistanceType_PLANE calculates distance on a plane and uses fewer resources. Default value: ARC. |
|
NestedFilter (optional) |
*search.NestedFilter |
The path and filter used to sort a Nested child field. |
Pagination configuration
|
Name |
Type |
Description |
|
Limit (optional) |
int32 |
The maximum number of rows per page. Default value: 10. The maximum is normally 100. If all returned columns are stored in the search index, the maximum can be increased to 1,000. For more information, see How do I increase the limit of a search index query to 1,000?. For offset-based pagination, Offset + Limit cannot exceed 100,000. |
|
Offset (optional) |
int32 |
The start position. Default value: 0. Use this parameter for shallow pagination within 100,000 rows. |
|
Token (optional) |
[]byte |
The NextToken value from the previous response. Use this parameter for consecutive deep pagination. SetToken clears Sort because the token contains the previous-page sort conditions. Do not specify Offset for token-based pagination. Base64-encode the binary token for persistence or transmission through text protocols or across processes. |
Token-based pagination moves forward only by default. To return to a previous page, cache and reuse an earlier token. Token-based pagination is not subject to the 100,000-row depth limit.
Response
|
Name |
Type |
Description |
|
Rows |
[]*tablestore.Row |
The rows returned by the current query. The number does not exceed Limit. |
|
SearchHits |
[]*tablestore.SearchHit |
The search hits. Read this field when you use relevance scores, highlighting, or Nested InnerHits. |
|
TotalCount |
int64 |
The total number of matched rows. The value depends on SetGetTotalCount and is not the number of rows on the current page. |
|
NextToken |
[]byte |
The token for the next page. An empty value indicates that no more data exists or that the current query does not have a deterministic sort order. |
|
IsAllSuccess |
bool |
Indicates whether all index partitions were queried. If the value is false, partial results are returned. |