# Terms\_set query does not work as expected within nested context

**URL:** <https://discuss.elastic.co/t/terms-set-query-does-not-work-as-expected-within-nested-context/380430>\
**Category:** Elasticsearch\
**Created:** [July 24, 2025, 2:06pm UTC](https://discuss.elastic.co/t/terms-set-query-does-not-work-as-expected-within-nested-context/380430 "2025-07-24T14:06:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [July 24, 2025, 2:06pm UTC](https://discuss.elastic.co/t/terms-set-query-does-not-work-as-expected-within-nested-context/380430/1 "2025-07-24T14:06:33Z")

</div>

Hey,

I am testing a use-case for a `terms_set` query, because I need minimum\_should\_match in this context. I noticed that it does not work as expected in combination within a nested scope, and just want to verify. Take the following reproducible example:

```auto
PUT alr_test
{
  "mappings": {
    "properties": {
      "data": {
        "type": "keyword"
      },
      "nested": {
        "type": "nested",
        "properties": {
          "data": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

PUT alr_test/_doc/1?refresh
{
  "data" : ["a", "b", "c"],
  "nested" : [{"data" :"a"} , {"data" :"b"}, {"data" :"c"}]
}

# works
GET alr_test/_search
{
  "query": {
    "terms_set": {
      "data": {
        "minimum_should_match": 2,
         "terms": ["a", "b", "c"]
      }
    }
  }
}

# works
GET alr_test/_search
{
  "query": {
    "nested": {
      "path": "nested",
      "query": {
        "terms_set" : {
          "nested.data" : {
            "minimum_should_match" : 1,
            "terms": ["a", "b", "c"]
          }
        }
      }
    }
  }
}

# does not work
GET alr_test/_search
{
  "query": {
    "nested": {
      "path": "nested",
      "query": {
        "terms_set" : {
          "nested.data" : {
            "minimum_should_match" : 2,
            "terms": ["a", "b", "c"]
          }
        }
      }
    }
  }
}

```

My assumption here is, that within the nested scope, each found sub document within the array is treated as an own document.

In order to have the right behaviour I'd need to write a bool query, with a should clause and three term queries that each have a nested scope.

I'd guess this is a feature and not a bug, but would like to verify.

This is on 8.18.3

--Alex

---

<div class="post-metadata">

**Author:** ![john-wagster](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john-wagster/32/139021_2.png) [@john-wagster](https://discuss.elastic.co/u/john-wagster)\
**Post date:** [July 25, 2025, 7:39pm UTC](https://discuss.elastic.co/t/terms-set-query-does-not-work-as-expected-within-nested-context/380430/2 "2025-07-25T19:39:29Z")

</div>

I can confirm this looks like a bug (or minimally missing functionality) to me. I was able to reproduce and I can't think of a good reason this shouldn't work. I took a brief look at the set of known issues around this feature as well and didn't see anything right off.

Do you mind creating an issue and linking it here. I'm happy to help make sure it's routed appropriately. Can't promise when we'll get to it, but really appreciate you calling it out.

> **[elastic/elasticsearch](https://github.com/elastic/elasticsearch/issues)**
>
> Free and Open Source, Distributed, RESTful Search Engine - elastic/elasticsearch
