# How to fetch data from multi indices based on condition

**URL:** <https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006>\
**Category:** Elasticsearch\
**Created:** [August 13, 2019, 10:27am UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006 "2019-08-13T10:27:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [August 13, 2019, 10:27am UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/1 "2019-08-13T10:27:52Z")

</div>

Dear All.

i have two indices Index01 and index02.

Condition 01:-1:  
in index01 have ducument/column id(1211) that is matching with index02.

Condition 02:-1:  
in index02 we have same document/column id(1211) but i want to display index02 document/column only like-firstname,lastname,dob based on matching HP(1111).

Data01/index01  
id location ph HP  
1211 UK 0977 1111  
1212 UK 0933 2222

Data02/index02  
FN LN DOB id  
lal pal 12301999 1211  
hapl oue 12041988 1212

I want only FETCH data when Provide HP=1111 THEN display records  
FN LN DOB ID

Thanks

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [August 13, 2019, 4:28pm UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/2 "2019-08-13T16:28:52Z")

</div>

If you only need to lookup a single document in `index02`, than yes, you can use [Terms Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html). Take a look into settings `index` and `id`.

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [August 14, 2019, 1:25pm UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/3 "2019-08-14T13:25:41Z")

</div>

Hi @thiago .

Thanks

but i need query for that i tried more but not luck on two different indices .

Thanks

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [August 14, 2019, 5:06pm UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/4 "2019-08-14T17:06:01Z")

</div>

Unfortunately you can't query, you need to know the ID to be fetched from the other index by advance.

FYI that's the closest to a join you can get in Elasticsearch besides the nested and parent/child mappings.

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [August 15, 2019, 3:20am UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/5 "2019-08-15T03:20:26Z")

</div>

Hi @thiago .

Thanks

then how can i implement the same use case please help by using nested or parent/child?

Thanks

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [August 15, 2019, 11:41am UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/6 "2019-08-15T11:41:22Z")

</div>

This is a whole different approach, you would need to check if it's valid for your use case. First, you need to have a sense of what's the "main" index, let's say it's `index01`. With that defined, you will need to reindex `index01` and there would be no `index02`. While reindexing `index01` it would need to already join, during indexing, with `index02` given that the joined resulting document would not have many documents from `index02` (10s are fine, close or higher then 100s would maybe cause memory issues). Now, if you only need to query documents from `index01` with a matching document in the inner collection with documents from `index02` in which the query uses just a single attribute, then you are fine, but if the query is complex then you will need either [Nested](https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html) or [Join](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html).

See this for more on how to map 1-N: [https://www.elastic.co/guide/en/elasticsearch/guide/current/modeling-your-data.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/modeling-your-data.html)

---

<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 12, 2019, 11:41am UTC](https://discuss.elastic.co/t/how-to-fetch-data-from-multi-indices-based-on-condition/195006/7 "2019-09-12T11:41:26Z")

</div>

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