0

I need to append a string in already existing field in a document without using script.

My document has a field "Field1" which currently has value1. I want to update the document by appending value2 to the "Field1", such that the field's value is updated to "value1value2".

This can be easily done by using script. But is there any other option by which this can be achieved. Also I want to use only update/update_by_query statement.

2 Answers 2

3

As @joe suggested you need to do it at ingest time, however, you can create a pipeline as a suggestion. I did a local test using the append and join processors. First I appended field1's value and field2's value, then use the join processor on the array created with name append_fields.

I ingested the following values in the index "append_index",

"field1" : "value1", "field2" : "value2"

"field1": "value10", "field2": "value11"

I suggest you create the following pipeline(append_values) and then run it using the _update_by_query:

PUT _ingest/pipeline/append_values
{
  "processors": [
    {
      "append": {
        "field": "append_fields",
        "value": [
          "{{field1}}",
          "{{field2}}"
        ]
      }
    },
    {
      "join": {
        "field": "append_fields",
        "separator": ""
      }
    }
  ]
}

To use a pipeline in the _update__by_query, use the following:

POST append_index/_update_by_query?pipeline=append_values

The response would be:

"hits" : [
  {
    "_index" : "append_index",
    "_type" : "_doc",
    "_id" : "_9ovaHQBsTCl1BZv7qhZ",
    "_score" : 1.0,
    "_source" : {
      "field1" : "value1",
      "append_fields" : "value1value2",
      "field2" : "value2"
    }
  },
  {
    "_index" : "append_index",
    "_type" : "_doc",
    "_id" : "AdovaHQBsTCl1BZv9ak1",
    "_score" : 1.0,
    "_source" : {
      "field1" : "value10",
      "append_fields" : "value10value11",
      "field2" : "value11"
    }
  }
]

Links: https://www.elastic.co/guide/en/elasticsearch/reference/7.9/append-processor.html https://www.elastic.co/guide/en/elasticsearch/reference/7.9/join-processor.html https://www.elastic.co/guide/en/elasticsearch/reference/7.9/pipeline.html https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html

Hope my suggestion helps :)

If you need help to understand what the append processor do, here is the output of executing the append processor without using the join processor:

"hits" : [
  {
    "_index" : "append_index",
    "_type" : "_doc",
    "_id" : "G9o9aHQBsTCl1BZvMcv6",
    "_score" : 1.0,
    "_source" : {
      "field1" : "value1",
      "append_fields" : [
        "value1",
        "value2"
      ],
      "field2" : "value2"
    }
  },
  {
    "_index" : "append_index",
    "_type" : "_doc",
    "_id" : "edo9aHQBsTCl1BZvP8vT",
    "_score" : 1.0,
    "_source" : {
      "field1" : "value10",
      "append_fields" : [
        "value10",
        "value11"
      ],
      "field2" : "value11"
    }
  }
]
Sign up to request clarification or add additional context in comments.

1 Comment

If the answer was useful, please accept the answer thank you :)
1

Not that I know of.

The only (indirect) alternative is appending those values at ingest time. Though that approach also utilizes a script...

What's the reasoning behind not wanting to script?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.