A Boolean query retrieves data in the data table based on a combination of subqueries. Tablestore returns the rows that match the subqueries. 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 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 | The list of subqueries that the query results must match. This parameter is equivalent to the AND operator. |
MustNotQueries | The list of subqueries that the query results must not match. This parameter is equivalent to the NOT operator. |
FilterQueries | The list of subqueries. Only rows that meet all subfilters are returned. A filter is similar to a query except that a filter does not calculate the relevance score based on the number of subfilters that a row meets. |
ShouldQueries | The list of subqueries. The rows that meet the minimum number of the specified subquery conditions are returned. The subqueries are in the OR relationship. Only rows that meet at least the minimum number of subquery conditions specified by the ShouldQueries parameter are returned. A higher overall relevance score indicates that more subquery conditions specified by the ShouldQueries parameter are met. |
MinimumShouldMatch | The minimum number of subquery conditions specified by the ShouldQueries parameter that the returned rows must meet. If no other subquery conditions except the subquery conditions that are specified by ShouldQueries are specified, the default value of the MinimumShouldMatch parameter is 1. If other subquery conditions, such as subquery conditions specified by the MustQueries, MustNotQueries, or FilterQueries parameter, are specified, the default value of the MinimumShouldMatch parameter is 0. |
Sample code
The following sample code provides an example on how to perform a Boolean query based on a combination of subqueries:
/**
* 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: Perform a range query to query the rows in which the value of the Col_Long column is greater than 3.
*/
rangeQuery := &search.RangeQuery{}
rangeQuery.FieldName = "Col_Long"
rangeQuery.GT(3)
/**
* Condition 2: Perform a match query to query the rows in which the value of the Col_Keyword column is hangzhou.
*/
matchQuery := &search.MatchQuery{}
matchQuery.FieldName = "Col_Keyword"
matchQuery.Text = "hangzhou"
{
/**
* Construct a Boolean query in which the rows must meet Query Condition 1 and Query Condition 2.
*/
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) // Check whether all rows that meet the query conditions are returned.
fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows.
fmt.Println("RowCount: ", len(searchResponse.Rows))
}
{
/**
* Construct a Boolean query in which the rows meet at least one of Query Condition 1 and Query Condition 2.
*/
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) // Check whether all rows that meet the query conditions are returned.
fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows.
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 (distinct).
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 Parallel scan.