# FilteredQuery slower than query and filter?

**URL:** https://discuss.elastic.co/t/filteredquery-slower-than-query-and-filter/1105
**Category:** Elasticsearch
**Created:** [May 21, 2015, 3:15pm UTC](https://discuss.elastic.co/t/filteredquery-slower-than-query-and-filter/1105 "2015-05-21T15:15:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JUAN\_JOSE\_SANCHEZ](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@JUAN\_JOSE\_SANCHEZ](https://discuss.elastic.co/u/JUAN_JOSE_SANCHEZ)
#### Post date: [May 21, 2015, 3:15pm UTC](https://discuss.elastic.co/t/filteredquery-slower-than-query-and-filter/1105/1 "2015-05-21T15:15:27Z")

</div>

Hi people,

I have executed this query into production, it tooks 2 seconds:

```auto
CURL -XGET 'http://blablabla/_search?routing=061500000015181033' -d '
{
  "size": 0,
  "query": {
    "filtered": {
      "query": {
        "term": {
          "blabla": "035345353"
        }
      },
      "filter": {
        "range": {
          "fecha": {
            "from": "20130101",
            "to": "20150501",
            "include_lower": true,
            "include_upper": true
          }
        }
      }
    }
  },
  "aggs": {
    "Sectores": {
      "terms": {
        "field": "descripcionSector"
      },
      "aggs": {
        "sumaSector": {
          "sum": {
            "field": "importe"
          }
        },
        "Subsectores": {
          "terms": {
            "field": "descripcionSubsector"
          },
          "aggs": {
            "sumaSubSector": {
              "sum": {
                "field": "importe"
              }
            }
          }
        }
      }
    }
  }
}'
```

and this one only took 159 ms

```auto
curl -XGET 'http://blablabla/_search?routing=061500000015181033' -d '
{
  "size": 0,
  "query": {
    "term": {
      "blala": "061500000015181033"
    }
  },
  "filter": {
    "range": {
      "fecha": {
        "gte": 20130101
      }
    }
  },
  "aggs": {
    "Sectores": {
      "terms": {
        "field": "descripcionSector"
      },
      "aggs": {
        "sumaSector": {
          "sum": {
            "field": "importe"
          }
        },
        "Subsectores": {
          "terms": {
            "field": "descripcionSubsector"
          },
          "aggs": {
            "sumaSubSector": {
              "sum": {
                "field": "importe"
              }
            }
          }
        }
      }
    }
  }
}'
```

Server was attending same level of requests at the time of executing, no overload...so why there's so much time difference? I guess caching is involved, but I thought filteredQuery was faster...

Does anyone can help me to understand this?

Regards

---

<div class="post-metadata">

### Author: ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)
#### Post date: [May 21, 2015, 3:20pm UTC](https://discuss.elastic.co/t/filteredquery-slower-than-query-and-filter/1105/2 "2015-05-21T15:20:49Z")

</div>

this to querys don't give you the same results, if you use filtered your aggregation and hits will be filter by (query and filter) in the second request, the hits be filter by your (filter + query) BUT the aggregatios will be use only your (query)!

@kimchy in this post explains the difference : [What is the difference between filtered query and regular query with filter?](https://discuss.elastic.co/t/what-is-the-difference-between-filtered-query-and-regular-query-with-filter/413)

---

<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 6, 2017, 12:12am UTC](https://discuss.elastic.co/t/filteredquery-slower-than-query-and-filter/1105/3 "2017-07-06T00:12:37Z")

</div>


