# Difference between GET by document id, and running a query matching on document id?

**URL:** <https://discuss.elastic.co/t/difference-between-get-by-document-id-and-running-a-query-matching-on-document-id/62313>\
**Category:** Elasticsearch\
**Created:** [October 5, 2016, 9:04pm UTC](https://discuss.elastic.co/t/difference-between-get-by-document-id-and-running-a-query-matching-on-document-id/62313 "2016-10-05T21:04:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bguyzer](https://avatars.discourse-cdn.com/v4/letter/b/50afbb/32.png) [@bguyzer](https://discuss.elastic.co/u/bguyzer)\
**Post date:** [October 5, 2016, 9:04pm UTC](https://discuss.elastic.co/t/difference-between-get-by-document-id-and-running-a-query-matching-on-document-id/62313/1 "2016-10-05T21:04:52Z")

</div>

Is there a performance difference when retrieving a document by

1. GET request by document ID IE:

`GET /my_index/my_type/12345`

1. via a \_search query IE:

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [October 5, 2016, 9:21pm UTC](https://discuss.elastic.co/t/difference-between-get-by-document-id-and-running-a-query-matching-on-document-id/62313/2 "2016-10-05T21:21:56Z")

</div>

GET by a document ID is automatically routed to the shard with that document because the routing value comes from the document's ID.

Any query on `_id` or `_uid` has to go to all the shards. In fact, `_id` could be fairly inefficient because it has to be rewritten to a bool query with a bunch of queries on `_uid` in the `should` clause. If you query on `_uid` and add `routing` to the request you get most of the efficiency back.

One difference is the real time vs not real time aspect. GET by document ID will always return the most recent version of that document we have even if that most recent version isn't in the Lucene index yet. Search will only search what has been refreshed. Exactly how GET by document ID does that actually differs depending on version. In all versions before the very next beta we make of 5.0 the document will be read form the translog if it is not in the lucene index. In the next beta of 5.0 and in Elasticsearch versions build from the 5.0, 5.x, and master branches Elasticsearch will force a refresh to push the document into the Lucene index and then read the Lucene index. The new way is much less efficient but much simpler and allows us to optimize the write path much better.

---

<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, 10:14pm UTC](https://discuss.elastic.co/t/difference-between-get-by-document-id-and-running-a-query-matching-on-document-id/62313/3 "2017-07-05T22:14:36Z")

</div>


