A match query with the Tablestore SDK for Java searches Text or Keyword fields and returns rows that satisfy the matching conditions with relevance scores.
Prerequisites
Install the Tablestore SDK for Java and initialize the client.
Feature description
A match query searches Text or Keyword fields. For information about the field types, see String types. The two field types use different matching behavior:
Text: The field value and query text are analyzed by using the analyzer configured when the search index is created. If no analyzer is configured, single-word tokenization is used by default. The defaultORoperator matches a field value that contains any query token. You can use theANDoperator to require all query tokens or specify the minimum number of tokens that must match.Keyword: Neither the field value nor the query text is analyzed. A row matches only if the complete field value equals the query text.
A match query does not require matching tokens to be adjacent or in the same order as the query text. To match tokens in order, use a match phrase query. If a Text field uses the fuzzy analyzer and you need high-performance fuzzy search, a match phrase query is also recommended.
The following example queries rows whose description field contains the tablestore or durable token and returns up to 10 rows, the total number of matching rows, and relevance scores.
String tableName = "example_table";
String indexName = "example_index";
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("description");
matchQuery.setText("tablestore durable");
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(matchQuery);
searchQuery.setSort(new Sort(Collections.singletonList(new ScoreSort())));
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);
for (SearchHit hit : response.getSearchHits()) {
System.out.println(hit.getRow());
System.out.println(hit.getScore());
}
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 |
|
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 |
Match condition
request.searchQuery.query is a MatchQuery object that contains the following parameters.
|
Name |
Type |
Description |
|
fieldName (required) |
String |
The name of the |
|
text (required) |
String |
The query text. For a |
|
operator (optional) |
QueryOperator |
The operator used to combine query tokens. |
|
minShouldMatch (optional) |
String or int |
The minimum number of query tokens that must match when |
|
weight (optional) |
float |
The query weight. The default value is |
setMinimumShouldMatch(Integer) is deprecated. Use setMinShouldMatch(int) or setMinShouldMatch(String).
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 both |
|
returnAll (optional) |
boolean |
Specifies whether to return all attribute columns in the table. The default value is |
|
returnAllFromIndex (optional) |
boolean |
Specifies whether to return all indexed attribute columns. The default value is |
Return values
Query response
The search method returns a SearchResponse object. The following table describes the main fields.
|
Name |
Type |
Description |
|
totalCount |
long |
The number of matching rows. Call |
|
rows |
|
The rows returned in the current response. Call |
|
searchHits |
|
The query hits. Call |
|
nextToken |
byte[] |
The next-page token. Call |
|
isAllSuccess |
boolean |
Indicates whether all index partitions were queried successfully. Call |
Query hit
response.searchHits[] is a SearchHit object that contains the following main fields.
|
Name |
Type |
Description |
|
row |
Row |
The matching row. Call |
|
score |
Double |
The relevance score. Call |
|
highlightResultItem |
HighlightResultItem |
The summary and highlighting result. Call |
Examples
Match all query tokens
Set operator to AND to match a row only if the field value contains all query tokens.
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("description");
matchQuery.setText("tablestore durable");
matchQuery.setOperator(QueryOperator.AND);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(matchQuery);
Set the minimum number of matching tokens
When the OR operator is used, set minShouldMatch to specify the minimum number of query tokens that must match. The following example requires at least two matching tokens.
MatchQuery matchQuery = new MatchQuery();
matchQuery.setFieldName("description");
matchQuery.setText("tablestore durable cloud");
matchQuery.setOperator(QueryOperator.OR);
matchQuery.setMinShouldMatch(2);
SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(matchQuery);