# Nesting \`and\` filter affects performance?

**URL:** https://discuss.elastic.co/t/nesting-and-filter-affects-performance/44512
**Category:** Elasticsearch
**Created:** [March 16, 2016, 5:48am UTC](https://discuss.elastic.co/t/nesting-and-filter-affects-performance/44512 "2016-03-16T05:48:30Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![joshma](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joshma/32/8500_2.png) [@joshma](https://discuss.elastic.co/u/joshma)
#### Post date: [March 16, 2016, 5:48am UTC](https://discuss.elastic.co/t/nesting-and-filter-affects-performance/44512/1 "2016-03-16T05:48:30Z")

</div>

I have two queries, where one takes about 100ms and the other times out after 24s. The only difference is that the first (SLOW) is roughly

```auto
{"and": [FILTER_A, {"and": [FILTER_B, SCRIPT_FILTER_C]}]}

```

and the second (FAST) is

```auto
{"and": [FILTER_A, {"and": [FILTER_B]}, SCRIPT_FILTER_C]}

```

(Fuller versions below.) `SCRIPT_FILTER_C` is expensive, so it should be evaluated last. We're generating these filters to some degree, which is why there's awkward nesting of `and` filters.

My questions: 1) Why does this happen? Does the "and" filter with one clause lead to some weird ordering or inability to cache? 2) Is there any way for me to go and diagnose this myself?

Thanks!

SLOW:

```auto
{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "folder_ids": [2756]
            }
          },
          {
            "and": [
              {
                "query": {
                  "match": {
                    "bases.substring_ngram_match": {
                      "minimum_should_match": "100%",
                      "query": "GAAATTTGTGATGCTATTGCCCTCGTGCGCTCTCCTGTTC",
                      "analyzer": "sequence_substring_ngram_analyzer"
                    }
                  }
                }
              },
              {
                "script": {
                  "lang": "groovy",
                  "params": {
                    ...some_script_params...
                  },
                  "script_file": "source_regex"
                }
              }
            ]
          }
        ]
      }
    }
  }
}

```

FAST:

```auto
{
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "folder_ids": [2756]
            }
          },
          {
            "and": [
              {
                "query": {
                  "match": {
                    "bases.substring_ngram_match": {
                      "minimum_should_match": "100%",
                      "query": "GAAATTTGTGATGCTATTGCCCTCGTGCGCTCTCCTGTTC",
                      "analyzer": "sequence_substring_ngram_analyzer"
                    }
                  }
                }
              }
            ]
          },
          {
            "script": {
              "lang": "groovy",
              "params": {
                ...some_script_params...
              },
              "script_file": "source_regex"
            }
          }
        ]
      }
    }
  }
}

```

---

<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: [July 5, 2017, 11:08pm UTC](https://discuss.elastic.co/t/nesting-and-filter-affects-performance/44512/2 "2017-07-05T23:08:00Z")

</div>


