# How to merge two indexes based on common field in Elasticsearch?

**URL:** https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114
**Category:** Elasticsearch
**Created:** [August 6, 2019, 11:16pm UTC](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114 "2019-08-06T23:16:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![EZprogramming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ezprogramming/32/57291_2.png) [@EZprogramming](https://discuss.elastic.co/u/EZprogramming)
#### Post date: [August 6, 2019, 11:16pm UTC](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114/1 "2019-08-06T23:16:14Z")

</div>

Given two indexes `test` and `amazon`.

```
PUT /test/_doc/1
{
  "customer_id" : "1234",
  "customer_name" : "origin",
  "farm_name": "Origin Delta"
}

PUT /amazon/_doc/2
{
  "customer_id" : "1234",
  "farm_name": "Origin Delta"
}

```

I want to merge these two indexes based on `customer_id`.

**Question:** Is there a way to merge two indexes, `test` and `amazon` together such that `amazon` index will have `customer_name` field with the value "origin"?

**Attempt 1:**

```
POST /test,amazon/_forcemerge
POST /_forcemerge
GET /test,amazon/_search

```

**output:**

```
"hits" : [
  {
    "_index" : "amazon",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "customer_id" : "1234",
      "farm_name" : "Origin Delta"
    }
  },
  {
    "_index" : "test",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "customer_id" : "1234",
      "customer_name" : "origin",
      "farm_name" : "Origin Delta"
    }
  }
]

```

Result Analysis:

- Attempt didn't work. I was expecting customer\_name: "origin" to be added to the index amazon, but it didn't work.

**Attempt 2:**

```
POST _reindex
{
  "source": {
    "index": ["amazon", "test"]
  },
  "dest": {
    "index": "new_amazon"
  }
}

GET /new_amazon/_search

```

**output:**

```
"hits" : [
  {
    "_index" : "new_amazon",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 1.0,
    "_source" : {
      "customer_id" : "1234",
      "farm_name" : "Origin Delta"
    }
  },
  {
    "_index" : "new_amazon",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 1.0,
    "_source" : {
      "customer_id" : "1234",
      "customer_name" : "origin",
      "farm_name" : "Origin Delta"
    }
  }
]

```

Result Analysis:

- Attempt didn't work. I was expecting customer\_name: "origin" to be added to the index amazon, but it didn't work.

---

<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: [August 7, 2019, 7:27am UTC](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114/2 "2019-08-07T07:27:52Z")

</div>

forcemerge is doing something completely different (think of it as some compaction, that happen automatically in the background anyway). reindex allows you to index documents of one index into another, while modifying them, but not to merge two indices into one.

As it is very hard to figure out the logic how to merge two indices (even though in many cases there is one join field, but it could also easily be more than that or another join logic like calculation).

In this case you are better if writing your own logic. You might want to take a look into something called entity centric indexing, see an old elasticon talk here [https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes](https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes)

Hope this helps!

--Alex

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [August 7, 2019, 7:34am UTC](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114/3 "2019-08-07T07:34:52Z")

</div>

You could use the scroll api to do a single search across both indices sorted by customer-Id. That would give you a single stream of results with related docs next to each other in the results order. A custom script could consume this stream and merge the doc pairs and then add the merged doc to a new index using the bulk api

---

<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: [September 4, 2019, 7:34am UTC](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114/4 "2019-09-04T07:34:53Z")

</div>

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