This topic describes how to quickly use Tablestore SDK for Java to perform operations on search indexes.
Prerequisites
A client is initialized. For more information, see Initialize a Tablestore client.
A data table that meets the following conditions is created. For more information, see Create a data table.
The max versions parameter is set to 1.
The TTL is set to -1 or updates on the data table are prohibited.
Procedure
Step 1: Create a search index
You can create a search index to accelerate data queries. When you create a search index, you must add the fields that you want to query to the search index. You can also configure advanced options such as time to live (TTL) and pre-sorting for the search index based on your business requirements.
The following sample code provides an example on how to create a search index for a data table. In this example, the search index contains the Col_Keyword column of the Keyword type and the Col_Long column of the Long type. Data in the search index is pre-sorted based on the primary key of the data table and never expires.
For information about the data type mappings between search indexes and data tables, see Data types.
private static void createSearchIndex(SyncClient client) {
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
// Specify the name of the data table.
request.setTableName("sampletable");
// Specify the name of the search index.
request.setIndexName("samplesearchindex");
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
// Specify the names and types of the fields.
new FieldSchema("Col_Keyword", FieldType.KEYWORD),
new FieldSchema("Col_Long", FieldType.LONG)));
request.setIndexSchema(indexSchema);
// Call the client to create the search index.
client.createSearchIndex(request);
}Step 2: Query data by using the search index
When you use a search index to query data, you can select the query types based on your business requirements. You can specify the columns that you want to return and the sorting method of the returned data.
The following sample code provides an example on how to query the rows in which the value of the Col_Keyword column exactly matches "hangzhou" in a table:
/**
* Query the rows in which the value of the Col_Keyword column exactly matches "hangzhou" in a table.
* @param client
*/
private static void termQuery(SyncClient client) {
SearchQuery searchQuery = new SearchQuery();
TermQuery termQuery = new TermQuery(); // Set the query type to TermQuery.
termQuery.setFieldName("Col_Keyword"); // Specify the name of the column that you want to query.
termQuery.setTerm(ColumnValue.fromString("hangzhou")); // Specify the keyword that you want to match.
searchQuery.setQuery(termQuery);
//searchQuery.setGetTotalCount(true); // Set the GetTotalCount parameter to true to return the total number of rows that meet the query conditions.
SearchRequest searchRequest = new SearchRequest("sampletable", "samplesearchindex", 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); // Set ReturnAll to true to return all columns.
//columnsToGet.setColumns(Arrays.asList("ColName1","ColName2")); // Configure the Columns parameter to specify the columns that you want to return.
//searchRequest.setColumnsToGet(columnsToGet);
SearchResponse resp = client.search(searchRequest);
//System.out.println("TotalCount: " + resp.getTotalCount()); // Specify that the total number of rows that meet the query conditions instead of the number of rows that are returned is displayed.
System.out.println("Row: " + resp.getRows());
}References
Query types supported by search indexes: match all query, term query, terms query, range query, prefix query, suffix query, wildcard query, match query, match phrase query, exists query, Boolean query, geo query (geo-distance query, geo-bounding box query, geo-polygon query, which can be used only for geo fields), nested query (which can be used only for Nested fields), and KNN vector query
Analyze data by using search indexes: Aggregation
Export data by using search indexes: parallel scan