Create a search index
Create a search index for a table and configure index fields, presorting, time-to-live (TTL), virtual columns, and highlighting with Tablestore SDK for Java.
Prerequisites
Before you begin, make sure that:
-
You have installed the Tablestore SDK for Java and initialized the client.
-
The table exists and its maximum number of data versions is 1.
-
The table TTL is -1, or updates with
UpdateRoware disabled.
Description
Call createSearchIndex to create a search index for a table. You can create multiple search indexes for the same table. In the request, specify the table name, index name, and complete index schema, and add the columns that you want to query to fieldSchemas. The data type of each index field must match the corresponding column type in the table.
public CreateSearchIndexResponse createSearchIndex(CreateSearchIndexRequest request)
Search index creation is asynchronous. After the request succeeds, query the index information and wait until the index status changes to RUNNING before you query data.
The following example creates example_index for example_table with Keyword and Long fields. Without presorting or TTL settings, the index is sorted by primary key and its data does not expire.
String tableName = "example_table";
String indexName = "example_index";
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("category", FieldType.KEYWORD),
new FieldSchema("price", FieldType.LONG)));
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName(tableName);
request.setIndexName(indexName);
request.setIndexSchema(indexSchema);
client.createSearchIndex(request);
Parameters
CreateSearchIndexRequest contains the following parameters:
|
Name |
Type |
Description |
|
tableName (required) |
|
The table name. |
|
indexName (required) |
|
The search index name. |
|
indexSchema (required) |
|
The index schema. |
|
sourceIndexName (optional) |
|
The source index name for index rebuilding. Set this parameter only when you dynamically modify a search index schema. |
|
timeToLive (optional) |
|
The index data TTL in seconds. Default value: -1. Set this parameter to -1 or an integer of at least 86,400. A value of -1 means that data does not expire. Tablestore automatically deletes data after its retention period exceeds this value. To set a value other than -1, disable |
Index schema
indexSchema is of the IndexSchema type and contains the following parameters.
|
Name |
Type |
Description |
|
fieldSchemas (required) |
|
The index fields. |
|
indexSetting (optional) |
|
The index settings. |
|
indexSort (optional) |
|
The index presorting settings. If this parameter is not set and the index does not contain a Nested field, Tablestore sorts by primary key. Nested indexes do not support presorting. |
Index fields
Each element in indexSchema.fieldSchemas[] is of the FieldSchema type and contains the following parameters.
|
Name |
Type |
Description |
|
fieldName (required) |
|
The index field name. The field can map to a primary key column or an attribute column. |
|
fieldType (required) |
|
The index field type. Use Nested for multilayer relationships, JSON for JSON-formatted data, and Geo-point for geographic data. You can also store JSON-formatted data as strings and query it with array or Nested fields. |
|
index (optional) |
|
Specifies whether to create an inverted or spatial index for the field. Default value: |
|
enableHighlighting (optional) |
|
Specifies whether to enable summary and highlighting. Only Text fields support this feature. Default value: |
|
analyzer (optional) |
|
The analyzer for a Text field. If this parameter is not set, single-character tokenization is used. |
|
analyzerParameter (optional) |
|
The analyzer settings. If you set |
|
enableSortAndAgg (optional) |
|
Specifies whether to enable sorting and aggregation. Default value: |
|
isArray (optional) |
|
Specifies whether the field is an array. Write array values as JSON arrays. You do not need to set this parameter for Nested fields. |
|
subFieldSchemas (optional) |
|
The subfields of a Nested field. This parameter is required for a Nested field. |
|
isVirtualField (optional) |
|
Specifies whether the field is a virtual column. Default value: |
|
sourceFieldNames (optional) |
|
The table fields mapped to the virtual column. This parameter is required for a virtual column. |
|
dateFormats (optional) |
|
The date formats supported by a Date field. This parameter is required for a Date field. |
|
vectorOptions (optional) |
|
The vector data type, dimension, and distance metric. This parameter is required for a Vector field. |
|
jsonType (optional) |
|
The index type of a JSON field. Valid values: |
Vector options
indexSchema.fieldSchemas[].vectorOptions is of the VectorOptions type and contains the following parameters.
|
Name |
Type |
Description |
|
dataType (required) |
|
The vector data type. Only |
|
dimension (required) |
|
The vector dimension. Maximum value: 4,096. |
|
metricType (required) |
|
The distance metric. Valid values:
|
Index settings
indexSchema.indexSetting is of the IndexSetting type and contains the following parameter.
|
Name |
Type |
Description |
|
routingFields (optional) |
|
The custom routing fields. Select one or more primary key columns. In most cases, one field is sufficient. If you specify multiple fields, Tablestore concatenates their values. Tablestore uses the routing field values to distribute index data and writes records with the same values to the same data partition. |
Index presorting
indexSchema.indexSort is of the Sort type and contains the following parameter.
|
Name |
Type |
Description |
|
sorters (optional) |
|
The presorters. |
Primary key sorting
An element of the indexSchema.indexSort.sorters[] path whose type is PrimaryKeySort presorts by primary key and contains the following parameter.
|
Name |
Type |
Description |
|
order (optional) |
|
The primary key sort order. Valid values: |
Field sorting
An element of the indexSchema.indexSort.sorters[] path whose type is FieldSort presorts by field value and contains the following parameters.
|
Name |
Type |
Description |
|
fieldName (required) |
|
The field used for presorting. |
|
order (optional) |
|
The field sort order. Valid values: |
|
mode (optional) |
|
The sorting method for a multivalued field. |
Examples
Configure index presorting
The following example presorts index data by the created_at field in ascending order.
String tableName = "example_table";
String indexName = "example_index";
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("category", FieldType.KEYWORD),
new FieldSchema("price", FieldType.LONG),
new FieldSchema("description", FieldType.TEXT),
new FieldSchema("created_at", FieldType.LONG)
.setEnableSortAndAgg(true)));
indexSchema.setIndexSort(new Sort(
Collections.<Sort.Sorter>singletonList(
new FieldSort("created_at", SortOrder.ASC))));
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName(tableName);
request.setIndexName(indexName);
request.setIndexSchema(indexSchema);
client.createSearchIndex(request);
Configure an index TTL
The following example configures a seven-day TTL. Use Tablestore SDK for Java 5.12.0 or later.
String tableName = "example_table";
String indexName = "example_index";
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("category", FieldType.KEYWORD),
new FieldSchema("price", FieldType.LONG)));
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName(tableName);
request.setIndexName(indexName);
request.setIndexSchema(indexSchema);
request.setTimeToLiveInDays(7);
client.createSearchIndex(request);
Create virtual columns
The following example maps the Keyword field category to a Long virtual column and the Long field price to a Keyword virtual column.
String tableName = "example_table";
String indexName = "example_index";
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("category", FieldType.KEYWORD),
new FieldSchema("category_as_long", FieldType.LONG)
.setVirtualField(true)
.setSourceFieldName("category"),
new FieldSchema("price", FieldType.LONG),
new FieldSchema("price_as_keyword", FieldType.KEYWORD)
.setVirtualField(true)
.setSourceFieldName("price")));
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName(tableName);
request.setIndexName(indexName);
request.setIndexSchema(indexSchema);
client.createSearchIndex(request);
Enable summary and highlighting
The following example enables summary and highlighting for the Text field description.
String tableName = "example_table";
String indexName = "example_index";
IndexSchema indexSchema = new IndexSchema();
indexSchema.setFieldSchemas(Arrays.asList(
new FieldSchema("category", FieldType.KEYWORD),
new FieldSchema("price", FieldType.LONG),
new FieldSchema("description", FieldType.TEXT)
.setEnableHighlighting(true)));
CreateSearchIndexRequest request = new CreateSearchIndexRequest();
request.setTableName(tableName);
request.setIndexName(indexName);
request.setIndexSchema(indexSchema);
client.createSearchIndex(request);