A Boolean query combines multiple subqueries to retrieve rows from a data table. Tablestore returns only the rows that satisfy the combined 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 a data table and Write data.
-
A search index is created for the data table. For more information, see Create search indexes.
Parameters
|
Parameter |
Description |
|
TableName |
The name of the data table. |
|
IndexName |
The name of the search index. |
|
MustQueries |
Logical AND operator. All subqueries in this list must match. Only rows that satisfy every condition are returned. |
|
MustNotQueries |
Logical NOT operator. Rows that match any subquery in this list are excluded from the results. |
|
FilterQueries |
Logical AND operator applied as a filter. Only rows that satisfy all subfilters are returned. Unlike MustQueries, FilterQueries does not calculate a relevance score. It performs a yes-or-no match, making it well suited for exact matches, ranges, and numeric conditions. |
|
ShouldQueries |
Logical OR operator. Rows that match at least the minimum number of subqueries are returned. Matching more subqueries increases the relevance score of a row. If no MustQueries, MustNotQueries, or FilterQueries conditions are set, at least one ShouldQuery must match. If other conditions are set, ShouldQueries become optional relevance boosters with a default minimum of 0. Set MinimumShouldMatch to override the default. |
|
MinimumShouldMatch |
The minimum number of ShouldQueries conditions a row must satisfy to be returned. Defaults to 1 when ShouldQueries is the only condition, and 0 when MustQueries, MustNotQueries, or FilterQueries conditions are also present. |
Sample code
The following examples show how to construct a Boolean query. The first uses MustQueries to require all conditions, and the second uses ShouldQueries to require at least one condition.
/**
* Perform a Boolean query based on a combination of subqueries.
*/
func BoolQuery(client *tablestore.TableStoreClient, tableName string, indexName string) {
searchRequest := &tablestore.SearchRequest{}
searchRequest.SetTableName(tableName)
searchRequest.SetIndexName(indexName)
// Condition 1: range query — Col_Long > 3
rangeQuery := &search.RangeQuery{}
rangeQuery.FieldName = "Col_Long"
rangeQuery.GT(3)
// Condition 2: match query — Col_Keyword contains "hangzhou"
matchQuery := &search.MatchQuery{}
matchQuery.FieldName = "Col_Keyword"
matchQuery.Text = "hangzhou"
{
// MustQueries: both conditions must match (AND)
boolQuery := &search.BoolQuery{
MustQueries: []search.Query{
rangeQuery,
matchQuery,
},
}
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(boolQuery)
searchRequest.SetSearchQuery(searchQuery)
searchResponse, err := client.Search(searchRequest)
if err != nil {
fmt.Printf("%#v", err)
return
}
fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Whether all matching rows were returned
fmt.Println("TotalCount: ", searchResponse.TotalCount) // Total rows matching the query
fmt.Println("RowCount: ", len(searchResponse.Rows))
}
{
// ShouldQueries: at least one condition must match (OR)
boolQuery := &search.BoolQuery{
ShouldQueries: []search.Query{
rangeQuery,
matchQuery,
},
MinimumShouldMatch: proto.Int32(1),
}
searchQuery := search.NewSearchQuery()
searchQuery.SetQuery(boolQuery)
searchRequest.SetSearchQuery(searchQuery)
searchResponse, err := client.Search(searchRequest)
if err != nil {
fmt.Printf("%#v", err)
return
}
fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Whether all matching rows were returned
fmt.Println("TotalCount: ", searchResponse.TotalCount) // Total rows matching the query
fmt.Println("RowCount: ", len(searchResponse.Rows))
}
}
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, geo query, Boolean query, KNN vector query, nested query, and exists query. You can use the query methods provided by the search index to query data from multiple dimensions based on your business requirements.
You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.
You can use the collapse (distinct) feature to collapse the result set based on a specific column. This way, data of the specified type appears only once in the query results. For more information, see Collapse (deduplicate).
If you want to analyze data in a data table, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.
If you want to 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 Perform a parallel scan.