A term query with the Tablestore SDK for Java matches field values or tokens that exactly equal the query term and returns matching rows or their total count.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Feature description
A term query matches content in a specified field that exactly equals the query term. For non-text fields such as Keyword and Long fields, the entire field value must exactly equal the query term. For a Text field (see String types), the row matches if any token produced by the analyzer exactly equals the query term. The query term itself is not tokenized.
The tokens generated for a Text field may change with the analyzer configuration, algorithm updates, and language usage. Avoid using a term query to match the entire original string of a Text field. Instead, use a virtual column to map the source field to the Keyword type, and query the virtual column.
To call search, set the query type to TermQuery, and use SearchQuery to configure the number of returned rows, total count tracking, and other common query behaviors.
SearchResponse search(SearchRequest request)
The following example queries rows whose category field value exactly equals books and returns up to 10 rows and the total number of matching rows.
String tableName = "example_table";
String indexName = "example_index";
TermQuery termQuery = new TermQuery();
termQuery.setFieldName("category");
termQuery.setTerm(ColumnValue.fromString("books"));
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(termQuery);
searchQuery.setLimit(10);
searchQuery.setTrackTotalCount(SearchQuery.TRACK_TOTAL_COUNT);
SearchRequest request = new SearchRequest(tableName, indexName, searchQuery);
SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
columnsToGet.setReturnAll(true);
request.setColumnsToGet(columnsToGet);
SearchResponse response = client.search(request);
System.out.println(response.getTotalCount());
System.out.println(response.getRows());
Parameters
Search request
request is a SearchRequest object that contains the following parameters.
|
Name |
Type |
Description |
|
tableName (required) |
String |
The name of the table. |
|
indexName (required) |
String |
The name of the search index. |
|
searchQuery (required) |
SearchQuery |
The query condition and common query settings. |
|
columnsToGet (optional) |
SearchRequest.ColumnsToGet |
The returned column settings. If you omit this parameter, only primary key columns are returned. |
|
timeoutInMillisecond (optional) |
int |
The request-level query timeout in milliseconds. The default value is |
|
routingValues (optional) |
|
The primary key values for custom routing fields. Omit this parameter if the index does not use custom routing. |
Query settings
request.searchQuery is a SearchQuery object that contains the following parameters.
|
Name |
Type |
Description |
|
query (required) |
Query |
The query condition. Set this parameter to |
|
offset (optional) |
Integer |
The starting position of the query. |
|
limit (optional) |
Integer |
The maximum number of rows to return. Set this parameter to |
|
highlight (optional) |
Highlight |
The summary and highlighting settings for Text fields. For configuration details, see Summary and highlighting. |
|
collapse (optional) |
Collapse |
The field collapse settings, which deduplicate results by a specified field. For configuration details, see Collapse query results. |
|
sort (optional) |
Sort |
The result sort order. For configuration details, see Sort and paginate results. |
|
trackTotalCount (optional) |
int |
The expected maximum number of matching rows to count. The default value is |
|
filter (optional) |
SearchFilter |
A filter that is applied to the results of |
|
aggregationList (optional) |
|
The aggregation settings. For configuration details, see Aggregation. |
|
groupByList (optional) |
|
The grouping settings. For configuration details, see Aggregation. |
|
token (optional) |
byte[] |
The pagination token. Set this parameter to the |
Query condition
request.searchQuery.query is a TermQuery object that contains the following parameters.
|
Name |
Type |
Description |
|
fieldName (required) |
String |
The name of the indexed field to query. |
|
term (required) |
ColumnValue |
The query term. For a Text field, the entire value is used as a single term and is not tokenized. |
|
weight (optional) |
float |
The relevance weight of the query condition. The value must be a positive floating-point number. A larger value gives the query condition a greater contribution to the BM25 relevance score. This parameter does not affect matching or the number of rows returned. It affects result order only when |
Returned columns
request.columnsToGet is a SearchRequest.ColumnsToGet object that contains the following parameters.
|
Name |
Type |
Description |
|
columns (optional) |
|
The attribute columns to return. Set this parameter only if |
|
returnAll (optional) |
boolean |
Specifies whether to return all attribute columns in the table. Default value: |
|
returnAllFromIndex (optional) |
boolean |
Specifies whether to return all indexed attribute columns. Default value: |
Return values
search returns a SearchResponse object. The following table describes the core fields.
|
Name |
Type |
Description |
|
totalCount |
long |
The number of matching rows. Call |
|
rows |
|
The rows returned by this query. Call |
|
searchHits |
|
The query hits. Call |
|
nextToken |
byte[] |
The next-page token. Call |
|
isAllSuccess |
boolean |
Indicates whether all index partitions were queried successfully. Call |