This topic describes how to use Boolean query to query data based on a combination of subqueries. Tablestore returns the rows that match the subqueries. Each subquery can be of any type, including BoolQuery.

Prerequisites

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 match all subfilters are returned. filter is similar to query except that filter does not calculate the relevance score based on the number of subfilters that the row matches.
ShouldQueries The list of subqueries that the query results can or cannot match. This parameter is equivalent to the OR operator.

Only rows that meet the minimum number of subquery conditions specified by ShouldQueries are returned.

A higher overall relevance score indicates that more subquery conditions specified by ShouldQueries are met.

MinimumShouldMatch The minimum number of subquery conditions specified by ShouldQueries that the 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 MustQueries, MustNotQueries, and FilterQueries, are specified, the default value of the MinimumShouldMatch parameter is 0.

Example

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 where the Col_Long column value is greater than 3. 
     */
    rangeQuery := &search.RangeQuery{}
    rangeQuery.FieldName = "Col_Long"
    rangeQuery.GT(3)

    /**
     * Condition 2: Perform a match query to query the rows where the Col_Keyword column value matches hangzhou. 
     */
    matchQuery := &search.MatchQuery{}
    matchQuery.FieldName = "Col_Keyword"
    matchQuery.Text = "hangzhou"

    {
        /**
         * Construct a Boolean query where the rows must meet both Condition 1 and 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 the returned results are complete. 
        fmt.Println("TotalCount: ", searchResponse.TotalCount) // Specify that all rows that match the specified conditions are displayed. The number of rows to return is not displayed. 
        fmt.Println("RowCount: ", len(searchResponse.Rows))
    }
    {
        /**
         * Construct a Boolean query where the rows meet at least one of Condition 1 and 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 the returned results are complete. 
        fmt.Println("TotalCount: ", searchResponse.TotalCount) // Specify that all rows that match the specified conditions are displayed. The number of rows to return is not displayed. 
        fmt.Println("RowCount: ", len(searchResponse.Rows))
    }
}