# Subset in an array

**URL:** <https://discuss.elastic.co/t/subset-in-an-array/237459>\
**Category:** Elasticsearch\
**Created:** [June 17, 2020, 11:11am UTC](https://discuss.elastic.co/t/subset-in-an-array/237459 "2020-06-17T11:11:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![surendra\_gandham](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surendra_gandham/32/70530_2.png) [@surendra\_gandham](https://discuss.elastic.co/u/surendra_gandham)\
**Post date:** [June 17, 2020, 11:11am UTC](https://discuss.elastic.co/t/subset-in-an-array/237459/1 "2020-06-17T11:11:01Z")

</div>

Hi everyone,

Thanks for reading.

Here's my problem. I have an array with numbers on each document as such:

Document 1: [1, 2, 3]  
Document 2: [1, 4]  
Document 3: [6,12,2]

I want to query my documents and find those documents that has subsets of my array: [1, 2, 3, 5, 10]. In this case only document 1 should be matched.

I don't want to run a script like: [Subsets](https://discuss.elastic.co/t/subsets/46488/3)

Thanks again for reading - I hope you have some inputs.

---

<div class="post-metadata">

**Author:** ![jessepeixoto](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jessepeixoto](https://discuss.elastic.co/u/jessepeixoto)\
**Post date:** [June 18, 2020, 3:18am UTC](https://discuss.elastic.co/t/subset-in-an-array/237459/2 "2020-06-18T03:18:16Z")

</div>

Hi @surendra_gandham,

I think it would be interesting for this situation to try the term\_set query.

You would have to add a field with the total of items in the array of each document and then use the term\_set query along with this field. [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html)

It would be something like the example below:

```auto
PUT my_index/_doc/1
{
  "values": [1, 2, 3],
  "itemsCount": 3
}

PUT my_index/_doc/2
{
  "values": [1, 4],
  "itemsCount": 2
}

PUT my_index/_doc/3
{
  "values": [6, 12, 2],
   "itemsCount": 3
}

```

```auto
GET my_index/_search
{
  "query": {
    "terms_set": {
      "values": {
        "terms": [1, 2, 3, 5, 10],
        "minimum_should_match_field": "itemsCount"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![surendra\_gandham](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/surendra_gandham/32/70530_2.png) [@surendra\_gandham](https://discuss.elastic.co/u/surendra_gandham)\
**Post date:** [June 18, 2020, 6:54am UTC](https://discuss.elastic.co/t/subset-in-an-array/237459/3 "2020-06-18T06:54:40Z")

</div>

Thanks, @jessepeixoto.  
I agree with you, but I am going with Minimum should match script for now. Meaning on the go I am going to calculate the length of existing array field.

---

<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:** [July 16, 2020, 6:54am UTC](https://discuss.elastic.co/t/subset-in-an-array/237459/4 "2020-07-16T06:54:46Z")

</div>

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