# Best approach to Join two different Elastic Indexes

**URL:** <https://discuss.elastic.co/t/best-approach-to-join-two-different-elastic-indexes/182284>\
**Category:** Elasticsearch\
**Created:** [May 22, 2019, 4:34pm UTC](https://discuss.elastic.co/t/best-approach-to-join-two-different-elastic-indexes/182284 "2019-05-22T16:34:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![matheusribeirogarcia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matheusribeirogarcia/32/44363_2.png) [@matheusribeirogarcia](https://discuss.elastic.co/u/matheusribeirogarcia)\
**Post date:** [May 22, 2019, 4:34pm UTC](https://discuss.elastic.co/t/best-approach-to-join-two-different-elastic-indexes/182284/1 "2019-05-22T16:34:35Z")

</div>

Hello everyone, I have two Indexes, let me call them Index1 and Index2.

I'm trying to update documents of Index2 that contains an expecif field value.

Basically, for every document in Index1 I get a field value and query this expecif value on Index2, for every document on Index2 that is queried, I copy all fields of index1 document into the relative Index2 document.

I have around 100 fields that I need to copy, what's the best approach?

Here's what I've start thinking:

```
       SearchResponse scrollResp = client.prepareSearch("ensinosuperiories")
				.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC).setScroll(new TimeValue(60000)).setSize(100)
				.get();
		do {
			for (SearchHit hit : scrollResp.getHits().getHits()) {
				source = hit.getSourceAsMap();
				try {
					institution_cod = source.get("CO_IES").toString();
					institution_name = source.get("NO_IES").toString();
					String[] keys = source.entrySet().toString().split(",");

					UpdateByQueryRequestBuilder updateByQuery = new UpdateByQueryRequestBuilder(client,
							UpdateByQueryAction.INSTANCE);
					updateByQuery.source("ensino_curso").filter(QueryBuilders.termQuery("CO_IES", institution_cod))
							.size(1000).script(new Script(ScriptType.INLINE,
									"ctx._source.NO_IES = "+institution_name,
								"painless", 
									Collections.emptyMap()));
					BulkByScrollResponse response = updateByQuery.get();

				} catch (NullPointerException e) {
				}
			}
			scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute()
					.actionGet();
		} while (scrollResp.getHits().getHits().length != 0);
	}

```

Is there a way to copy the fields that doesn't require manual writting every single field?

---

<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:** [June 19, 2019, 4:34pm UTC](https://discuss.elastic.co/t/best-approach-to-join-two-different-elastic-indexes/182284/2 "2019-06-19T16:34:44Z")

</div>

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