# Merge fields between documents

**URL:** https://discuss.elastic.co/t/merge-fields-between-documents/303525
**Category:** Elasticsearch
**Tags:** eql-elastic-query-language
**Created:** [April 28, 2022, 2:34pm UTC](https://discuss.elastic.co/t/merge-fields-between-documents/303525 "2022-04-28T14:34:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Khaled\_Saidi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khaled_saidi/32/98636_2.png) [@Khaled\_Saidi](https://discuss.elastic.co/u/Khaled_Saidi)
#### Post date: [April 28, 2022, 2:34pm UTC](https://discuss.elastic.co/t/merge-fields-between-documents/303525/1 "2022-04-28T14:34:34Z")

</div>

Hi everyone,

I have 2 kinds of docments in an Elasticsearch index.  
Document 1:

```auto
{
	"_id": "xxxxxxx",
	"internal_id": "123456",
	"message": "LOREM IPSUM"
}

```

Document 2:

```auto
{
	"_id": "yyyyyyy",
	"internal_id": "123456",
	"date": "2022-04-28",
	"method": "GET"
}

```

Is there a way to query the index and have such a result:

```auto
{
	"_id": "yyyyyyy",
	"internal_id": "123456",
	"date": "2022-04-28",
	"method": "GET",
	"message": "LOREM IPSUM"
}

```

A query that is able to "inject" a field from document 1 into document 2 in its result.  
If not possible, how to join on the same field between 2 documents.

Thanx a lot for your help and answers.

Khaled

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [April 28, 2022, 4:05pm UTC](https://discuss.elastic.co/t/merge-fields-between-documents/303525/2 "2022-04-28T16:05:43Z")

</div>

Hi!

I think you should do this merge on the application side but I send the post below, hope it helps.

> [@Combine multiple document into one document with limited Fields ( merging of documents )](https://discuss.elastic.co/t/combine-multiple-document-into-one-document-with-limited-fields-merging-of-documents/231758/6):
>
> You can use a [transform](https://www.elastic.co/guide/en/elasticsearch/reference/7.7/transforms.html) 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 do…

---

<div class="post-metadata">

### Author: ![Khaled\_Saidi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/khaled_saidi/32/98636_2.png) [@Khaled\_Saidi](https://discuss.elastic.co/u/Khaled_Saidi)
#### Post date: [April 29, 2022, 8:01am UTC](https://discuss.elastic.co/t/merge-fields-between-documents/303525/3 "2022-04-29T08:01:45Z")

</div>

Hi Andre,

Thank you for this solution. i will try it today..  
Hope it works for me.

---

<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: [May 27, 2022, 8:01am UTC](https://discuss.elastic.co/t/merge-fields-between-documents/303525/4 "2022-05-27T08:01:46Z")

</div>

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