# Can I have only filter context query?

**URL:** https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047
**Category:** Elasticsearch
**Created:** [November 16, 2018, 11:50am UTC](https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047 "2018-11-16T11:50:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Nedko\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@Nedko\_Georgiev](https://discuss.elastic.co/u/Nedko_Georgiev)
#### Post date: [November 16, 2018, 11:50am UTC](https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047/1 "2018-11-16T11:50:47Z")

</div>

Can I create a query with only filter context in it, without any query context or do I always have to have some query context in addition to the filter?  
In addition I'd like it to be nested, but let me know if I can have filter without nested as well if combination of filter and nested is not possible.  
May be use filtered?  
The idea is that I don't need scoring, just a quick filter over the documents.

An example would for the query itself would be something like this:

```auto
{
  "query": {
    "nested": {
      "path": "properties",
      "filter": {
        "bool": {
          "must": [
            {
              "term": {
                "properties.key": "propertyName"
              }
            },
            {
              "term": {
                "properties.value": "propertyValue"
              }
            }
          ]
        }
      }
    }
  }
}

```

The above doesn't work, while the one below does work:

```auto
{
  "query": {
    "nested": {
      "path": "properties",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "properties.key": "propertyName"
              }
            },
            {
              "match": {
                "properties.value": "propertyValue"
              }
            }
          ]
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![byronvoorbach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/byronvoorbach/32/8283_2.png) [@byronvoorbach](https://discuss.elastic.co/u/byronvoorbach)
#### Post date: [November 16, 2018, 12:47pm UTC](https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047/2 "2018-11-16T12:47:02Z")

</div>

```
{
      "query": {
        "nested": {
          "path": "properties",
          "query": {
            "bool": {
              "filter": [
                {
                  "term": {
                    "properties.key": {
                      "value": "VALUE"
                    }
                  }
                },
                {
                  "term": {
                    "properties.value": {
                      "value": "VALUE"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }

```

Change MUST to FILTER and replace your match query with term filters. Keep in mind that it matters how your data is analyzed:

> The `term` query finds documents that contain the **exact** term specified in the inverted index.

[Link](https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-term-query.html)

---

<div class="post-metadata">

### Author: ![Nedko\_Georgiev](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@Nedko\_Georgiev](https://discuss.elastic.co/u/Nedko_Georgiev)
#### Post date: [November 20, 2018, 1:25pm UTC](https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047/3 "2018-11-20T13:25:45Z")

</div>

Thanks @byronvoorbach, that actually didn't work, but this is what worked for me:  
The example below includes a filter by nested fields and an additional field

```auto
{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "filter": [
                  {
                    "match": {
                      "properties.key": "someKey"
                    }
                  },
                  {
                    "match": {
                      "properties.value": "someValue"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "match": {
            "someField": "someFieldValue"
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![byronvoorbach](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/byronvoorbach/32/8283_2.png) [@byronvoorbach](https://discuss.elastic.co/u/byronvoorbach)
#### Post date: [November 20, 2018, 1:38pm UTC](https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047/4 "2018-11-20T13:38:27Z")

</div>

Cool! Nice that you managed to work it out 🙂

---

<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: [December 18, 2018, 1:38pm UTC](https://discuss.elastic.co/t/can-i-have-only-filter-context-query/157047/5 "2018-12-18T13:38:31Z")

</div>

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