# How do I combine 2 index data in elasticsearch?

**URL:** https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044
**Category:** Elasticsearch
**Created:** [September 11, 2019, 8:47am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044 "2019-09-11T08:47:20Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 8:47am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/1 "2019-09-11T08:47:20Z")

</div>

How do I combine 2 index data in elasticsearch?

Example;

1.index data = {appId,name,clasId}  
2.index data={clasId,name,description}

1.index via clasId 2.index class name how do I pull ?

---

<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: [September 11, 2019, 8:58am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/2 "2019-09-11T08:58:19Z")

</div>

See [[Resolved]Join data cross two index based on common field](https://discuss.elastic.co/t/resolved-join-data-cross-two-index-based-on-common-field/195458)

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 11:15am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/3 "2019-09-11T11:15:16Z")

</div>

this page not working  
[https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes](https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes)

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 11:17am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/4 "2019-09-11T11:17:59Z")

</div>

I could not see the solution.

---

<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: [September 11, 2019, 12:09pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/5 "2019-09-11T12:09:42Z")

</div>

> [@mr\_searchng](#):
>
> I could not see the solution.

This one? [How to merge two indexes based on common field in Elasticsearch? - #3 by Mark\_Harwood](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114/3)

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 12:21pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/6 "2019-09-11T12:21:39Z")

</div>

[How to merge two indexes based on common field in Elasticsearch?](https://discuss.elastic.co/t/how-to-merge-two-indexes-based-on-common-field-in-elasticsearch/194114/3)

screen shot of the solution here

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 12:47pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/7 "2019-09-11T12:47:31Z")

</div>

I do not fully understand the solution.

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 12:58pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/8 "2019-09-11T12:58:16Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/d/5/d582cac65216a8909eaba6857aa1407c8abc4fb1.png)

If that's the solution, I don't understand it.  
Can you tell in more detail

---

<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: [September 11, 2019, 12:59pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/9 "2019-09-11T12:59:35Z")

</div>

It requires writing custom code that uses those APIs.  
The exact solution would depend on your preferred choice of programming language.

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 1:01pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/10 "2019-09-11T13:01:50Z")

</div>

Can't this process be done in elasticsearch?

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 1:07pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/11 "2019-09-11T13:07:58Z")

</div>

It requires writing custom code that uses those APIs.  
is there any code snippet that does the operation you mentioned above?

---

<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: [September 11, 2019, 2:14pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/12 "2019-09-11T14:14:55Z")

</div>

This is a rough outline for merging 2 indices into a third using python [https://gist.github.com/markharwood/7bca8a15a6b0c36b6b412971ee192037](https://gist.github.com/markharwood/7bca8a15a6b0c36b6b412971ee192037)

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 2:32pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/13 "2019-09-11T14:32:42Z")

</div>

POST test1/\_doc/1  
{  
"Id":1,  
"name":"fooName",  
"categoryId":1  
}  
POST category/\_doc/1  
{  
"Id":1,  
"name":"one",  
"address":"fooAddress"  
}

here is categoryId into test1,Id into category ,this fields join data, both two field musn't categoryId,

JOİN FİELDS:

test1 :categoryId  
category:Id

RESULT:  
test1 or other creating third index  
{  
"Id":1,  
"name":"fooName",  
"categoryId":1,  
"name":"one"  
}

---

<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: [September 11, 2019, 2:54pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/14 "2019-09-11T14:54:08Z")

</div>

If you don't have a common field name you would need to use a script to retrieve the sort value depending on which index the document came from eg.

```
GET test1,test2/_search
{
  "sort":{
	"_script" : {
			"type" : "string",
			"script" : {
				"lang": "painless",
				"source": "if (doc.containsKey('catId')) return doc['catId']; return doc['myId'] ;"
			},
			"order" : "asc"
		}
  }
}

```

You'd have to modify my Python code which fuses the documents accordingly because that assumes a common field name.

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 11, 2019, 3:13pm UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/15 "2019-09-11T15:13:45Z")

</div>

Can you make join code running in elastic search?

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 12, 2019, 7:53am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/16 "2019-09-12T07:53:08Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/6/3/631eee56edc24cdf651b466c1354092496769934.png)

that's exactly what I want.

---

<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: [September 12, 2019, 8:06am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/17 "2019-09-12T08:06:39Z")

</div>

At some point the database ends and your application code begins.  
I’ve already given you the pointers required to do this - if you can’t code that’s fine but we are not here to do that for you.

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 12, 2019, 8:10am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/18 "2019-09-12T08:10:19Z")

</div>

thank you for everything ,  
For the first time I have been dealing with nosql structure.

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 12, 2019, 8:13am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/19 "2019-09-12T08:13:30Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/c/6/c60409a2e18fb52e9310bd8d2eae55746ad60036.png)

My purpose was this.  
In elasticsearch, solve this with a code structure that does this.

---

<div class="post-metadata">

### Author: ![mr\_searchng](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mr\_searchng](https://discuss.elastic.co/u/mr_searchng)
#### Post date: [September 12, 2019, 8:14am UTC](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044/20 "2019-09-12T08:14:11Z")

</div>

I would really appreciate it if you really help.

[Next page](https://discuss.elastic.co/t/how-do-i-combine-2-index-data-in-elasticsearch/199044.md?page=2)
