Search indexes accelerate the retrieval of Lastpoint indexes and provide multi-dimensional query and statistical analysis capabilities. This topic describes how to use Tablestore SDK for Java to retrieve data in a Lastpoint index by using a search index.
Usage notes
The Lastpoint index feature is supported by Tablestore SDK for Java V5.17.1 or later. To use the feature, use Tablestore SDK for Java V5.17.1 or later.
For information about how to download a new version of Tablestore SDK for Java, see Version history of Tablestore SDK for Java.
Prerequisites
A Lastpoint index is created for a time series table. For more information, see Create a Lastpoint index.
Procedure
1. Create a search index
The following sample code provides an example on how to create a search index for a Lastpoint index. For information about the sample scenario and data, see Appendix.
In the example, the value of the _tags field is a string array composed of tags. We recommend that you set the type of the mapped field in the search index to a Keyword array, which makes it easier to query the tags in the _tags field.
private static void createSearchIndex(SyncClient client) {
CreateSearchIndexRequest createSearchIndexRequest = new CreateSearchIndexRequest();
createSearchIndexRequest.setTableName("");
createSearchIndexRequest.setIndexName("");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("_#h", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("_m_name", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("_data_source", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("_tags", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true).setIsArray(true),
new FieldSchema("_time", FieldType.LONG).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("gps", FieldType.GEO_POINT).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("speed", FieldType.DOUBLE).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("status", FieldType.KEYWORD).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("total_mileage", FieldType.DOUBLE).setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("remaining_mileage", FieldType.DOUBLE).setIndex(true).setEnableSortAndAgg(true)
));
createSearchIndexRequest.setIndexSchema(indexSchema);
client.createSearchIndex(createSearchIndexRequest);
}2. Retrieve data by using a search index
The following sample code provides an example on how to use the range query feature of search indexes.
In this example, a search index is used to retrieve the rows in which the value of the speed field is greater than 20.0 in a Lastpoint index.
private static void rangeQuery(SyncClient client) {
SearchQuery searchQuery = new SearchQuery();
RangeQuery rangeQuery = new RangeQuery(); // Set the query type to RangeQuery.
rangeQuery.setFieldName("speed"); // Specify the field to match.
rangeQuery.greaterThan(ColumnValue.fromDouble(20.0)); // Specify the query condition for the field. Only rows in which the value of the field is greater than 20.0 meet the query condition.
searchQuery.setGetTotalCount(true);
searchQuery.setQuery(rangeQuery);
// Specify that the rows that meet the query condition is sorted by the speed column in descending order.
FieldSort fieldSort = new FieldSort("speed");
fieldSort.setOrder(SortOrder.DESC);
searchQuery.setSort(new Sort(Arrays.asList((Sort.Sorter)fieldSort)));
searchQuery.setGetTotalCount(true); // Specify that the total number of rows that meet the query condition is returned.
SearchRequest searchRequest = new SearchRequest("", "", searchQuery);
// You can configure the columnsToGet parameter to specify the columns that you want to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true); // Specify that all columns are returned.
searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
System.out.println("TotalCount: " + resp.getTotalCount()); // Print the total number of matched rows, not the number of returned rows.
System.out.println("Row: " + resp.getRows());
}FAQ
References
The core features provided by search indexes include query based primary key columns or non-primary key columns, Boolean query, geo query, full-text search, fuzzy query, prefix query, nested query, collapse (distinct), sorting, match all query, and aggregation. For more information, see Features.
Appendix
In the Internet of Vehicles (IoV) scenario, sensors report the time series data of vehicles to the cloud. Users can store, query, and analyze the time series data to meet business requirements, such as vehicle status report, vehicle positioning, traffic management, and screen mirroring of vehicle trajectory.
The following table shows the sample data in a time series table.
In this example, the _m_name, _data_source, and _tags field are time series identifiers, which specify the measurement name, data source, and tag information of the time series, respectively. The _time field specifies the time when data is reported. The gps, speed, status, total_mileage, and remaining_mileage fields are time series data in the time series, which specify the vehicle GPS coordinates, vehicle speed, vehicle status, total vehicle mileage, and remaining vehicle mileage, respectively.
_m_name | _data_source | _tags | _time | gps | speed | status | total_mileage | remaining_mileage |
Platform A | sensor1 | ["region=hangzhou","car_model=sedan","number_plate=ZheA D7512*","color=white"] | 1730422800000000 | 30.245853,120.178564 | 0 | Idle | 20000 | 450 |
Platform A | sensor1 | ["region=hangzhou","car_model=sedan","number_plate=ZheA D7512*","color=white"] | 1730423400000000 | 30.245853,120.178564 | 0 | Idle | 20000 | 450 |
Platform A | sensor2 | ["region=hangzhou","car_model=suv","number_plate=ZheC 72B2*","color=black"] | 1730779200000000 | 30.245278,120.150269 | 50 | Active | 15000 | 300 |
Platform A | sensor2 | ["region=hangzhou","car_model=suv","number_plate=ZheC 72B2*","color=black"] | 1730779800000000 | 30.245853,120.213654 | 80 | Active | 15050 | 250 |
Platform B | sensor3 | ["region=hangzhou","car_model=sedan","number_plate=ZheB 121*9","color=blue"] | 1730862000000000 | 30.246013,120.124470 | 60 | Active | 18200 | 300 |
Platform B | sensor3 | ["region=hangzhou","car_model=sedan","number_plate=ZheB 121*9","color=blue"] | 1730862600000000 | 30.246022,120.124460 | 0 | Idle | 18230 | 270 |
Tablestore automatically synchronizes data of the latest point in time in time series in the time series table to the Lastpoint index. The following table shows the sample data in the Lastpoint index.
_#h | _m_name | _data_source | _tags | _time | gps | speed | status | total_mileage | remaining_mileage |
4c#Platform A#07 | Platform A | sensor1 | ["region=hangzhou","car_model=sedan","number_plate=ZheA D7512*","color=white"] | 1730423400000000 | 30.245853,120.178564 | 0 | Idle | 20000 | 450 |
25#Platform A#ae | Platform A | sensor2 | ["region=hangzhou","car_model=suv","number_plate=ZheC 72B2*","color=black"] | 1730779800000000 | 30.245853,120.213654 | 80 | Active | 15050 | 250 |
b2#Platform B#4b | Platform B | sensor3 | ["region=hangzhou","car_model=sedan","number_plate=ZheB 121*9","color=blue"] | 1730862600000000 | 30.246022,120.124460 | 0 | Idle | 18230 | 270 |