# What's faster /index/\_doc/id or /index/\_search with id query

**URL:** <https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297>\
**Category:** Elasticsearch\
**Created:** [December 10, 2019, 12:33pm UTC](https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297 "2019-12-10T12:33:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![henryn73](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/henryn73/32/51126_2.png) [@henryn73](https://discuss.elastic.co/u/henryn73)\
**Post date:** [December 10, 2019, 12:33pm UTC](https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297/1 "2019-12-10T12:33:45Z")

</div>

If you are looking for a document based on id, what is faster and why:  
/index/\_doc/id -\> i would think it is, but maybe under covers it is the same as  
/index/\_search {"query" : {"terms" : { "\_id": .....  
any reason one would prefer one of these vs the other and vise versa?  
Thank you.

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [December 10, 2019, 8:37pm UTC](https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297/2 "2019-12-10T20:37:51Z")

</div>

One is not syntactic sugar for the other. The `index/_doc/id` call is real-time, in that you can create doc `id` and then read it back instantly. The `_search` API behaves differently, and will only surface your doc after it's visible in the segment it has been written to based on the `refresh_interval`. I'd expect the simple id lookup to be faster as it's just doing a lot less that search.

---

<div class="post-metadata">

**Author:** ![DavidTurner](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidturner/32/22453_2.png) [@DavidTurner](https://discuss.elastic.co/u/DavidTurner)\
**Post date:** [December 11, 2019, 8:37am UTC](https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297/3 "2019-12-11T08:37:04Z")

</div>

> [@loren](#):
>
> I'd expect the simple id lookup to be faster as it's just doing a lot less that search.

This is not completely true because a realtime GET will [execute an (internal) refresh if the document was recently modified](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#realtime), which may be expensive. You can of course [disable the realtime behaviour](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-get.html#realtime) and then I think I'd also expect it to be a little cheaper than a terms query (or the [more-specialized IDs query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ids-query.html)). It's worth benchmarking, however, as these kinds of question are subtle and may depend on the details of the actual workload.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 11, 2019, 9:13am UTC](https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297/4 "2019-12-11T09:13:15Z")

</div>

A GET by doc\_id routes the request to exactly one shard whereas a search is broadcast to all shards.

---

<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:** [January 8, 2020, 9:13am UTC](https://discuss.elastic.co/t/whats-faster-index-doc-id-or-index-search-with-id-query/211297/5 "2020-01-08T09:13:25Z")

</div>

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