All Products
Search
Document Center

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

Last Updated:Mar 28, 2026

TairSearch is an in-house data structure for full-text search that uses Elasticsearch-compatible syntax. This topic explains how the bool query works and demonstrates common compound query patterns using must, must_not, and should clauses.

How it works

A bool query combines multiple conditions using clause types, each with distinct logical behavior:

ClauseLogic equivalentBehavior
mustANDAll conditions must match. Documents that also match should clauses receive a higher relevance score, which affects their ranking in results.
must_notNOTDocuments matching any of these conditions are excluded from results.
shouldORConditions are optional. When should is the only clause type, minimum_should_match defaults to 1 (at least one must match). When combined with must or must_not, minimum_should_match defaults to 0 (should clauses become optional score boosters).

Clause evaluation follows this priority order: must_not > must > should.

Bool queries, as well as any other query types, can be nested inside a bool query to express complex logic such as (A AND B) OR C.

TairSearch_bool

For the full TairSearch command reference, see Search.

Run compound queries

The following examples demonstrate four common bool query patterns. All examples use the same index and document data.

Step 1: Create an index

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

Step 2: Add a document

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

Step 3: Query with bool logic

Example 1: A = a AND B = b

Use must with multiple term conditions to require all fields to match.

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

Example 2: A = a OR B = b

Use should to match documents where at least one condition is true.

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

Example 3: A = a AND B != b

Combine must and must_not to include documents that match one field and exclude documents that match another.

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

Example 4: (A = a AND B = b) OR C = c

Nest a bool query inside a should clause to express compound logic. The inner bool handles the AND condition; the outer should adds the OR branch.

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