# Filtered by IdsQuery vs by TermsQuery

**URL:** <https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553>\
**Category:** Elasticsearch\
**Created:** [November 18, 2016, 3:01pm UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553 "2016-11-18T15:01:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [November 18, 2016, 3:01pm UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553/1 "2016-11-18T15:01:52Z")

</div>

Hi all,

Admit we have lot of documents with \_id (String) setted by ourself and a field with the same value of the \_id as Long.

I want to run a filtered query on a list of Long , so I have 2 options :

Option 1:

```
add a must IdsQuery with my list of ids converted as String

```

Option 2:

```
add a must TermsQuery with my list of ids as Long

```

Question : wich one will be the fastest or you recommend ?

thx !

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 18, 2016, 3:11pm UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553/2 "2016-11-18T15:11:34Z")

</div>

In a filter context they are both rewritten to a lucene TermsQuery, so it shouldn't make any difference.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [November 18, 2016, 3:35pm UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553/3 "2016-11-18T15:35:50Z")

</div>

Ok, as the Long value is converted to a String for the \_id field, it may be slower ? Or does Lucene has a super faster map of documents ids ? 🙂

in both cases I'm quite sure it'll make no difference but I prefer to have your expert point of view !

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 21, 2016, 10:49am UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553/4 "2016-11-21T10:49:37Z")

</div>

Hi,

If you are using Elasticsearch 2.x, this should not make any difference since both data types are backed by the similar data structure, the inverted index. In ES 5.0 (which uses Lucene 6) this might have changed since Lucene 6 introduced the "[dimensional points](https://www.elastic.co/blog/lucene-points-6.0)" feature to represent numeric values. I don't know if that has a big impact on your use case though, in this case I'd suggest to do some measuring of your own. Would be nice to share your results here.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [November 21, 2016, 1:00pm UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553/5 "2016-11-21T13:00:51Z")

</div>

Finally, I have converted all idsQuery to termsQuery for 2 reasons :

1°/ I think that one day we'll probably switch to auto generated \_id for documents.

Recommended in the documentation:  
[https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tune-for-indexing-speed.html#\_use\_auto\_generated\_ids](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/tune-for-indexing-speed.html#_use_auto_generated_ids)

2°/ To my mind, an index over a Long array should be faster than a String array...

I'll try to do benchmarks if possible.

---

<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 19, 2016, 1:01pm UTC](https://discuss.elastic.co/t/filtered-by-idsquery-vs-by-termsquery/66553/6 "2016-12-19T13:01:05Z")

</div>

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