# How to get matched values from array without getting all the values in it, using a dynamic template without type nested?

**URL:** <https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823>\
**Category:** Elasticsearch\
**Created:** [April 26, 2020, 8:09am UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823 "2020-04-26T08:09:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkarthikraj](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@rkarthikraj](https://discuss.elastic.co/u/rkarthikraj)\
**Post date:** [April 26, 2020, 8:09am UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823/1 "2020-04-26T08:09:37Z")

</div>

I tried to search inside array of object using match query but I got all the values inside the array, because the array is not of type nested.

I am using dynamic template to create index.

Is there any way to search single object which matches the given criteria instead of getting all the values in the array? I had tried multiple methods using query but the issue is the template is not of type nested.

Kindly provide any insights regarding this or any workaround is there to get single object from array which matches.

I am not using type nested since it has certain limitations.

My document looks like this.

```auto
{
  "productpk_s": "PROD001",
  "product": {
    "productpk_s": "PROD001",
    "mfgname_s": "TEST MFG"
  },
  "salesorder": [
    {
      "salesorderpk_s": "SO001",
      "productpk_s": "PROD001",
      "vendorname_s": "Mitsubishi"
    },
    {
      "salesorderpk_s": "SO002",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    },
    {
      "salesorderpk_s": "SO003",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    },
    {
      "salesorderpk_s": "SO004",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    },
    {
      "salesorderpk_s": "SO005",
      "productpk_s": "PROD001",
      "vendorname_s": "Sony"
    }
  ]
}

```

Sample query I tried:

```auto
{
  "_source": [
    "product.productpk_s",
    "salesorder.salesorderpk_s",
    "salesorder.vendorname_s"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": { "salesorder.vendorname_s.keyword": "Mitsubishi" }
        }
      ]
    }
  }
}

```

I think I am doing something wrong.

Kindly suggest, how the search can be performed without type nested in dynamic template.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 26, 2020, 10:17am UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823/2 "2020-04-26T10:17:17Z")

</div>

What you are describing is exactly what nested documents are for so i am not aware of any way to do this using flat documents and get the benefits without the drawbacks.

---

<div class="post-metadata">

**Author:** ![psramkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/psramkumar/32/31393_2.png) [@psramkumar](https://discuss.elastic.co/u/psramkumar)\
**Post date:** [April 26, 2020, 12:44pm UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823/3 "2020-04-26T12:44:12Z")

</div>

Use `script_fields` source

```auto
{
  "size": 10,
  "query": {
    "match": {
      "acc_names": {
        "query": "network"
      }
    }
  },
  "script_fields": {
    "position": {
      "script": {
        "source": "def newarr=[];for(an in doc['name_arr'].values) if (an.toLowerCase().indexOf('search_text')!=-1) newarr.push(an); return newarr;"
      }
    },
    "other_fields": {
      "script": {
        "source": "doc['other_fields'].value"
      }
    },
    "one_more_field": {
      "script": {
        "source": "doc['one_more_field'].values"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![psramkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/psramkumar/32/31393_2.png) [@psramkumar](https://discuss.elastic.co/u/psramkumar)\
**Post date:** [April 26, 2020, 12:46pm UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823/4 "2020-04-26T12:46:51Z")

</div>

is there any reason you are not using the template  
`http://localhost:9200/_template`

---

<div class="post-metadata">

**Author:** ![rkarthikraj](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@rkarthikraj](https://discuss.elastic.co/u/rkarthikraj)\
**Post date:** [April 26, 2020, 4:41pm UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823/5 "2020-04-26T16:41:10Z")

</div>

No reason Ramkumar for not using \_template.

---

<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:** [May 24, 2020, 4:41pm UTC](https://discuss.elastic.co/t/how-to-get-matched-values-from-array-without-getting-all-the-values-in-it-using-a-dynamic-template-without-type-nested/229823/6 "2020-05-24T16:41:13Z")

</div>

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