# Convert Query-Context into Filter-Context

**URL:** https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428
**Category:** Elasticsearch
**Created:** [January 3, 2017, 12:21pm UTC](https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428 "2017-01-03T12:21:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mos/32/4898_2.png) [@mos](https://discuss.elastic.co/u/mos)
#### Post date: [January 3, 2017, 12:21pm UTC](https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428/1 "2017-01-03T12:21:15Z")

</div>

Using ES 5.1.1 - imagine the following simple query:

```
  GET /bank/_search
    {
      "query": {
        "match": {
          "firstname": "Amber"
        }
      }
    }

```

This query shouldn't be executed as a query, but as a filter (speeding up things). I tried something like

```
GET /bank/_search
{
  "query": { 
    "bool": { 
     "filter": [ 
        { "term": { "firstname": "Amber" }}
      ]
    }
  }
}

```

But this doesn't work and returns 0 results. Using ES 2.2 this worked with the old "filtered" keyword (instead of "bool"). But that's not allowed in 5.x ([https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-filtered-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-filtered-query.html))

What is the simplest syntax to convert the query above into a filter-search?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 3, 2017, 12:26pm UTC](https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428/2 "2017-01-03T12:26:01Z")

</div>

`term` queries do not use an Analyzer to process the text whereas `match` queries do.  
So Amber won't be lower-cased (or whatever your analysis pipeline normally does)

Simply put the `match` query inside the bool/filter expression and not the `term` query.

---

<div class="post-metadata">

### Author: ![mos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mos/32/4898_2.png) [@mos](https://discuss.elastic.co/u/mos)
#### Post date: [January 3, 2017, 12:35pm UTC](https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428/3 "2017-01-03T12:35:27Z")

</div>

Thanks, Mark.You are right!

Still wondering why a syntax like this

```
GET /bank/_search
{
  "query": { 
    "bool": { 
     "filter":  
        { "match": { "firstname": "Amber" }}
    }
  }
}

```

is required and not something like

```
GET /bank/_search
    {
      "filter": {
        "match": {
          "firstname": "Amber"
        }
      }
    }

```

Why need a filter always be executed inside a "query-context"?  
Otherwise it would be much more straightforward. Any ideas for this design decision?

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [January 3, 2017, 12:48pm UTC](https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428/4 "2017-01-03T12:48:59Z")

</div>

> [@mos](#):
>
> Why need a filter always be executed inside a "query-context"?

The root `query` is just the place-marker for where we keep all the document selection criteria. Frequently criteria is complex and requires multiple levels of nested booleans and filters /queries. Having a single root to this tree structure can help unfold the various parts of the logic piece by piece in my experience but I can see for the simple use case it might seem verbose.

---

<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: [January 31, 2017, 12:49pm UTC](https://discuss.elastic.co/t/convert-query-context-into-filter-context/70428/5 "2017-01-31T12:49:16Z")

</div>

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