Adding Boolean Logic to Queries

Introduction to Compound Queries

Leaf Query Performs basic Operation. Example of leaf query is Terms query or match query

Compound Query Wrap Leaf query or other compound query.


Querying with Boolean Logic

  • bool queries are used to add boolean logic to your queries
GET /recipe/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "ingredients.name": "parmesan"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "ingredients.name": "tuna"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "preparation_time_minutes": {
              "gte": 10,
              "lte": 15
            }
          }
        }
      ]
    }
  }
}
  • should will used to boost the relevant score of the search results in the query context
GET /recipe/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "ingredients.name": "parmesan"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "ingredients.name": "tuna"
          }
        }
      ],
      "should": [
        {
          "match": {
            "ingredients.name": "parsley"
          }
        }
      ], 
      "filter": [
        {
          "range": {
            "preparation_time_minutes": {
              "gte": 10,
              "lte": 15
            }
          }
        }
      ]
    }
  }
}

Debugging bool queries with named queries

GET /recipe/_search
  {
    "query": {
      "bool": {
        "must": [
          {
            "match": {
              "ingredients.name": {
                "query": "parmesan",
                "_name": "parmesan_must"
              }
            }
          }
        ],
        "must_not": [
          {
            "match": {
              "ingredients.name": {
                "query": "tuna",
                "_name": "tuna_must_not"
              }
            }
          }
        ],
        "should": [
          {
            "match": {
              "ingredients.name": {
                "query": "parsley",
                "_name": "parsley_should"
              }
            }
          }
        ], 
        "filter": [
          {
            "range": {
              "preparation_time_minutes": {
                "gte": 10,
                "lte": 15,
                "_name": "prep_time_filter"
              }
            }
          }
        ]
      }
    }
  }

How the “match” query works

When you use the match query, it analyzes the search query and converts match query to term queries during analysis process. When the boolean operator is set to and it will translate to must whereas when set to or it translates to should query during analysis phase.

GET /recipe/_search
{
  "query": {
    "match": {
      "title": "pasta Carbonara"
    }
  }
}
GET /recipe/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "title": "pasta"
          }
        }
      ,
      {
       "term": {
         "title": "carbonara"
       } 
      }
      ]
    }
  }
}