Exists query is also called NULL query or NULL-value query. This query is used in sparse data to determine whether a column of a row exists. For example, you can query the rows in which the value of the address column is not empty.

Note
  • If you want to check whether a column contains empty values, you must use ExistsQuery together with must_not_queries of BoolQuery.
  • If one of the following conditions is met, the system considers that a column does not to exist. In this example, the city column is used.
    • The type of the city column in the search index is a basic type such as keyword. If a row in which the city column does not exist in the data table, the search index considers that the city column does not exist.
    • The type of the city column in the search index is a basic type such as keyword. If a row in which the value of the city column is an empty array in the data table ("city" = "[]"), the search index considers that the city column does not exist.

Prerequisites

  • The OTSClient 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

Parameter Description
table_name The name of the data table.
index_name The name of the search index.
field_name The name of the column.
query_type The query type. Set the query type to QueryTypeConst::EXISTS_QUERY.
Note You cannot directly query nested columns. To query a nested column, you must encapsulate NestedQuery by specifying the path of the nested column and a subquery. The subquery can be a query of any type. For more information, see Nested query.

Examples

  • Example 1

    The following sample code shows how to query the rows in which the text column is not empty:

    $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::EXISTS_QUERY,
                'query' => array(
                    'field_name' => 'text'
                )
            )
        ),
        'columns_to_get' => array(
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
            'return_names' => array('keyword', 'long', 'array')
        )
    );
    $response = $otsClient->search($request);
  • Example 2

    The following sample code shows how to query the rows in which the nested_long subcolumn of the nested column is not empty:

    $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::NESTED_QUERY,
                'query' => array(
                    'path' => "nested",
                    'query' => array(
                        'query_type' => QueryTypeConst::EXISTS_QUERY,
                        'query' => array(
                            'field_name' => 'nested.nested_long',
                        )
                    ),
                    'score_mode' => ScoreModeConst::SCORE_MODE_AVG
                )
            )
        ),
        'columns_to_get' => array(
            'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
            'return_names' => array('nested')
        )
    );
    $response = $this->otsClient->search($request);