# Return \_version with query hits

**URL:** https://discuss.elastic.co/t/return--version-with-query-hits/10316
**Category:** Elasticsearch
**Created:** [January 11, 2013, 3:25pm UTC](https://discuss.elastic.co/t/return--version-with-query-hits/10316 "2013-01-11T15:25:53Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)
#### Post date: [January 11, 2013, 3:25pm UTC](https://discuss.elastic.co/t/return--version-with-query-hits/10316/1 "2013-01-11T15:25:53Z")

</div>

I'm using the \_version attribute to ensure a put to particular document id  
only succeeds if the correct version is specified This works great except I  
don't seem to be able to get elasticsearch to return the correct version  
with query results.

I'm sending this query:

{"query":{"term":{"accounts.provider\_uid":"xxxx","accounts.provider":"xxxx"}},"from":0,"size":1,"fields":["\_source","\_id","\_version","\_score"]}

I get one result for the query and the \_id, \_score, and \_source field are  
there as expected but the \_version field that I would need to update with  
the version check enabled is missing.

Am I doing something wrong or is this simply not supported?

--

---

<div class="post-metadata">

### Author: ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)
#### Post date: [January 11, 2013, 4:25pm UTC](https://discuss.elastic.co/t/return--version-with-query-hits/10316/2 "2013-01-11T16:25:36Z")

</div>

On Fri, 2013-01-11 at 07:25 -0800, Jilles van Gurp wrote:

> I'm using the \_version attribute to ensure a put to particular  
> document id only succeeds if the correct version is specified This  
> works great except I don't seem to be able to get elasticsearch to  
> return the correct version with query results.
> 
> I'm sending this query:  
> {"query":{"term":{"accounts.provider\_uid":"xxxx","accounts.provider":"xxxx"}},"from":0,"size":1,"fields":["\_source","\_id","\_version","\_score"]}
> 
> I get one result for the query and the \_id, \_score, and \_source field  
> are there as expected but the \_version field that I would need to  
> update with the version check enabled is missing.
> 
> Am I doing something wrong or is this simply not supported?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

clint

> --

--

---

<div class="post-metadata">

### Author: ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)
#### Post date: [January 14, 2013, 1:19pm UTC](https://discuss.elastic.co/t/return--version-with-query-hits/10316/3 "2013-01-14T13:19:13Z")

</div>

Thanks!

That worked. Sort of. I actually found what looks like a bug.

This returns a \_version:

{"from":0,"size":1,"version":true,"query":{"term":{"accounts.provider\_uid":"xxxx","accounts.provider":"xxxx"}}}

This doesn't:

{"query":{"term":{"accounts.provider\_uid":"xxxxxx","accounts.provider":"xxxxx"}},"from":0,"size":1,"version":true}  
The queries are identical except for the position of the parameters.

I filed an issue for  
this: ["version":true only works if it appears before query · Issue #2545 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/2545)

Jilles

On Friday, January 11, 2013 5:25:36 PM UTC+1, Clinton Gormley wrote:

> On Fri, 2013-01-11 at 07:25 -0800, Jilles van Gurp wrote:
> 
> > I'm using the \_version attribute to ensure a put to particular  
> > document id only succeeds if the correct version is specified This  
> > works great except I don't seem to be able to get elasticsearch to  
> > return the correct version with query results.
> > 
> > I'm sending this query:
> 
> {"query":{"term":{"accounts.provider\_uid":"xxxx","accounts.provider":"xxxx"}},"from":0,"size":1,"fields":["\_source","\_id","\_version","\_score"]}
> 
> > I get one result for the query and the \_id, \_score, and \_source field  
> > are there as expected but the \_version field that I would need to  
> > update with the version check enabled is missing.
> > 
> > Am I doing something wrong or is this simply not supported?
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/version.html)
> 
> clint
> 
> > --

--

---

<div class="post-metadata">

### Author: ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)
#### Post date: [January 14, 2013, 10:41pm UTC](https://discuss.elastic.co/t/return--version-with-query-hits/10316/4 "2013-01-14T22:41:31Z")

</div>

This turned out to not be a bug in ES, but the github issue did prompt me  
to update my Java client to call SearchRequestBuilder.setVersion(true), and  
now my SearchHit objects contain the version number! Thanks, Shay!

On Friday, January 11, 2013 10:25:53 AM UTC-5, Jilles van Gurp wrote:

> I'm using the \_version attribute to ensure a put to particular document id  
> only succeeds if the correct version is specified This works great except I  
> don't seem to be able to get elasticsearch to return the correct version  
> with query results.
> 
> I'm sending this query:
> 
> {"query":{"term":{"accounts.provider\_uid":"xxxx","accounts.provider":"xxxx"}},"from":0,"size":1,"fields":["\_source","\_id","\_version","\_score"]}
> 
> I get one result for the query and the \_id, \_score, and \_source field are  
> there as expected but the \_version field that I would need to update with  
> the version check enabled is missing.
> 
> Am I doing something wrong or is this simply not supported?

--

---

<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 6, 2017, 2:56am UTC](https://discuss.elastic.co/t/return--version-with-query-hits/10316/5 "2017-07-06T02:56:34Z")

</div>


