# First filter then run nested query

**URL:** https://discuss.elastic.co/t/first-filter-then-run-nested-query/309905
**Category:** Elasticsearch
**Created:** [July 18, 2022, 1:41pm UTC](https://discuss.elastic.co/t/first-filter-then-run-nested-query/309905 "2022-07-18T13:41:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Michal\_Bogusz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michal_bogusz/32/101718_2.png) [@Michal\_Bogusz](https://discuss.elastic.co/u/Michal_Bogusz)
#### Post date: [July 18, 2022, 1:41pm UTC](https://discuss.elastic.co/t/first-filter-then-run-nested-query/309905/1 "2022-07-18T13:41:10Z")

</div>

I have document with mapping:

```auto
{
    "properties": {
        "title": {"type": "text"},
        "added": {"type": "date"},
        "pdf_url": {"type": "text"},
        "blocks": {"type": "nested"}
    }
}

```

I plan to have a lot of documents. So I want to firstly filter documents for example with `"added"` field and then run query on my `"nested"` field.

This is my attempt to do it:

```auto
"query": {
    "filter": {
        "range": {
            "added": {"gte": "2015-01-01"}
        }
    },
    "nested": {
        "path": "blocks",
        "query": {
            "match": {
                "blocks.block_text": {
                    "query": "my query",
                    "fuzziness": "auto"
                },
            }
        },
        "inner_hits": {
            "highlight": {
                "fields": {
                    "blocks.block_text": {}
                }
            }
        }
    }
}

```

And I get `elasticsearch.BadRequestError: BadRequestError(400, 'parsing_exception', 'unknown query [filter]')`

I tried also wrap everything into `"bool"` but then I get similar error with `"nested"`.

I kinda can achieve my result with this query. But firstly I am afraid that with large dataset it won't be efficient. Secondly I don't want filtered value having impact on score

```auto
"query": {
    "bool": {
        "must": [
            {
                "match": {
                    "title": {
                        "query": "title"
                    }
                }
            },
            {
                "nested": {
                    "path": "blocks",
                    "query": {
                        "match": {
                            "blocks.block_text": {
                                "query": "my qury",
                                "fuzziness": "auto"
                            },
                        }
                    },
                    "inner_hits": {
                        "highlight": {
                            "fields": {
                                "blocks.block_text": {}
                            }
                        }
                    }
                }
            }
        ]
    }
}

```

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [July 18, 2022, 1:48pm UTC](https://discuss.elastic.co/t/first-filter-then-run-nested-query/309905/2 "2022-07-18T13:48:44Z")

</div>

Hi @Michal_Bogusz

Add in your bool query the filter query.

```auto
 "bool": {
      "filter": [
        {
          "range": {
            "added": {
              "gte": "2015-01-01"
            }
          }
        }
      ],
     "must": [
        ....
      ]

```

---

<div class="post-metadata">

### Author: ![Michal\_Bogusz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michal_bogusz/32/101718_2.png) [@Michal\_Bogusz](https://discuss.elastic.co/u/Michal_Bogusz)
#### Post date: [July 18, 2022, 1:51pm UTC](https://discuss.elastic.co/t/first-filter-then-run-nested-query/309905/3 "2022-07-18T13:51:03Z")

</div>

Works like charm, thank you

---

<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: [August 15, 2022, 1:51pm UTC](https://discuss.elastic.co/t/first-filter-then-run-nested-query/309905/4 "2022-08-15T13:51:46Z")

</div>

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