All Products
Search
Document Center

Tablestore:Perform a match phrase query

Last Updated:Apr 30, 2026

A match phrase query returns rows only when the tokens in a column match the keyword in both order and position. Unlike a match query, token positions matter. If the target column uses fuzzy tokenization, a match phrase query runs at lower latency than a wildcard query.

Prerequisites

Parameters

Parameter

Description

FieldName

The name of the column to query. The column can be of the TEXT type.

Text

The keyword to match against column values.

For TEXT columns, the keyword is tokenized based on the analyzer type specified when the search index was created. If no analyzer type is specified, single-word tokenization is used by default.

For example, querying with the phrase "this is" returns rows such as "..., this is tablestore" and "this is a table", but not "this table is ..." or "is this a table".

Query

The query type. Set to MatchPhraseQuery.

TableName

The name of the data table.

IndexName

The name of the search index.

GetTotalCount

Whether to return the total number of matching rows. Default value: false. Setting this to true reduces query performance.

ColumnsToGet

The columns to return from matching rows. Configure using ReturnAll, Columns, or ReturnAllFromIndex.

The default value of ReturnAll is false, which specifies that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.

  • Configure Columns to specify the columns that you want to return.

  • Set ReturnAllFromIndex to true to return all columns from the search index.

If you set ReturnAll to true, all columns are returned.

Example

The following example queries rows where the Text_type_col column matches the phrase "Tablestore SearchIndex".

/// <summary>
/// Query rows where Text_type_col matches "Tablestore SearchIndex" and return the total match count.
/// </summary>
/// <param name="otsClient"></param>
public static void MatchPhraseQuery(OTSClient otsClient)
{
    var searchQuery = new SearchQuery();
    // Set the query type to MatchPhraseQuery.
    searchQuery.Query = new MatchPhraseQuery("Text_type_col", "Tablestore SearchIndex");
    // Return the total number of matching rows.
    searchQuery.GetTotalCount = true;
    var request = new SearchRequest(TableName, IndexName, searchQuery);
    // Specify the columns to return. If not set, only primary key columns are returned.
    request.ColumnsToGet = new ColumnsToGet()
    {
        // Return all columns in the search index.
        ReturnAllFromIndex = true
        // Return specific columns.
        //Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
        // Return all columns in the matching rows.
        //ReturnAll = true
    };

    var response = otsClient.Search(request);

    // TotalCount is the number of matching rows, not the number of returned rows.
    Console.WriteLine("Total Count:" + response.TotalCount);
}

FAQ

References

  • When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, and exists query. You can use different query methods to query data from multiple dimensions based on your business requirements.

    You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.

    You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).

  • If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.

  • If you want to obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.