# KNN search on filtered nested document

**URL:** <https://discuss.elastic.co/t/knn-search-on-filtered-nested-document/386096>\
**Category:** Elastic Search\
**Created:** [April 29, 2026, 1:58pm UTC](https://discuss.elastic.co/t/knn-search-on-filtered-nested-document/386096 "2026-04-29T13:58:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renjith\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/renjith_r/32/147465_2.png) [@Renjith\_R](https://discuss.elastic.co/u/Renjith_R)\
**Post date:** [April 29, 2026, 1:58pm UTC](https://discuss.elastic.co/t/knn-search-on-filtered-nested-document/386096/1 "2026-04-29T13:58:59Z")

</div>

Document in my index represents various books. All the paragraphs of a book are stored under nested document "texts"

```json
"texts": {
        "type": "nested",
        "properties": {
          "part": {
            "type": "keyword"
          },
          "text": {
            "type": "text"
          },
          "text_vector": {
            "type": "dense_vector",
            "element_type": "float",
            "dims": 384,
            "index": true,
            "similarity": "cosine"
          },

```

part : represents a section title

text: the text under that section

text\_vector: vector representation of text

I want to pre filter/filterout nested documents based on "part" metadata and perform knn search on them. The inclusion works perfectly fine using the query below

```json
"knn": [
    {
      "field": "texts.text_vector",
      "query_vector": [
        -0.00758091127499938,
        -0.06187092140316963,
        -0.056799083948135376,
        ........
      ],
      "k": 10,
      "num_candidates": 500,
      "filter": [
        {
          "bool": {
            "must": [
              {
                "match_phrase": {
                  "texts.part": {
                    "query": "Part1"
                  }
                }
              }
            ]
          }
        }
      ],
      "inner_hits": {
        "size": 10
      }
    }
  ]

```

but exclusion won't work

```json
"knn": [
    {
      "field": "texts.text_vector",
      "query_vector": [
        -0.00758091127499938,
        -0.06187092140316963,
        -0.056799083948135376,
        ........
      ],
      "k": 10,
      "num_candidates": 500,
      "filter": [
        {
          "bool": {
            "must_not": [
              {
                "match_phrase": {
                  "texts.part": {
                    "query": "Part1"
                  }
                }
              }
            ]
          }
        }
      ],
      "inner_hits": {
        "size": 10
      }
    }
  ]

```

My reference: [kNN search in Elasticsearch | Elastic Docs](https://www.elastic.co/docs/solutions/search/vector/knn?utm_source=chatgpt.com#nested-knn-search-filtering-nested-metatadata)

Tried term, match etc.. instead of match\_phrase. None of them worked.  
Can anyone help?
