# Is it ok to make an idsQuery with lots (10k+) of ids on ES 5.x?

**URL:** <https://discuss.elastic.co/t/is-it-ok-to-make-an-idsquery-with-lots-10k-of-ids-on-es-5-x/73833>\
**Category:** Elasticsearch\
**Created:** [February 3, 2017, 11:14am UTC](https://discuss.elastic.co/t/is-it-ok-to-make-an-idsquery-with-lots-10k-of-ids-on-es-5-x/73833 "2017-02-03T11:14:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TrustNoOne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trustnoone/32/15183_2.png) [@TrustNoOne](https://discuss.elastic.co/u/TrustNoOne)\
**Post date:** [February 3, 2017, 11:14am UTC](https://discuss.elastic.co/t/is-it-ok-to-make-an-idsquery-with-lots-10k-of-ids-on-es-5-x/73833/1 "2017-02-03T11:14:20Z")

</div>

Hi,  
I recently bumped into a small problem: I need to aggregate a field (i.e. find unique values) from the search results of a first query and use it as a filter in an ids (or terms) query on a second index.

As an example, let's say that I have an " **events**" index with millions and millions of events made by users. I can aggregate the userId from the results of some search on events and use it as a filter on the " **users**" index.

a **termsQuery** (suppose I have the id also in a keyword field) with 10k+ ids is unfeasible. After setting `indices.query.bool.max_clause_count: 10000` it hogs the cpu for ~10seconds to get a a result on my laptop

The **idsQuery** instead (I am using external ids in the users index) seems to be very fast. _It's so fast it looks like it isn't even there in a quick test I did._

Is this the right way to proceed? Should I do this differently?

I cannot denormalize the user into each event because the impact on disk usage would be huge.

I am currently using ES 5.1, I'll upgrade to 5.2 soon. In my test I created an index with 10M documents and filtered for 10k ids.

Thank you

---

<div class="post-metadata">

**Author:** ![Luke\_Nezda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luke_nezda/32/47152_2.png) [@Luke\_Nezda](https://discuss.elastic.co/u/Luke_Nezda)\
**Post date:** [February 6, 2017, 4:16am UTC](https://discuss.elastic.co/t/is-it-ok-to-make-an-idsquery-with-lots-10k-of-ids-on-es-5-x/73833/2 "2017-02-06T04:16:38Z")

</div>

That's cool that ids query works better than terms query. You may also like [https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html#query-dsl-terms-lookup](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html#query-dsl-terms-lookup)

---

<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:** [March 6, 2017, 4:16am UTC](https://discuss.elastic.co/t/is-it-ok-to-make-an-idsquery-with-lots-10k-of-ids-on-es-5-x/73833/3 "2017-03-06T04:16:56Z")

</div>

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