When you use a search index to query data, you can use a predefined sorting method or specify a sorting method. This way, the rows that meet the query conditions are returned based on the order that you predefined or specified. If a large number of rows are included in the response, you can locate the required data by configuring the Limit and Offset parameters or by using tokens.
Scenarios
Category | Method | Feature | Scenario |
Sorting | Predefine a sorting method when you create a search index | By default, data in a search index is sorted based on the presorting settings that are specified by the IndexSort parameter. The presorting settings that are specified by the IndexSort parameter determine the default order in which the rows that meet the query conditions are returned. | |
Specify a sorting method when you query data | Sorting based on the BM25-based keyword relevance score (ScoreSort) | You can use ScoreSort to sort query results based on the BM25-based keyword relevance score. ScoreSort is suitable for scenarios such as full-text search. | |
You can use PrimaryKeySort to sort query results based on the primary key value. PrimaryKeySort is suitable for scenarios in which you want to sort data based on the unique identifiers of the data. | |||
Sorting based on the values of one or more columns (FieldSort) | You can use FieldSort to sort query results based on the values of one or more columns. FieldSort is suitable for scenarios in which you want to sort data based on properties such as sales volume or page views. In most cases, FieldSort is used in industries such as e-commerce and social networking and media asset. | ||
You can use GeoDistanceSort to sort query results by geographical location. GeoDistanceSort is suitable for scenarios in which you want to sort data based on the distance from a specific location. In most cases, GeoDistanceSort is used in industries such as mapping and logistics. For example, you can sort restaurants around a location based on the distance from the location. | |||
Paging | Specify a paging method when you query data | If the number of rows in the response is less than 50,000, you can use this method to jump to a page. | |
By default, you can only page backward when you use a token. However, you can cache and use the previous token to page forward because a token is valid during the query. |
Index presorting
By default, data in a search index is sorted based on the presorting settings that are specified by the IndexSort parameter. When you use a search index to query data, the presorting settings that are specified by the IndexSort parameter determine the default order in which the matched data is returned.
When you create a search index, you can specify presorting settings by configuring the IndexSort parameter. If you do not specify presorting settings, data in the search index is sorted by primary key value.
You can specify PrimaryKeySort or FieldSort as the presorting method for a search index. PrimaryKeySort sorts data by primary key value and FieldSort sorts data by field value.
Search indexes that contain Nested fields do not support index presorting.
If you want to modify the settings of the IndexSort parameter for an existing search index, you can dynamically modify the schema of the search index. For more information, see Dynamically modify the schema of a search index.
Specify a sorting method when you query data
Sorting can be enabled only for fields for which the EnableSortAndAgg parameter is set to true.
You can specify a sorting method for each query. Search index-based queries support the following sorting methods. You can also specify multiple sorting methods based on different priorities.
ScoreSort
You can use ScoreSort to sort query results based on the BM25-based keyword relevance score. ScoreSort is suitable for scenarios such as full-text search.
Before you sort the matched data by keyword relevance score, you must configure the parameters for ScoreSort. Otherwise, the matched data is sorted based on the presorting settings that are specified by the IndexSort parameter.
searchQuery := search.NewSearchQuery()
searchQuery.SetSort(&search.Sort{
[]search.Sorter{
&search.ScoreSort{
Order: search.SortOrder_DESC.Enum(), // Sort the query result in descending order of scores.
},
},
})
PrimaryKeySort
You can use PrimaryKeySort to sort query results based on the primary key value.
searchQuery := search.NewSearchQuery()
searchQuery.SetSort(&search.Sort{
[]search.Sorter{
&search.PrimaryKeySort{
Order: search.SortOrder_ASC.Enum(),
},
},
})
FieldSort
Sort the query results based on the values of a single column
You can use FieldSort to sort query results based on the values of a specific column.
// Sort the query results based on the values of the Col_Long column in descending order. searchQuery.SetSort(&search.Sort{ []search.Sorter{ &search.FieldSort{ FieldName: "Col_Long", Order: search.SortOrder_DESC.Enum(), }, }, })
Sort the query results based on the values of multiple columns
You can also sort query results based on the values of two columns in specific orders to determine the order in which the matched data is returned.
searchQuery.SetSort(&search.Sort{ []search.Sorter{ &search.FieldSort{ FieldName: "col1", Order: search.SortOrder_ASC.Enum(), }, &search.FieldSort{ FieldName: "col2", Order: search.SortOrder_DESC.Enum(), }, }, })
GeoDistanceSort
You can use GeoDistanceSort to sort query results by geographical location.
searchQuery.SetSort(&search.Sort{
[]search.Sorter{
&search.GeoDistanceSort{
FieldName: "location", // Specify the name of the GEOPOINT field.
Points: []string{"40,-70"}, // Specify the coordinate pair of the central point.
},
},
})
Specify a paging method
You can configure the Limit and Offset parameters or use tokens to page the rows in the response.
Paging based on the Limit and Offset parameters
If the total number of rows in the response is less than 50,000, you can configure the Limit and Offset parameters to page the rows. The sum of the values of the Limit and Offset parameter cannot exceed 50,000. The maximum value of the Limit parameter is 100.
For information about how to increase the maximum value of the Limit parameter, see How do I increase the value of the limit parameter to 1000 when I call the Search operation of the search index feature to query data?.
If you do not specify values for the Limit and Offset parameters, the default values are used. The default value of the Limit parameter is 10. The default value of the Offset parameter is 0.
searchQuery := search.NewSearchQuery()
searchQuery.SetLimit(10)
searchQuery.SetOffset(10)
Paging based on tokens
We recommend that you use a token for deep paging because this method has no limits on the paging depth.
If Tablestore cannot read all data that meets the query conditions, Tablestore returns NextToken. You can use NextToken to continue reading the subsequent data.
By default, you can only page backward when you use a token. However, you can cache and use the previous token to page forward because a token is valid during the query.
If you want to persist NextToken or transfer NextToken to the frontend page, you can use Base64 to encode NextToken into a string. Tokens are not strings. If you use string(NextToken)
to encode a token into a string, information about the token is lost.
When you use a token, the sorting method is the same as the method that is used in the previous request. Tablestore sorts data based on the IndexSort parameter by default or based on the method that you specified. You cannot specify the sorting method when you use a token. You cannot configure the Offset parameter when you use a token. Data is returned page by page in sequence. This results in a slow query.
Search indexes that contain Nested fields do not support IndexSort. If you require paging and you use a search index that contains Nested fields to query data, you must specify the sorting method in the query conditions to return data in the specified order. Otherwise, Tablestore does not return NextToken when only part of the data that meets the query conditions is returned.
/**
* Use a token to read data by page.
* If NextToken is included in SearchResponse, you can use this token to initiate the next query.
* All data that meets the query conditions is returned if the value of NextToken is nil.
*/
func QueryRowsWithToken(client *tablestore.TableStoreClient, tableName string, indexName string) {
querys := []search.Query{
&search.MatchAllQuery{},
&search.TermQuery{
FieldName: "Col_Keyword",
Term: "tablestore",
},
}
for _, query := range querys {
fmt.Printf("Test query: %#v\n", query)
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName(tableName)
searchRequest.SetIndexName(indexName)
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(query)
searchQuery.SetLimit(10)
searchQuery.SetGetTotalCount(true)
searchRequest.SetSearchQuery(searchQuery)
searchResponse, err := client.Search(searchRequest)
if err != nil {
fmt.Printf("%#v", err)
return
}
rows := searchResponse.Rows
requestCount := 1
for searchResponse.NextToken != nil {
{
// If you want to persist NextToken or transfer NextToken to the frontend page, you can use Base64 to encode NextToken into a string.
// Tokens are not strings. If you use string(NextToken) to encode a token into a string, information about the token is lost.
tokenAsString := base64.StdEncoding.EncodeToString(searchResponse.NextToken)
// Decode the string into bytes.
tokenAsByte, err := base64.StdEncoding.DecodeString(tokenAsString)
if err != nil {
fmt.Printf("len:%d, %#v",len(tokenAsByte), err)
return
}
}
searchQuery.SetToken(searchResponse.NextToken)
searchResponse, err = client.Search(searchRequest)
if err != nil {
fmt.Printf("%#v", err)
return
}
requestCount++
for _, r := range searchResponse.Rows {
rows = append(rows, r)
}
}
fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess)
fmt.Println("TotalCount: ", searchResponse.TotalCount)
fmt.Println("RowsSize: ", len(rows))
fmt.Println("RequestCount: ", requestCount)
}
}