When you use a search index to query data, you can predefine a sorting method when you create the search index or specify a sorting method when you query data. 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 | Specify a sorting method when you create a search index | Index presorting | 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. | |
Sorting based on the value of the primary key (PrimaryKeySort) | You can use PrimaryKeySort to sort query results based on the value of the primary key. 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 a specific column (FieldSort) | You can use FieldSort to sort query results based on the values of a specific column. 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. | ||
Sorting by geographical location (GeoDistanceSort) | 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 | Paging based on the Limit and Offset parameters | If the number of rows in the response is smaller than 50,000, you can use this method to jump to a page. |
Paging based on Tokens | If you use this feature, data is returned page by page and you can only page backward. If you want to page forward, you can cache and use a previous Token because Tokens are 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.
- You can specify PrimaryKeySort or FieldSort as the presorting method for a search index. PrimaryKeySort sorts data by primary key and FieldSort sorts data by field value.
- Search indexes that contain fields of the Nested type do not support index presorting.
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
var searchQuery = new SearchQuery();
searchQuery.Sort = new Sort(new List<ISorter>() { new ScoreSort() });
PrimaryKeySort
You can use PrimaryKeySort to sort query results based on the value of the primary key.
// Sort the query results based on the values of the primary key in ascending order.
var searchQuery = new SearchQuery();
searchQuery.Sort = new Sort(new List<ISorter>() { new PrimaryKeySort() });
// Sort the query results based on the values of the primary key in descending order.
var searchQuery = new SearchQuery();
searchQuery.Sort = new Sort(new List<ISorter>() { new PrimaryKeySort(SortOrder.DESC) });
FieldSort
You can use FieldSort to sort query results based on the values of a specific column.
var searchQuery = new SearchQuery();
var fieldSort = new FieldSort("col", SortOrder.ASC);
searchQuery.Sort = new Sort(new List<ISorter>() { fieldSort });
You can also sort query result based on the values of two columns in specific orders to determine the order in which the matched data is returned.
var searchQuery = new SearchQuery();
var col1Sort = new FieldSort("col", SortOrder.ASC);
var col2Sort = new FieldSort("co2", SortOrder.ASC);
searchQuery.Sort = new Sort(new List<ISorter>() { col1Sort, col2Sort });
GeoDistanceSort
You can use GeoDistanceSort to sort query results by geographical location.
var searchQuery = new SearchQuery();
var geoDistanceSort = new GeoDistanceSort("geoCol", new List<string>(){"0","0"});
searchQuery.Sort = new Sort(new List<ISorter>() { geoDistanceSort });
Specify a paging method
You can configure the Limit and Offset parameters or use Tokens to page the rows in the response.
Configure the Limit and Offset parameters
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.
var searchQuery = new SearchQuery();
searchQuery.Query = new MatchAllQuery();
searchQuery.Limit = 100;
searchQuery.Offset = 100;
Use a Token
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. If you want to page forward, you can cache and use a previous Token because Tokens are valid during the query.
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 field 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, which results in a slow query.
/// <summary>
/// In this example, a token is used to page the rows in the response and Tablestore reads all data and returns the data in a list.
/// </summary>
/// <param name="otsClient"></param>
public static SearchResponse ReadMoreRowsWithToken(OTSClient otsClient)
{
var searchQuery = new SearchQuery();
searchQuery.Query = new MatchAllQuery();
var request = new SearchRequest(TableName, IndexName, searchQuery);
var response = otsClient.Search(request);
var rows = response.Rows;
while (response.NextToken != null) // If NextToken is null in the response, all data is read.
{
request.SearchQuery.Token = response.NextToken;
response = otsClient.Search(request);
rows.AddRange(response.Rows);
}
return response;
}