A Boolean query retrieves data in a data table based on a combination of subqueries. Tablestore returns the rows that match the subquery conditions. A subquery can be of any type, including Boolean query.
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.
- 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. |
must_queries | The list of subquery conditions. Only rows that meet all subquery conditions are returned. This parameter is equivalent to the AND operator. |
must_not_queries | The list of subquery conditions. Only rows that do not meet any subquery conditions are returned. This parameter is equivalent to the NOT operator. |
filter_queries | The list of subquery conditions. Only rows that meet all subfilters are returned. A filter is similar to a query except that no relevance score is calculated in a filter based on the number of subfilters that a row meets. |
should_queries | The list of subquery conditions. Not all the subquery conditions need to be met for the query results to return. This parameter is equivalent to the OR operator. Only rows that meet the minimum number of subquery conditions specified by the should_queries parameter are returned. A higher overall relevance score indicates that more subquery conditions specified by the should_queries parameter are met. |
minimum_should_match | The minimum number of subquery conditions specified by the should_queries parameter that the returned rows must meet. If only the should_queries parameter is used to specify subquery conditions, the default value of the minimum_should_match parameter is 1. If one or more of the must_queries, must_not_queries, and filter_queries parameters are also used to specify subquery conditions, the default value of the minimum_should_match parameter is 0. |
Examples
This section provides examples on the use of Boolean queries.
AND
The following sample code shows how to perform a Boolean query by using the must_queries parameter and two query conditions that are in an AND relationship.
$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::BOOL_QUERY,
'query' => array(
'must_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
)
),
'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('keyword', 'long')
)
);
$response = $otsClient->search($request);
OR
The following sample code shows how to perform a Boolean query by using the should_queries parameter and two query conditions that are in an OR relationship.
$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::BOOL_QUERY,
'query' => array(
'should_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
'minimum_should_match' => 1
)
),
'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('keyword', 'long')
)
);
$response = $otsClient->search($request);
NOT
The following sample code shows how to perform a Boolean query by using the must_not_queries parameter and two query conditions that are in a NOT relationship.
$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::BOOL_QUERY,
'query' => array(
'must_not_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
)
),
'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('keyword', 'long')
)
);
$response = $otsClient->search($request);
filter_queries
The following sample code shows how to perform a Boolean query by using the filter_queries parameter and two query conditions. The rows are not relevance-scored based on the number of conditions that are met.
$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::BOOL_QUERY,
'query' => array(
'filter_queries' => array(
array(
'query_type' => QueryTypeConst::TERM_QUERY,
'query' => array(
'field_name' => 'keyword',
'term' => 'keyword'
)
),
array(
'query_type' => QueryTypeConst::RANGE_QUERY,
'query' => array(
'field_name' => 'long',
'range_from' => 100,
'include_lower' => true,
'range_to' => 101,
'include_upper' => false
)
)
),
)
),
'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('keyword', 'long')
)
);
$response = $otsClient->search($request);
Combined queries
The following sample code provides an example on how to perform a Boolean query to query the rows that meet a combination of subquery conditions, which is (col2<4 or col3<5) or (col2 = 4 and (col3 = 5 or col3 =6)). In the preceding combination of subquery conditions, the Boolean subqueries are connected by AND or OR operators.
$request = array(
'table_name' => 'php_sdk_test',
'index_name' => 'php_sdk_test_index',
'search_query' => [
'offset' => 0,
'limit' => 10,
'get_total_count' => false,
'query' => [
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => [
// Final combination: (col2 < 4 or col3 < 5) or (col2 = 4 and (col3 = 5 or col3 = 6))
'should_queries' => [
[
'query_type' => QueryTypeConst::BOOL_QUERY,
'query' => [
// Combination 1: col2 < 4 or col3 < 5
'should_queries' => [
[
'query_type' => QueryTypeConst::RANGE_QUERY,
// Condition 1: col2 < 4
'query' => [
'field_name' => 'col2',
'range_to' => 4
]
],
[
'query_type' => QueryTypeConst::RANGE_QUERY,
// Condition 2: col3 < 5
'query' => [
'field_name' => 'col3',
'range_to' => 5
]
]
]
]
],
[
'query_type' => QueryTypeConst::BOOL_QUERY,
// Combination 2: (col2 = 4 and (col3 = 5 or col3 = 6))
'query' => [
'must_queries' => [
[
'query_type' => QueryTypeConst::TERM_QUERY,
// Condition 3: col2 = 4
'query' => [
'field_name' => 'col2',
'term' => 4
]
],
[
'query_type' => QueryTypeConst::BOOL_QUERY,
// Combination 3: (col3 = 5 or col3 = 6)
'query' => [
'should_queries' => [
[
'query_type' => QueryTypeConst::TERM_QUERY,
// Condition 4: col3 = 5
'query' => [
'field_name' => 'col3',
'term' => 5
]
],
[
'query_type' => QueryTypeConst::TERM_QUERY,
// Condition 5: col3 = 6
'query' => [
'field_name' => 'col3',
'term' => 6
]
]
],
'minimum_should_match' => 1
]
]
]
]
]
],
'minimum_should_match' => 1
]
]
],
// You can configure the columns_to_get parameter to specify the columns to return or specify that all columns are returned. If you do not configure this parameter, only the primary key columns are returned.
'columns_to_get' => [
//'return_type' => ColumnReturnTypeConst::RETURN_ALL // Specify that all columns are returned.
// Specify that the specified columns are returned.
'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED,
'return_names' => array('col1', 'col2')
]
);
$response = $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.