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

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?

How about this

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 field mapping parameter for this.

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

1 Like

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