All Products
Search
Document Center

Tablestore:Match phrase query

Last Updated:Jul 27, 2026

A match phrase query with the Tablestore SDK for Java searches a Text field based on token order and positions and returns rows that satisfy the phrase condition with relevance scores.

Prerequisites

Install the Tablestore SDK for Java and initialize the client.

Feature description

A match phrase query searches for tokens that occur consecutively in the specified order and positions in a Text field. For information about the field type, see String types. 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.

A match phrase query requires all query tokens to occur in the same order and at adjacent positions. For example, the query text this is matches this is tablestore but does not match this table is or is this a table. A match query only checks whether tokens match and does not require the tokens to be adjacent or in the same order as the query text.

If a Text field uses the fuzzy analyzer, a match phrase query can provide fuzzy matching similar to a wildcard query with lower query latency.

The following example queries rows whose description field consecutively contains the tablestore and durable tokens in order. The query returns up to 10 rows, the total number of matching rows, and relevance scores.

String tableName = "example_table";
String indexName = "example_index";

MatchPhraseQuery matchPhraseQuery = new MatchPhraseQuery();
matchPhraseQuery.setFieldName("description");
matchPhraseQuery.setText("tablestore durable");

SearchQuery searchQuery = new SearchQuery();
searchQuery.setQuery(matchPhraseQuery);
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 -1, which does not set a separate query timeout.

routingValues (optional)

List<PrimaryKey>

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 MatchPhraseQuery for a match phrase query.

offset (optional)

Integer

The starting position of the query.

limit (optional)

Integer

The maximum number of rows to return. Set this parameter to 0 to return no rows.

highlight (optional)

Highlight

The summary and highlighting settings for Text fields. For configuration details and limits, 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 TRACK_TOTAL_COUNT_DISABLED, which disables counting. Set this parameter to TRACK_TOTAL_COUNT to count all matching rows. A smaller value improves query performance.

filter (optional)

SearchFilter

A filter that is applied to the results of query.

aggregationList (optional)

List<Aggregation>

The aggregation settings. For configuration details, see Aggregation.

groupByList (optional)

List<GroupBy>

The grouping settings. For configuration details, see Aggregation.

token (optional)

byte[]

The pagination token. Set this parameter to the nextToken value from the previous response to continue reading rows. When you set token, the SDK clears sort because the token already contains the sort conditions.

Phrase match condition

request.searchQuery.query is a MatchPhraseQuery object that contains the following parameters.

Name

Type

Description

fieldName (required)

String

The name of the Text index field to query.

text (required)

String

The query text. The text is analyzed by using the field analyzer and matched based on token order and positions.

weight (optional)

float

The query weight. The default value is 1.0 and the value must be a positive floating-point number. A larger value increases this query's contribution to the relevance score without changing the matching scope.

Returned columns

request.columnsToGet is a SearchRequest.ColumnsToGet object that contains the following parameters.

Name

Type

Description

columns (optional)

List<String>

The attribute columns to return. Set this parameter only if both returnAll and returnAllFromIndex are false. If you omit this parameter, only primary key columns are returned.

returnAll (optional)

boolean

Specifies whether to return all attribute columns in the table. The default value is false.

returnAllFromIndex (optional)

boolean

Specifies whether to return all indexed attribute columns. The default value is false. This parameter and returnAll cannot both be true.

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 getTotalCount() to obtain the value. The returned value depends on the trackTotalCount setting.

rows

List<Row>

The rows returned in the current response. Call getRows() to obtain the value. The number of rows does not exceed limit.

searchHits

List<SearchHit>

The query hits. Call getSearchHits() to obtain the value. This field contains relevance scores and summary and highlighting results.

nextToken

byte[]

The next-page token. Call getNextToken() to obtain the value. If the value is not null, set it as token in the next request to continue reading rows.

isAllSuccess

boolean

Indicates whether all index partitions were queried successfully. Call isAllSuccess() to obtain the value. If the value is false, the response contains partial results and totalCount may be less than the actual number of matching rows.

Query hit

response.searchHits[] is a SearchHit object that contains the following main fields.

Name

Type

Description

row

Row

The matching row. Call getRow() to obtain the value.

score

Double

The relevance score. Call getScore() to obtain the value. When ScoreSort is used, this field contains the actual score. Matching tokens and weight affect the score.

highlightResultItem

HighlightResultItem

The summary and highlighting result. Call getHighlightResultItem() to obtain the value.