# How to store results temporarily or use a subquery

**URL:** <https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341>\
**Category:** Elasticsearch\
**Created:** [November 3, 2017, 3:51pm UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341 "2017-11-03T15:51:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dulanjan\_Katuwawela](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Dulanjan\_Katuwawela](https://discuss.elastic.co/u/Dulanjan_Katuwawela)\
**Post date:** [November 3, 2017, 3:51pm UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/1 "2017-11-03T15:51:34Z")

</div>

So I did search for use of subqueries but most posts are from several years back, so reposting to make sure there's no feature that's come up recently.

If I have a query that results in a million IDs, how do I query against just those IDs (assuming my index contains over 100 million documents)?

In the relational world you could use store the results in a temp table and reference it in a subsequent query, or you could use a subquery which would contain the logic for the first query. What options are available at this point to do this? Or is there still no support for accomplishing this?

Thanks!!

---

<div class="post-metadata">

**Author:** ![Dulanjan\_Katuwawela](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Dulanjan\_Katuwawela](https://discuss.elastic.co/u/Dulanjan_Katuwawela)\
**Post date:** [November 6, 2017, 2:38pm UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/2 "2017-11-06T14:38:25Z")

</div>

anyone?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 7, 2017, 12:24am UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/3 "2017-11-07T00:24:53Z")

</div>

You need to do it externally to Elasticsearch, there is nothing it has that can do this natively.

---

<div class="post-metadata">

**Author:** ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)\
**Post date:** [November 7, 2017, 5:42am UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/4 "2017-11-07T05:42:57Z")

</div>

probably you can do it with putting ids into the doc and referring it as a terms query  
[https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-terms-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-terms-query.html)

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 7, 2017, 5:53am UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/5 "2017-11-07T05:53:44Z")

</div>

Not unless you get the terms first, that's the problem.

---

<div class="post-metadata">

**Author:** ![Dulanjan\_Katuwawela](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Dulanjan\_Katuwawela](https://discuss.elastic.co/u/Dulanjan_Katuwawela)\
**Post date:** [November 7, 2017, 4:18pm UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/6 "2017-11-07T16:18:03Z")

</div>

one thing we could do (which will work only if you relate data to a singular master dataset) is to reindex the results of a query, storing just the master key, establishing a parent child relationship on the created index with the master index, and use it in the form of a join (this gives you the ability to do a subquery only for a singular field, not any field though).

---

<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:** [December 5, 2017, 4:18pm UTC](https://discuss.elastic.co/t/how-to-store-results-temporarily-or-use-a-subquery/106341/7 "2017-12-05T16:18:31Z")

</div>

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