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 TairSearch.

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.
    TFT.SEARCH key '{
        "query": {
            "bool" : {
                 "must": [
                     { "term": { "A": "a" } },
                     { "term": { "B": "b" } }
                 ]
             }
        }
    }'
    TFT.SEARCH key '{
        "query": {
            "bool": {
                 "should": [
                     { "term": { "A": "a" } },
                     { "term": { "B": "b" } }
                 ]
             }
        }
    }'
    TFT.SEARCH key '{
        "query": {
            "bool": {
                "must": [
                     { "term": { "A": "a" } }
                ],
                "must_not": [
                     { "term": { "B": "b" } }
                ]
            }
        }
    }'
    TFT.SEARCH key '{
        "query": {
            "bool" : {
                "should": [
                    {
                        "bool": {
                            "must": [
                                 { "term": { "A": "a" } },
                                 { "term": { "B": "b" } }
                             ]
                        }
                    },
                    { "term": { "C": "c" }  }
                ]
            }
        }
    }'