Unlock the Power of AI

1 million free tokens

88% Price Reduction

Activate Now

Use the bool query in TairSearch to perform compound queries

Updated at: 2024-11-21 02:14

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 the semantics of AND.

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

  • should: specifies the elements that are optional for documents to be returned. The semantics of this clause is similar to the semantics 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 a bool statement to implement compound queries. For more information about TairSearch, see Search.

Sample code

  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.

    Example 1: SELECT A = a and B = b

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

    Example 2: SELECT A = a or B = b

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

    Example 3: SELECT A = a and B != b

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

    Example 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" }  }
                ]
            }
        }
    }'
  • On this page (1, T)
  • Syntax
  • Sample code
Feedback
phone Contact Us

Chat now with Alibaba Cloud Customer Service to assist you in finding the right products and services to meet your needs.

alicare alicarealicarealicare