# Convert two repeated values in array into a string

**URL:** <https://discuss.elastic.co/t/convert-two-repeated-values-in-array-into-a-string/304429>\
**Category:** Elasticsearch\
**Created:** [May 11, 2022, 8:15am UTC](https://discuss.elastic.co/t/convert-two-repeated-values-in-array-into-a-string/304429 "2022-05-11T08:15:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adrianfusco](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adrianfusco/32/111798_2.png) [@adrianfusco](https://discuss.elastic.co/u/adrianfusco)\
**Post date:** [May 11, 2022, 8:15am UTC](https://discuss.elastic.co/t/convert-two-repeated-values-in-array-into-a-string/304429/1 "2022-05-11T08:15:30Z")

</div>

Hello,

I have some old documents where a field has an array of two vales repeated, something like this:

```auto
          "task" : [
            "first_task",
            "first_task"
          ],

```

I'm trying to convert this array into a string because it's the same value. I've seen the following script: [Convert array with 2 equal values to single value](https://discuss.elastic.co/t/convert-array-with-2-equal-values-to-single-value/196763) but in my case, this problem can't be fixed through logstash because it happens just with old documents stored.

Is there a way to manipulate actual data from Elasticsearch and convert it to string using DSL queries?

I was thinking to do something like this:

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "description": "Change task field from array to first element of this one",
          "lang": "painless",
          "source": """
            if (ctx['task'][0] == ctx['task'][1]) {
                ctx['task'] = ctx['task'][0];
            }
          """
        }
      }
    ]
  },
  "docs": [
    {
        "_index" : "tasks",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "@timestamp" : "2022-05-03T07:33:44.652Z",
          "task" : ["first_task", "first_task"]
        }
    }
  ]
}

```

The result document is the following:

```auto
{
  "docs" : [
    {
      "doc" : {
        "_index" : "tasks",
        "_type" : "_doc",
        "_id" : "1",
        "_source" : {
          "@timestamp" : "2022-05-03T07:33:44.652Z",
          "task" : "first_task"
        },
        "_ingest" : {
          "timestamp" : "2022-05-11T09:08:48.150815183Z"
        }
      }
    }
  ]
}

```

We can see the `task` field is reassigned and we have the first element of the array as a value.

Thanks.

---

<div class="post-metadata">

**Author:** ![adrianfusco](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/adrianfusco/32/111798_2.png) [@adrianfusco](https://discuss.elastic.co/u/adrianfusco)\
**Post date:** [May 11, 2022, 12:21pm UTC](https://discuss.elastic.co/t/convert-two-repeated-values-in-array-into-a-string/304429/2 "2022-05-11T12:21:49Z")

</div>

Here's the solution: [elasticsearch - Convert two repeated values in array into a string - Stack Overflow](https://stackoverflow.com/questions/72198379/convert-two-repeated-values-in-array-into-a-string/72200472#72200472)

I'll copy it here.

You can achieve this with `_update_by_query` endpoint. Here is an example:

```auto
POST tasks/_update_by_query
{
  "script": {
    "source": """
      if (ctx._source['task'][0] == ctx._source['task'][1]) {
          ctx._source['task'] = ctx._source['task'][0];
      }
    """,
    "lang": "painless"
  },
  "query": {
    "match_all": {}
  }
}

```

You can remove the `match_all` query if you want to update all documents or you can filter documents by chaning the conditions in the query.

Keep in mind that running a script to update all documents in the index may cause some performance issues while the update process is running.

---

<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:** [June 8, 2022, 12:22pm UTC](https://discuss.elastic.co/t/convert-two-repeated-values-in-array-into-a-string/304429/3 "2022-06-08T12:22:20Z")

</div>

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