# How to avoid duplicate values being copied while using copy\_to?

**URL:** <https://discuss.elastic.co/t/how-to-avoid-duplicate-values-being-copied-while-using-copy-to/330905>\
**Category:** Elasticsearch\
**Created:** [April 27, 2023, 6:25am UTC](https://discuss.elastic.co/t/how-to-avoid-duplicate-values-being-copied-while-using-copy-to/330905 "2023-04-27T06:25:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Srikrishna\_Raghupath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/srikrishna_raghupath/32/119198_2.png) [@Srikrishna\_Raghupath](https://discuss.elastic.co/u/Srikrishna_Raghupath)\
**Post date:** [April 27, 2023, 6:25am UTC](https://discuss.elastic.co/t/how-to-avoid-duplicate-values-being-copied-while-using-copy-to/330905/1 "2023-04-27T06:25:31Z")

</div>

My Index definition:

```auto
PUT /test-index
{
  "mappings": {
    "properties": {
      "category":{
        "type": "text", 
        "similarity": "boolean",
        "term_vector": "with_positions_offsets",
        "fields":{
          "facet":{
            "type": "keyword"
          }
        },
        "copy_to": "test"
      },
      "keywords":{
        "type": "text", 
        "similarity": "boolean",
        "term_vector": "with_positions_offsets",
        "fields":{
          "facet":{
            "type": "keyword"
          }
        },
        "copy_to": "test"
      },
      "test":{
        "type": "text", 
        "similarity": "boolean",
        "term_vector": "with_positions_offsets",
        "fields":{
          "facet":{
            "type": "keyword"
          }
        }
      }
    }
  }
}

```

documents:

```auto

PUT /test-index/_doc/1
{
    "category": ["ABC","123","hgh6"],
    "keywords": "XYZ"
}

PUT /test-index/_doc/2
{
    "category": ["XYZ","ABC","hgh7"],
    "keywords": "XYZ"
}

PUT /test-index/_doc/3
{
    "category": ["XYZ","ABC","hgh7"],
    "keywords": "uuu"
}

```

Search Query:

```auto
GET test-index/_search
{
  "query": {
    "match_all": {}
  },
  "fields": [
    "test"
  ]
}

```

Output:

 ![Screenshot 2023-04-27 at 08.21.04](https://us1.discourse-cdn.com/elastic/original/3X/e/f/efbc26294cae5cf981d7639f6a31859a43ca7ff8.png)

My question is,  
how to avoid duplicate values in the field "test"? I want it behave as a set instead of list.

Thank you in advance

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 30, 2023, 11:22am UTC](https://discuss.elastic.co/t/how-to-avoid-duplicate-values-being-copied-while-using-copy-to/330905/2 "2023-04-30T11:22:10Z")

</div>

Hi @Srikrishna_Raghupath

Maybe this filters dont work for you, [unique](https://www.elastic.co/guide/en/elasticsearch/reference/7.17/analysis-unique-tokenfilter.html) and [remove duplicate](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-remove-duplicates-tokenfilter.html), but this script processor can help you.

In this case you dont need the copy\_to, the processor set put the values in field.

```auto
POST /_ingest/pipeline/_simulate
{
  "pipeline": {
    "description": "_description",
    "processors": [
      {
        "script": {
          "source": """
            def categories = ctx['category'];
            def keyword = ctx['keywords'];
            if (!categories.contains(keyword)) {
              categories.add(keyword);
            }
            ctx['test'] = categories;
          """
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_id": "id",
      "_source": {
        "category": [
          "XYZ",
          "ABC",
          "hgh"
        ],
        "keywords": "XYZ"
      }
    }
  ]
}

```

Index Data

```auto
PUT /test-index/_doc/2?pipeline=avoid_duplicate
{
    "category": ["XYZ","ABC","hgh7"],
    "keywords": "XYZ"
}

```

---

<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 28, 2023, 11:23am UTC](https://discuss.elastic.co/t/how-to-avoid-duplicate-values-being-copied-while-using-copy-to/330905/3 "2023-05-28T11:23:05Z")

</div>

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