# Should doc\['field\_name'\] remove duplicates?

**URL:** <https://discuss.elastic.co/t/should-doc-field-name-remove-duplicates/202333>\
**Category:** Elasticsearch\
**Created:** [October 4, 2019, 11:23am UTC](https://discuss.elastic.co/t/should-doc-field-name-remove-duplicates/202333 "2019-10-04T11:23:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gautesk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gautesk/32/55396_2.png) [@gautesk](https://discuss.elastic.co/u/gautesk)\
**Post date:** [October 4, 2019, 11:23am UTC](https://discuss.elastic.co/t/should-doc-field-name-remove-duplicates/202333/1 "2019-10-04T11:23:09Z")

</div>

I am running elasticsearch 6.3 and try to perform a terms aggregation with a script as source. In the script, I use doc values to access a list that contains duplicates. It seems like all duplicates are removed when I use doc['my\_field1'] to access the field. Is it supposed to be like that?

Snippet from my document:  
{  
"my\_field1": ["a", "a", "b", "b"],  
"my\_field2": ["c", "d", "e", "f"]  
}

Snippet of my code:

```
  "aggregations": {
    "terms_agg1": {
      "terms": {
        "script": {
          "source": "\n String returnString = '';\n for (int i = 0; i < doc['my_field1'].length; i++) { returnString += doc['my_field1'][i] + ';' + doc['my_field2'][i] + ')') } return returnString ",
          "lang": "painless"
        }
      }
    }
}

```

I expected that the entire content of my\_field1 and my\_field2 would be iterated in the script, but the former is read in as ["a", "b"] and hence my loop only iterates the lists two times.

As far as I can see, the doc\_values documentation does not say anything about removing duplicates. I understand that the aggregation will eventually remove duplicates, but that should not apply to the source of the script, should it?

Any help would be greatly appreciated!

---

<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:** [November 1, 2019, 11:23am UTC](https://discuss.elastic.co/t/should-doc-field-name-remove-duplicates/202333/2 "2019-11-01T11:23:13Z")

</div>

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