Use Tablestore SDK for Java to create a search index on a Lastpoint index and query the latest data of each time series by data field conditions.
Prerequisites
Install the Tablestore SDK for Java and initialize a client. Lastpoint indexes require version 5.17.1 or later. We recommend that you use the latest version.
Description
Direct reads from a Lastpoint index locate data by primary key range. To filter the latest data by data fields with Boolean queries, full-text search, geo queries, or aggregations, create a search index on the Lastpoint index and call search. For more information about query capabilities, see Search indexes.
public CreateSearchIndexResponse createSearchIndex(CreateSearchIndexRequest request) throws TableStoreException, ClientException
public SearchResponse search(SearchRequest request) throws TableStoreException, ClientException
Create a search index
Only fields included in the search index schema can be queried. The _tags field of a Lastpoint index is a string array and must be mapped as a Keyword array. Map other data fields to their corresponding types.
The following example creates example_search_index on example_lastpoint_index and indexes the tags, speed, and status fields.
List<FieldSchema> fieldSchemas = Arrays.asList(
new FieldSchema("_tags", FieldType.KEYWORD)
.setIndex(true).setIsArray(true),
new FieldSchema("speed", FieldType.DOUBLE)
.setIndex(true).setEnableSortAndAgg(true),
new FieldSchema("status", FieldType.KEYWORD)
.setIndex(true).setEnableSortAndAgg(true));
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(fieldSchemas);
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName("example_lastpoint_index");
request.setIndexName("example_search_index");
request.setIndexSchema(indexSchema);
client.createSearchIndex(request);
Query the latest data
Creating a search index is asynchronous. Run queries after the index status changes to RUNNING and the data synchronization phase changes to INCR.
The following example uses a range query to retrieve Lastpoint rows in which speed is greater than 20.0. The query returns the total number of matching rows and all columns.
RangeQuery rangeQuery = new RangeQuery();
rangeQuery.setFieldName("speed");
rangeQuery.greaterThan(ColumnValue.fromDouble(20.0));
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(rangeQuery);
searchQuery.setGetTotalCount(true);
SearchRequest request = new SearchRequest(
"example_lastpoint_index",
"example_search_index",
searchQuery);
SearchRequest.ColumnsToGet columnsToGet =
new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true);
request.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(request);
System.out.println(response.getTotalCount());
response.getRows().forEach(System.out::println);
Parameters
Create search index request
CreateSearchIndexRequest contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
|
The Lastpoint index name. A Lastpoint index is used as a data table when you create a search index. |
|
indexName (required) |
|
The search index name. |
|
indexSchema (required) |
|
The search index schema, including field schemas, index settings, and presorting settings. |
Index fields
Each element in indexSchema.fieldSchemas[] is of the FieldSchema type. The following parameters configure the fields in the example.
|
Name |
Type |
Description |
|
fieldName (required) |
|
The field name in the Lastpoint index. |
|
fieldType (required) |
|
The search index field type. The type must match the value type in the Lastpoint index. |
|
index (optional) |
|
Specifies whether to index the field. Default value: |
|
isArray (optional) |
|
Specifies whether the field is an array. Default value: |
|
enableSortAndAgg (optional) |
|
Specifies whether sorting and aggregations are enabled. Default value: |
Search request
SearchRequest contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
|
The Lastpoint index name. |
|
indexName (required) |
|
The name of the search index on the Lastpoint index. |
|
searchQuery (required) |
|
The query configuration. The example sets |
|
columnsToGet (optional) |
|
The columns to return. If this parameter is not specified, only primary key columns are returned. The example uses |
Response
SearchResponse contains the following operation-specific fields.
|
Field |
Type |
Description |
|
|
|
Call |
|
|
|
Call |
|
|
|
Call |
|
|
|
Call |