# How to perform filter with condition?

**URL:** https://discuss.elastic.co/t/how-to-perform-filter-with-condition/366295
**Category:** Elasticsearch
**Tags:** eql-elastic-query-language
**Created:** [September 10, 2024, 8:19am UTC](https://discuss.elastic.co/t/how-to-perform-filter-with-condition/366295 "2024-09-10T08:19:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![\_Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_zhang/32/137117_2.png) [@\_Zhang](https://discuss.elastic.co/u/_Zhang)
#### Post date: [September 10, 2024, 8:19am UTC](https://discuss.elastic.co/t/how-to-perform-filter-with-condition/366295/1 "2024-09-10T08:19:01Z")

</div>

In SQL:

```auto
select * from my_index where type in ('a', 'b') and (type <> 'a' or value_field = 'sth')

```

Filter by field `value_field` when `type` is 'a'. Using script:

```auto
{
    "query": {
        "bool": {
            "filter": [
                {
                  "terms": {
                    "type": [
                      "a",
                      "b"
                    ]
                  }
                },
                {
                  "script": {
                    "script": {
                      "source": "return doc['type'].value != 'a' || doc['value_field'].value == params['val']",
                      "params": {
                        "val": "sth"
                      }
                    }
                  }
                }
            ]
    }
}

```

Any other way to do this without script?

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [September 10, 2024, 6:37pm UTC](https://discuss.elastic.co/t/how-to-perform-filter-with-condition/366295/2 "2024-09-10T18:37:43Z")

</div>

Hi @_Zhang

Try this:

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "type": ["a", "b"]
          }
        }
      ],
      "should": [
        {
          "bool": {
            "must_not": {
              "term": {
                "type": "a"
              }
            }
          },
          {
            "term": {
              "value_field": "sth"
            }
          }
        ],
      "minimum_should_match": 1
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![\_Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/_zhang/32/137117_2.png) [@\_Zhang](https://discuss.elastic.co/u/_Zhang)
#### Post date: [September 11, 2024, 2:13am UTC](https://discuss.elastic.co/t/how-to-perform-filter-with-condition/366295/3 "2024-09-11T02:13:29Z")

</div>

Hello, @RabBit_BR . Should I put the `should` clause into `filter` as there are other `should` clauses? A nested `bool` query like this: `{ query: { bool: { filter: { bool: [{ filter: {...} }, should: {...}] } } } }`
