This topic describes how to use Tablestore SDK for Java to perform a range query of search indexes. You can specify a range condition to query data that falls within the range.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created and data is written to the data table. For more information, see Create a data table and Write data.
A search index is created for the data table. For more information, see Create a search index.
Parameters
Parameter | Description |
query | The query type. Set this parameter to
|
sort | The sorting method. For more information, see Perform sorting and paging. |
getTotalCount | Specifies whether to return the total number of rows that meet the query condition. Default value: false. A value of false specifies that the total number of rows that meet the query condition is not returned. If you set this parameter to true, the query performance is compromised. |
tableName | The name of the data table. |
indexName | The name of the search index. |
columnsToGet | Specifies whether to return all columns of each row that meets the query condition. You can configure the returnAll and columns fields for this parameter. The default value of the returnAll field is false, which specifies that not all columns of each row that meets the query condition are returned. You can use the columns field to specify the columns that you want to return. If you do not use the columns field to specify the columns that you want to return, only the primary key columns are returned. If you set the returnAll field to true, all columns of each row that meets the query condition are returned. |
Examples
private static void rangeQuery(SyncClient client) {
SearchQuery searchQuery = new SearchQuery();
RangeQuery rangeQuery = new RangeQuery(); // Set the query type to RangeQuery.
/**
* Example 1: Query the rows in which the value of the Col_Long column is greater than 3.
*/
rangeQuery.setFieldName("Col_Long");
rangeQuery.greaterThan(ColumnValue.fromLong(3));
/**
* //Example 2: Query the rows in which the value of the Col_Date_String column is greater than or equal to 2020-01-01 10:55:12 and less than 2021-03-03 08:20:50.
* //The type of the Col_Date_String column is String in the data table and Date in the search index to which the data table is mapped. The values of the Col_Date_String column in the search index are in the yyyy-MM-dd HH:mm:ss format.
* rangeQuery.setFieldName("Col_Date_String");
* rangeQuery.greaterThanOrEqual(ColumnValue.fromString("2020-01-01 10:55:12"));
* rangeQuery.lessThan(ColumnValue.fromString("2021-03-03 08:20:50"));
*/
/**
* //Example 3: Query the rows in which the value of the Col_Date_Long column is greater than 1731036722.
* //The type of the Col_Date_Long column is Integer in the data table and Date in the search index to which the data table is mapped. The values of the Col_Date_Long column in the search index are timestamps in seconds.
* rangeQuery.setFieldName("Col_Date_Long");
* rangeQuery.greaterThan(ColumnValue.fromLong(1731036722));
*/
searchQuery.setQuery(rangeQuery);
searchQuery.setGetTotalCount(true); // Specify that the total number of rows that meet the query condition is returned.
SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
//Configure the columnsToGet parameter to specify that specific columns or all columns are returned. If you do not configure the columnsToGet parameter, only the primary key columns are returned.
//SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
// Specify that all columns are returned.
//columnsToGet.setReturnAll(true);
// Specify that specific columns are returned.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2"));
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount());// Display the total number of matched rows instead of the number of returned rows is returned.
System.out.println("Row: " + resp.getRows());
}
FAQ
References
When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, geo query, KNN vector query, Boolean query, nested query, and exists query. After you create a search index, you can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements.
You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Perform sorting and paging.
You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).
If you want to analyze data in a table, you can call the Search operation to use the aggregation feature or use the SQL query feature. For example, you can query the maximum and minimum values, the sum of the values, and the number of rows. For more information, see Aggregation and SQL query.
If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.