Match phrase query is similar to match query, except match phrase query evaluates the positions of tokens. A row meets the query condition only when the order and positions of the tokens in the row match the order and positions of the tokens that are contained in the keyword. If the tokenization method for the column that you want to query is fuzzy tokenization, match phrase query is performed at a lower latency than wildcard query.

Prerequisites

  • An OTSClient instance is initialized. For more information, see Initialization.
  • A data table is created. Data is written to the table.
  • A search index is created for the data table. For more information, see Create search indexes.

Parameters

ParameterDescription
table_nameThe name of the table.
index_nameThe name of the search index.
offsetThe position from which the current query starts.
limitThe maximum number of rows that the current query returns.

To query only the number of matched rows but not detailed data, you can set Limit to 0. In this case, Tablestore returns the number of matched rows without data from the table.

get_total_countSpecify whether to return the total number of rows that match the query conditions. By default, this parameter is set to false, which indicates that the total number of rows that match the query conditions is not returned.

Query performance is affected when this parameter is set to true.

query_typeThe query type. To use phrase query, set this parameter to QueryTypeConst::MATCH_PHRASE_QUERY.
field_nameThe name of the column used to match the query conditions.

Match query applies to TEXT columns.

textThe keyword used for the query to match the column values.

If the column used for the match query is a TEXT column, the keyword is tokenized into multiple keywords based on the analyzer you set when you create the search index. By default, single-word tokenization is performed if you do not set the analyzer when you create the search index.

For example, if you use the phrase "this is" as the keyword for a query, you can obtain query results such as "..., this is tablestore" and "this is a table". Query results such as "this table is ..." or "is this a table" cannot be obtained.

columns_to_getSpecifies whether to return all columns of each row that meets the query conditions. You can configure return_type and return_names for this parameter.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_SPECIFIED, you can use return_names to specify the columns that you want to return.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_ALL, all columns are returned.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_ALL_FROM_INDEX, all columns in the search index are returned.
  • If you set the return_type parameter to ColumnReturnTypeConst::RETURN_NONE, only the primary key columns are returned.

Examples

$request = array(
    'table_name' => 'php_sdk_test',
    'index_name' => 'php_sdk_test_search_index',
    'search_query' => array(
        'offset' => 0,
        'limit' => 2,
        'get_total_count' => true,
        'query' => array(
            'query_type' => QueryTypeConst::MATCH_PHRASE_QUERY,
            'query' => array(
                'field_name' => 'text',
                'text' => 'text keyword'
            )
        ),
        'sort' => array(
            array(
                'field_sort' => array(
                    'field_name' => 'keyword',
                    'order' => SortOrderConst::SORT_ORDER_ASC
                )
            ),
        )
    ),
    'columns_to_get' => array(
        'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
        'return_names' => array('text')
    )
);
$response = $otsClient->search($request);