Use Tablestore SDK for Python to control the order of search index results and paginate results by using offset or next_token.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Description
Search indexes support index presorting and query-time sorting. When you create a search index, use index_sort to specify the default order. If index_sort is not specified, rows are sorted by primary key. Index presorting supports only PrimaryKeySort and FieldSort and is not supported for an index that contains a Nested field. After creation, you can dynamically update the schema to change index presorting. At query time, use SearchQuery.sort to specify ScoreSort, PrimaryKeySort, FieldSort, or GeoDistanceSort, or combine multiple sorters in list order. Except for primary keys, sort fields must have sorting and aggregation enabled when the index is created.
|
Pagination method |
Description |
|
limit and offset |
Use when results do not exceed 100,000 rows and you need to jump to a specified position. |
|
next_token |
Use for deep pagination or sequentially reading all results. Pagination depth is not subject to the 100,000-row limit, but pages can be read only sequentially. |
The following example returns the first 10 rows sorted by score in descending order and then by primary key in ascending order.
sort = Sort([
FieldSort("score", SortOrder.DESC),
PrimaryKeySort(SortOrder.ASC),
])
response = client.search(
"example_table",
"example_index",
SearchQuery(MatchAllQuery(), sort=sort, limit=10),
ColumnsToGet(return_type=ColumnReturnType.ALL),
)
print(response.rows)
Parameters
Search request
The search method contains the following parameters.
|
Name |
Type |
Description |
|
table_name (required) |
|
The name of the data table. |
|
index_name (required) |
|
The name of the search index. |
|
search_query (required) |
|
The query condition and common query configurations. |
|
columns_to_get (optional) |
|
The return column configuration. If this parameter is not specified, only primary key columns are returned. |
|
routing_keys (optional) |
|
The primary key values of custom routing fields. This parameter is not required if custom routing is not configured. |
|
timeout_s (optional) |
|
The request timeout in seconds. If this parameter is not specified, the client-level timeout is used. |
Query configuration
search_query is of the SearchQuery type and contains the following sorting and pagination parameters.
|
Name |
Type |
Description |
|
query (required) |
|
The query condition. |
|
sort (optional) |
|
The query-time sort configuration. If omitted, index presorting is used. Do not specify it when using |
|
offset (optional) |
|
The offset. Default value: |
|
limit (optional) |
|
The maximum number of rows to return. Default value: |
|
next_token (optional) |
|
The pagination token. Omit it in the first request and use |
|
get_total_count (optional) |
|
Specifies whether to return the total number of matching rows. Default value: |
Sort configuration
search_query.sort is of the Sort type and contains the following parameter.
|
Name |
Type |
Description |
|
sorters (required) |
|
The list of sorters. The list order determines multi-level sort priority. Supported sorter types are |
Relevance score sort
If search_query.sort.sorters[] is of the ScoreSort type, rows are sorted by relevance score. ScoreSort contains the following parameter.
|
Name |
Type |
Description |
|
sort_order (optional) |
|
The sort order. Default value: |
Primary key sort
If search_query.sort.sorters[] is of the PrimaryKeySort type, rows are sorted by primary key. PrimaryKeySort contains the following parameter.
|
Name |
Type |
Description |
|
sort_order (optional) |
|
The sort order. Default value: |
Field sort
If search_query.sort.sorters[] is of the FieldSort type, rows are sorted by field value. FieldSort contains the following parameters.
|
Name |
Type |
Description |
|
field_name (required) |
|
The sort field name. Sorting and aggregation must be enabled for the field. |
|
sort_order (optional) |
|
The sort order. Default value: |
|
sort_mode (optional) |
|
The value selection mode for a multi-valued field: |
|
nested_filter (optional) |
|
The Nested child-field sort configuration, including the Nested path and a query that selects child rows used for sorting. |
Nested filter
search_query.sort.sorters[].nested_filter is of the NestedFilter type, can be used in FieldSort or GeoDistanceSort, and contains the following parameters.
|
Name |
Type |
Description |
|
path (required) |
|
The path of the Nested field. |
|
query_filter (required) |
|
The query condition that selects Nested child rows used for sorting. Set this parameter to |
Geo-distance sort
If search_query.sort.sorters[] is of the GeoDistanceSort type, rows are sorted by the distance between a geographical point and the target points. GeoDistanceSort contains the following parameters.
|
Name |
Type |
Description |
|
field_name (required) |
|
The name of the GeoPoint sort field. |
|
points (required) |
|
The target points in |
|
sort_order (optional) |
|
|
|
sort_mode (optional) |
|
The value selection mode when multiple distances exist: |
|
geo_distance_type (optional) |
|
The distance calculation method. |
|
nested_filter (optional) |
|
The Nested child-field sort configuration. |
Return columns
columns_to_get is of the ColumnsToGet type and contains the following parameters.
|
Name |
Type |
Description |
|
column_names (optional) |
|
The names of attribute columns to return. Specify this parameter only when |
|
return_type (optional) |
|
The return column mode. |
Response
The search method returns SearchResponse. The following table describes the core fields.
|
Field |
Type |
Description |
|
rows |
|
The rows returned by the query. The number does not exceed |
|
next_token |
|
The token for the next page. An empty value indicates that no more data is available. |
|
total_count |
|
The number of matching rows. The value depends on |
|
is_all_succeed |
|
Indicates whether all index partitions were queried. If the value is |
|
agg_results |
|
The metric aggregation results. This field is empty if |
|
group_by_results |
|
The grouping results. This field is empty if |
|
search_hits |
|
The search hits, including extended information such as rows, relevance scores, and highlights. |
An empty next_token can also indicate that the query has no deterministic sort order. total_count is the total number of matching rows, not the current-page row count.
Tuple-compatible response
Starting from Tablestore SDK for Python 5.2.0, search APIs return response objects instead of tuples. Version 5.1.0 and earlier return tuples directly. In version 5.2.1 and later, you can call SearchResponse.v1_response() to obtain a tuple compatible with earlier versions. For new code, access SearchResponse attributes directly to avoid unpacking errors if response fields are extended.
(
rows,
next_token,
total_count,
is_all_succeed,
agg_results,
group_by_results,
search_hits,
) = response.v1_response()
Examples
Sort by geo distance
The following example returns results from nearest to farthest based on the spherical distance between location and 30.25,120.16.
sort = Sort([
GeoDistanceSort(
"location",
["30.25,120.16"],
sort_order=SortOrder.ASC,
sort_mode=SortMode.MIN,
geo_distance_type=GeoDistanceType.ARC,
)
])
response = client.search(
"example_table",
"example_index",
SearchQuery(MatchAllQuery(), sort=sort, limit=10),
)
print(response.rows)
Paginate by using next_token
Specify the sort order in the first request. In subsequent requests, pass only next_token from the previous response and the same query condition until the token is empty.
query = MatchAllQuery()
response = client.search(
"example_table",
"example_index",
SearchQuery(query, sort=Sort([PrimaryKeySort()]), limit=100),
)
all_rows = list(response.rows)
while response.next_token:
response = client.search(
"example_table",
"example_index",
SearchQuery(query, next_token=response.next_token, limit=100),
)
all_rows.extend(response.rows)
print(len(all_rows))
When paginating by using next_token, do not specify offset, and pages cannot be skipped directly. To move backward, cache the token used for each page and query again with the token for the target page. A search index that contains a Nested field has no index presorting. Explicitly specify sort in the first request, or the server does not return next_token.