Combine multiple document into one document with limited Fields ( merging of documents )

You can use a transform for it. The group_by must be based on a common criteria, e.g. a common id.

For the combining step, you can use a scripted metric, e.g.:

"all_docs": {
  "scripted_metric": {
    "init_script": "state.docs = []",
    "map_script": "state.docs.add(new HashMap(params['_source']))",
    "combine_script": "return state.docs",
    "reduce_script": "def docs = []; for (s in states) {for (d in s) { docs.add(d);}}return docs"
  }
}

this would create an array with the original docs. If you want to merge them or merge only a specific field, you need to change the example accordingly, the docs contain some more ideas.

Hope that helps.

1 Like