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

Prerequisites

  • The OTSClient is initialized. For more information, see Initialization.
  • 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 search indexes.

Parameters

Parameter Description
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. A filter is similar to a query except that a 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.
TableName The name of the table.
IndexName The name of the search index.
ColumnsToGet Specifies whether to return all columns of each row that meets the query conditions. You can configure ReturnAll, Columns, and ReturnAllFromIndex for this parameter.
The default value of ReturnAll is false, which indicates that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.
  • Configure Columns to specify the columns that you want to return.
  • Set ReturnAllFromIndex to true to return all columns from the search index.

If you set ReturnAll to true, all columns are returned.

Examples

/// <summary>
/// A Boolean query contains one or more subqueries. Each subquery has its own type. 
/// MustQueries: The rows must meet the conditions. 
/// ShouldQueries: More than one condition is included in ShouldQueries. If a row meets at least one condition specified by ShouldQueries, the row meets the query conditions. 
/// MustNotQueries: The rows must not meet the conditions. 
///MinimumShouldMatch: The rows must meet the minimum number of conditions. 
/// </summary>
/// <param name="otsClient"></param>
public static void BoolQuery(OTSClient otsClient)
{
    Console.WriteLine("\n Start bool query...");

    var searchQuery = new SearchQuery();
    // Specify that the total number of rows that meet the query conditions is returned. 
    searchQuery.GetTotalCount = true;
    var boolQuery = new BoolQuery();
    var shouldQuerys = new List<IQuery>();
    shouldQuerys.Add(new TermQuery(Keyword_type_col, new ColumnValue("SearchIndex")));
    shouldQuerys.Add(new TermQuery(Keyword_type_col, new ColumnValue("TableStore")));
    boolQuery.ShouldQueries = shouldQuerys;
    boolQuery.MinimumShouldMatch = 1;

    searchQuery.Query = boolQuery;

    var request = new SearchRequest(TableName, IndexName, searchQuery);
    // You can use the ColumnsToGet parameter to specify the columns that you want to return, specify that all columns are returned, or specify that all columns in the search index are returned. If you do not configure this parameter, only the primary key columns are returned. 
    request.ColumnsToGet = new ColumnsToGet()
    {
        // Specify that all columns in the search index are returned. 
        ReturnAllFromIndex = true
        // Specify the columns that you want to return. 
        //Columns = new List<string>() { Long_type_col, Text_type_col, Keyword_type_col }
        // Specify that all columns are returned. 
        //ReturnAll = true
    };

    var response = otsClient.Search(request);

    Console.WriteLine("Total Count:" + response.TotalCount); // Specify that the total number of rows that meet the query conditions instead of the number of rows that are returned is displayed. 
    foreach (var row in response.Rows)
    {
        PrintRow(row);
    }
}