# Multi index search taking long time to give results

**URL:** <https://discuss.elastic.co/t/multi-index-search-taking-long-time-to-give-results/37743>\
**Category:** Elasticsearch\
**Created:** [December 22, 2015, 9:57am UTC](https://discuss.elastic.co/t/multi-index-search-taking-long-time-to-give-results/37743 "2015-12-22T09:57:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankur\_Singh](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@Ankur\_Singh](https://discuss.elastic.co/u/Ankur_Singh)\
**Post date:** [December 22, 2015, 9:57am UTC](https://discuss.elastic.co/t/multi-index-search-taking-long-time-to-give-results/37743/1 "2015-12-22T09:57:32Z")

</div>

Hi all,

I have 2 indices, index1 and index2 each containing thousands of records. I am querying index1, getting 40k hits, then for each hit I need to query index2 which itself gives 10k hits each time. This way, I end up with a for loop with 40k iterations. Each iteration has a for loop with 10k iterations. Making a total of 40k \* 10k=400000,000 iterations. As you can see this is a huge value and I have to wait for about 30 minutes for the results.  
There surely must be a better way to implement this. May be a function I am not aware of or maybe a better design for such queries.  
Any help would be appreciated!!

---

<div class="post-metadata">

**Author:** ![nodexy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nodexy/32/1064_2.png) [@nodexy](https://discuss.elastic.co/u/nodexy)\
**Post date:** [December 22, 2015, 2:25pm UTC](https://discuss.elastic.co/t/multi-index-search-taking-long-time-to-give-results/37743/2 "2015-12-22T14:25:18Z")

</div>

Please NEVER invoke es API in a for loop !!! Use bulk operation and sleep to optimize it .  
Also you can try to loop by page(if page size is 40, you just need 1k iterations ) , and use a list of ids as parameter instead of single id .

Another way in elasticsearch is using parent-child relationship : [https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.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:** [July 5, 2017, 11:29pm UTC](https://discuss.elastic.co/t/multi-index-search-taking-long-time-to-give-results/37743/3 "2017-07-05T23:29:18Z")

</div>

Closed on July 5, 2017, 11:29pm UTC


