# How do I create a boolean "OR" filter?

**URL:** <https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281>\
**Category:** Elasticsearch\
**Created:** [August 23, 2021, 10:59pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281 "2021-08-23T22:59:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![elastic\_yogi](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elastic\_yogi](https://discuss.elastic.co/u/elastic_yogi)\
**Post date:** [August 23, 2021, 10:59pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/1 "2021-08-23T22:59:52Z")

</div>

I'd like to filter for EITHER (OR) of two values, but a `filter` appears to require (AND) a document to contain EACH value. I'm using a `bool` query currently, and `should` appears to be the only way to do an OR expression. However, I do not want relevance scores and I want node query caching, which is supported by `filter context` (enabled by `filter`) not `query context` (enabled by `should`).

The only ways to get `filter context` are described here in the documentation:

> Filter context is in effect whenever a query clause is passed to a `filter` parameter, such as the `filter` or `must_not` parameters in the [`bool`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) query, the `filter` parameter in the [`constant_score`](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) query, or the [`filter`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html) aggregation. ([Query and filter context | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html))

As you can see, the `filter` parameter is required.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 23, 2021, 11:17pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/2 "2021-08-23T23:17:02Z")

</div>

You can use the `should` clause to create and OR filter

Here is a complete example

```auto
DELETE discuss-test

PUT discuss-test
{
  "mappings": {
    "properties": {
      "type": {
        "type": "keyword"
      },
      "gender": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}

POST discuss-test/_doc
{
  "type" : "feline",
  "gender" : "male",
  "name" : "chuck"
}

POST discuss-test/_doc
{
  "type" : "feline",
  "gender" : "female",
  "name" : "mary"
}

POST discuss-test/_doc
{
  "type" : "canis",
  "gender" : "male",
  "name" : "spot"
}

POST discuss-test/_doc
{
  "type" : "canis",
  "gender" : "female",
  "name" : "mary"
}

```

Now the query

```auto

# This is the equivalent of an OR filter with should 
GET discuss-test/_search
{
  "query": {
    "bool": {
      "should": [ <! -- This mean OR, must means AND
        {
          "term": {
            "type": {
              "value": "canis"
            }
          }
        },
        {
                  "term": {
            "name": {
              "value": "mary"
            }
          }
        }
      ]
    }
  }
}

```

and the resulting OR.

```auto

{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.3862942,
    "hits" : [
      {
        "_index" : "discuss-test",
        "_type" : "_doc",
        "_id" : "U6tJdXsBm1AJFmUV717P",
        "_score" : 1.3862942,
        "_source" : {
          "type" : "canis",
          "gender" : "female",
          "name" : "mary"
        }
      },
      {
        "_index" : "discuss-test",
        "_type" : "_doc",
        "_id" : "UatJdXsBm1AJFmUV716l",
        "_score" : 0.6931471,
        "_source" : {
          "type" : "feline",
          "gender" : "female",
          "name" : "mary"
        }
      },
      {
        "_index" : "discuss-test",
        "_type" : "_doc",
        "_id" : "UqtJdXsBm1AJFmUV7162",
        "_score" : 0.6931471,
        "_source" : {
          "type" : "canis",
          "gender" : "male",
          "name" : "spot"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![elastic\_yogi](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elastic\_yogi](https://discuss.elastic.co/u/elastic_yogi)\
**Post date:** [August 23, 2021, 11:18pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/3 "2021-08-23T23:18:57Z")

</div>

This introduces scoring however.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 23, 2021, 11:55pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/4 "2021-08-23T23:55:07Z")

</div>

Yup sorry I read wrong, just saw you did not want scoring...

Took me a few minutes how about this...  
Wrap the bool should inside the filter...

```auto
GET discuss-test/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "type": "canis"
                }
              },
              {
                "term": {
                  "name": "mary"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

Result is

```auto

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "discuss-test",
        "_type" : "_doc",
        "_id" : "UatJdXsBm1AJFmUV716l",
        "_score" : 0.0,
        "_source" : {
          "type" : "feline",
          "gender" : "female",
          "name" : "mary"
        }
      },
      {
        "_index" : "discuss-test",
        "_type" : "_doc",
        "_id" : "UqtJdXsBm1AJFmUV7162",
        "_score" : 0.0,
        "_source" : {
          "type" : "canis",
          "gender" : "male",
          "name" : "spot"
        }
      },
      {
        "_index" : "discuss-test",
        "_type" : "_doc",
        "_id" : "U6tJdXsBm1AJFmUV717P",
        "_score" : 0.0,
        "_source" : {
          "type" : "canis",
          "gender" : "female",
          "name" : "mary"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![elastic\_yogi](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elastic\_yogi](https://discuss.elastic.co/u/elastic_yogi)\
**Post date:** [August 24, 2021, 12:17am UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/5 "2021-08-24T00:17:08Z")

</div>

Perfect. That removes the scoring. I just hope it's not calculating the scores underneath still and then just dropping them. Do you know this by any chance?

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [August 24, 2021, 12:18am UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/6 "2021-08-24T00:18:46Z")

</div>

Run it in the Query Profiler .. looks like constant score to me.

 ![Screen Shot 2021-08-23 at 5.18.00 PM](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7efb04bfde7bfafc2369f6090e1e44eb6233b34b.png)

No it does not look like is is actually scoring to me.

If you look at the details, there is no time on the actual score.

 ![Screen Shot 2021-08-23 at 5.31.13 PM](https://us1.discourse-cdn.com/elastic/original/3X/9/d/9d67245dcb143220bb3a67150411379b2339e36c.png)

You can even try a constant\_score query ... there is less time spent in the filter than the that

```auto
GET discuss-test/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": { "type": "canis" }
      },
      "boost": 1.2
    }
  }
}

```

Take a look...

---

<div class="post-metadata">

**Author:** ![elastic\_yogi](https://avatars.discourse-cdn.com/v4/letter/e/71c47a/32.png) [@elastic\_yogi](https://discuss.elastic.co/u/elastic_yogi)\
**Post date:** [August 24, 2021, 1:16am UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/7 "2021-08-24T01:16:40Z")

</div>

Wow, that's great. Thank you.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 21, 2021, 1:17am UTC](https://discuss.elastic.co/t/how-do-i-create-a-boolean-or-filter/282281/8 "2021-09-21T01:17:01Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
