All Products
Search
Document Center

Tair (Redis® OSS-Compatible):Use the bool query to perform compound queries in TairSearch

Last Updated:Oct 24, 2024

TairSearch is an in-house data structure for full-text search, and uses syntax that is similar to that of Elasticsearch. This topic describes how to use the bool query in TairSearch to implement compound queries.

Syntax

TairSearch supports the following types of clauses for the bool query:

  • must: specifies the elements that are required for documents to be returned. The semantics of this clause is similar to that of AND.

  • must_not: specifies the elements that cannot be contained in documents to be returned. The semantics of this clause is similar to that of NOT.

  • should: specifies the elements that are optional for documents to be returned. The semantics of this clause is similar to that of OR. You can use the minimum_should_match parameter together with this clause to specify the minimum number of should clauses that documents to be returned must match. If a bool query contains only should clauses, this parameter is set to 1. If the bool query also contains must or must_not clauses, this parameter is set to 0.

    If a document matches both must and should clauses, the score of this document increases as a result of multiple hits. This affects the ranking of this document in the results.

TairSearch_boolThe priority of these clauses in the bool query is must_not > must > should. Additionally, bool statements or any other statements can be nested inside of a bool statement to implement compound queries. For more information about TairSearch, see Search.

Example

  1. Create an index.

    TFT.CREATEINDEX key '{
        "mappings": {
            "properties": {
                "A": { "type": "keyword" },
                "B": { "type": "keyword" },
                "C": { "type": "keyword" }
            }
        }
    }'
  2. Add document data.

    TFT.ADDDOC key '{
        "A": "a",
        "B": "b",
        "C": "c"
    }'
  3. Query data.

    示例1:SELECT A = a and B = b

    TFT.SEARCH key '{
        "query": {
            "bool" : {
                 "must": [
                     { "term": { "A": "a" } },
                     { "term": { "B": "b" } }
                 ]
             }
        }
    }'

    示例2:SELECT A = a or B = b

    TFT.SEARCH key '{
        "query": {
            "bool": {
                 "should": [
                     { "term": { "A": "a" } },
                     { "term": { "B": "b" } }
                 ]
             }
        }
    }'

    示例3:SELECT A = a and B != b

    TFT.SEARCH key '{
        "query": {
            "bool": {
                "must": [
                     { "term": { "A": "a" } }
                ],
                "must_not": [
                     { "term": { "B": "b" } }
                ]
            }
        }
    }'

    示例4:SELECT (A = a and B = b) or C = c

    TFT.SEARCH key '{
        "query": {
            "bool" : {
                "should": [
                    {
                        "bool": {
                            "must": [
                                 { "term": { "A": "a" } },
                                 { "term": { "B": "b" } }
                             ]
                        }
                    },
                    { "term": { "C": "c" }  }
                ]
            }
        }
    }'