A boolean query (BoolQuery) contains one or more subqueries. It uses these subqueries to check whether a row meets the query conditions. Each subquery can be any query type, including another boolean query.
Prerequisites
An OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created and data is written to the data table. For more information, see Create data tables and Write data.
A search index is created for the data table. For more information, see Create a search index.
Parameters
Parameter | Description |
tableName | The name of the data table. |
indexName | The name of the search index. |
mustQueries | A list of queries. A row must match all subqueries. This is equivalent to the AND operator. |
mustNotQueries | A list of queries. A row must not match any of the subqueries. This is equivalent to the NOT operator. |
filterQueries | A list of queries. A row must match all subqueries in the filter. A filter is similar to a query, but it does not calculate a relevance score based on the number of matched filters. |
shouldQueries | A list of queries. A row can match these subqueries. This is equivalent to the OR operator. A row must match at least the minimum number of shouldQueries subqueries to be considered a match. The more shouldQueries subqueries a row matches, the higher its relevance score. |
minimumShouldMatch | The minimum number of shouldQueries to match. The default value is 1 if only shouldQueries exists at the same level. The default value is 0 if other queries, such as mustQueries, mustNotQueries, and filterQueries, exist at the same level. |
Example
The following example shows how to construct a BoolQuery to perform a boolean query.
var client = require('../client');
var TableStore = require('../../index.js');
var Long = TableStore.Long;
/**
* Use a boolean query to implement (col2 < 4 OR col3 < 5) OR (col2 = 4 AND (col3 = 5 OR col3 = 6)). The logic is as follows:
* boolQuery1 = rangeQuery(col2 < 4) OR rangeQuery(col3 < 5)
* boolQuery2 = termQuery(col3 = 5) OR termQuery(col3 = 6)
* boolQuery3 = termQuery(col2 = 4) AND boolQuery2
* boolQuery4 = boolQuery1 OR boolQuery3
*/
client.search({
tableName: "sampleTable",
indexName: "sampleSearchIndex",
searchQuery: {
offset: 0, // The query offset.
limit: 10, // To get only the row count without retrieving data, set limit to 0. This returns no rows.
getTotalCount: false, // TotalCount in the result indicates the total number of rows in the table. The default value is false, which means it is not returned.
query: { // Construct boolQuery4. Set the query condition to match at least one of boolQuery1 and boolQuery3.
queryType: TableStore.QueryType.BOOL_QUERY,
query: {
shouldQueries: [ // You can use mustQueries, shouldQueries, or mustNotQueries.
{ // Construct boolQuery1. Set the query condition to match at least one of "Query Condition 1" and "Query Condition 2".
queryType: TableStore.QueryType.BOOL_QUERY,
query: {
// shouldQueries: The query condition is that the value of col2 is less than 4 or the value of col3 is less than 5.
shouldQueries:[
{
// Query Condition 1: rangeQuery, the value of col2 is less than 4.
queryType: TableStore.QueryType.RANGE_QUERY,
query:{
fieldName: "col2",
rangeTo: 4
}
},
{
// Query Condition 2: rangeQuery, the value of col3 is less than 5.
queryType: TableStore.QueryType.RANGE_QUERY,
query:{
fieldName: "col3",
rangeTo: 5
}
}
],
minimumShouldMatch:1
}
},
{ // Construct boolQuery3. Set the query condition to match both "Query Condition 3" and boolQuery2.
queryType: TableStore.QueryType.BOOL_QUERY,
query: {
mustQueries: [
// mustQueries: The query condition is that the value of col2 is 4, and the value of col3 is 5 or 6.
{
// Query Condition 3: termQuery, the value of col2 is 4.
queryType:TableStore.QueryType.TERM_QUERY,
query: {
fieldName : "col2",
term: 4
}
},
{ // Construct boolQuery2: Set the query condition to match at least one of "Query Condition 4" and "Query Condition 5".
queryType: TableStore.QueryType.BOOL_QUERY,
query: {
// shouldQueries: The query condition is that the value of col3 is 5 or 6.
shouldQueries:[
{
// Query Condition 4: termQuery, the value of col3 is 5.
queryType: TableStore.QueryType.TERM_QUERY,
query:{
fieldName:"col3",
term: 5
}
},
{
// Query Condition 5: termQuery, the value of col3 is 6.
queryType: TableStore.QueryType.TERM_QUERY,
query:{
fieldName:"col3",
term: 6
}
}
],
minimumShouldMatch:1
}
}
]
}
}
],
minimumShouldMatch: 1 // This is valid only for shouldQueries. It specifies the minimum number of conditions to match.
}
},
},
columnToGet: { // Specifies the columns to return. Valid values: RETURN_SPECIFIED (returns custom columns), RETURN_ALL (returns all columns), RETURN_ALL_FROM_INDEX (returns all columns from the search index), and RETURN_NONE (returns no columns).
returnType: TableStore.ColumnReturnType.RETURN_SPECIFIED,
returnNames: ["col2", "col3", "col4"]
}
}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', JSON.stringify(data, null, 2));
});FAQ
References
The following query types are supported by search indexes: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, vector query, and exists query. You can select a query type to query data 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.