# How to create a new field in a doc using scripted fields and ingest pipeline

**URL:** <https://discuss.elastic.co/t/how-to-create-a-new-field-in-a-doc-using-scripted-fields-and-ingest-pipeline/222989>\
**Category:** Elasticsearch\
**Created:** [March 10, 2020, 5:36pm UTC](https://discuss.elastic.co/t/how-to-create-a-new-field-in-a-doc-using-scripted-fields-and-ingest-pipeline/222989 "2020-03-10T17:36:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fenster25](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fenster25/32/55546_2.png) [@fenster25](https://discuss.elastic.co/u/fenster25)\
**Post date:** [March 10, 2020, 5:36pm UTC](https://discuss.elastic.co/t/how-to-create-a-new-field-in-a-doc-using-scripted-fields-and-ingest-pipeline/222989/1 "2020-03-10T17:36:19Z")

</div>

So I have two fields in my docs

```
{
    emails: ["", "", ""]
    name: "",
 }

```

And I want to have a new field once the docs are indexed called uid which will just contain the concatenated strings of all the emails and the name for every doc.

I am able to get scripted field like that using this GET request on my index `_search` endpoint

```
{
 "script_fields": {
"combined": {
	"script": {
      "lang": "painless",
      "source": "def result=''; for (String email: doc['emails.keyword']) { result = result + email;} return doc['name'].value + result;"
    }
}
  }
 }

```

I want to know what my ingest pipeline PUT request body should look like if I want to have the same scripted field indexed with my docs?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [March 12, 2020, 10:54am UTC](https://discuss.elastic.co/t/how-to-create-a-new-field-in-a-doc-using-scripted-fields-and-ingest-pipeline/222989/2 "2020-03-12T10:54:29Z")

</div>

How about this

```auto
POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "lang": "painless",
          "source": """
  ctx.all = ctx.emails.stream().collect(Collectors.joining(" ")) + " " + ctx.name
  """
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "emails": [
          "a@a.a",
          "b@b.b",
          "c@c.c"
        ],
        "name": "The unknown"
      }
    }
  ]
}

```

You could also take a look at the [copy\_to](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/copy-to.html) field mapping parameter for this.

Maybe you can explain a bit more about the use-case to figure out what makes the most sense.

---

<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:** [April 9, 2020, 10:54am UTC](https://discuss.elastic.co/t/how-to-create-a-new-field-in-a-doc-using-scripted-fields-and-ingest-pipeline/222989/3 "2020-04-09T10:54:37Z")

</div>

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