# Filter context within query context

**URL:** <https://discuss.elastic.co/t/filter-context-within-query-context/156171>\
**Category:** Elasticsearch\
**Created:** [November 10, 2018, 11:11pm UTC](https://discuss.elastic.co/t/filter-context-within-query-context/156171 "2018-11-10T23:11:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gschneel](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gschneel](https://discuss.elastic.co/u/gschneel)\
**Post date:** [November 10, 2018, 11:11pm UTC](https://discuss.elastic.co/t/filter-context-within-query-context/156171/1 "2018-11-10T23:11:47Z")

</div>

I have an index containing documents which have the `object_type` field set to `comment` or `post`. For `comment` I want to search only the `text` field, for `post` I want to search both `text` and `post_title`. Essentially I'd like to query something like this: `(doc.object_type = "post" and (doc.text matching text or doc.post_title matching text)) or (doc.object_type = "comment" and doc.text matching text)`

When I run the query below I'm seeing `post`s come up in the results despite the `comment` filter. Can someone explain what the inner filter clause does here? Is it possible to nest a `filter` inside a `should`?

```
{
  "from": 0,
  "size": 5,
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "channel_name": "jeanne4"
              }
            }
          ]
        }
      },
      "should": [
        {
          "bool": {
            "filter": {
              "term": {
                "object_type": "comment"
              }
            },
            "must": {
              "match": {
                "text": "amtrak"
              }
            }
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Jaspreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaspreet_singh/32/58296_2.png) [@Jaspreet\_Singh](https://discuss.elastic.co/u/Jaspreet_Singh)\
**Post date:** [November 12, 2018, 6:23pm UTC](https://discuss.elastic.co/t/filter-context-within-query-context/156171/2 "2018-11-12T18:23:47Z")

</div>

> [@gschneel](#):
>
> "from": 0, "size": 5, "query": { "bool": { "filter": { "bool": { "should": [{ "term": { "channel\_name": "jeanne4" } }] } }, "should": [{ "bool": { "filter": { "term": { "object\_type": "comment" } }, "must": { "match": { "text": "amtrak" } } } }] } } }

What is the purpose of this filter?

```auto
 "filter": {
                "bool": {
                    "should": [
                        {
                            "term": {
                                "channel_name": "jeanne4"
                            }
                        }
                    ]
                }
            }

```

Cos this is basically bringing in anything that is in channel\_name = "jeanne4" including comments, posts etc. You are getting a union of the filter and should clauses. Your post does not mention anything about channels so it is hard to tell what you are trying to do. Perhaps you are limiting all searching to a particular channel, in which case this should come in a must clause.  
Just this part should get you only comments that have the text "amtrack".

```auto
{
    "bool": {
        "filter": {
            "term": {
                "object_type": "comment"
            }
        },
        "must": {
            "match": {
                "text": "amtrak"
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![gschneel](https://avatars.discourse-cdn.com/v4/letter/g/85e7bf/32.png) [@gschneel](https://discuss.elastic.co/u/gschneel)\
**Post date:** [November 13, 2018, 2:33pm UTC](https://discuss.elastic.co/t/filter-context-within-query-context/156171/3 "2018-11-13T14:33:17Z")

</div>

Hi Jaspreet, sorry I was not more detailed in my post. My intention was to have the filter on `channel_name` be an intersection, not a union. That looks like the root of my problem, thanks for your help.

---

<div class="post-metadata">

**Author:** ![Jaspreet\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jaspreet_singh/32/58296_2.png) [@Jaspreet\_Singh](https://discuss.elastic.co/u/Jaspreet_Singh)\
**Post date:** [November 13, 2018, 3:49pm UTC](https://discuss.elastic.co/t/filter-context-within-query-context/156171/4 "2018-11-13T15:49:46Z")

</div>

Yea sure - no worries. This can be very tricky. Let me know if you have further questions.

---

<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 11, 2018, 3:50pm UTC](https://discuss.elastic.co/t/filter-context-within-query-context/156171/5 "2018-12-11T15:50:01Z")

</div>

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