An exists query is also called a 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.
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 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
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created. Data is written to the table. For more information, see Create a data table and Write data.
A search index is created for the data table. For more information, see Create a search index.
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 that you want to query. |
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
This section provides examples on how to perform an exists query.
Query rows that are not empty in the text column
The following sample code shows how to query rows that are not empty in the text column in a table.
$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);
Query rows that are not empty in a subcolumn of a nested column
The following sample code shows how to query rows that are not empty in the nested_long subcolumn of a nested column in a table.
$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);
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.
If you want to sort or paginate the rows that meet the query conditions, you can use the sorting and paging feature. For more information, see Sorting and paging.
If you want to collapse the result set based on a specific column, you can use the collapse (distinct) feature. 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, such as obtaining the extreme values, sum, and total number of rows, you can perform aggregation operations or execute SQL statements. For more information, see Aggregation and SQL query.
If you want to quickly 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.