# setVersion(false) on SearchRequestBuilder Not Working

**URL:** https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298
**Category:** Elasticsearch
**Created:** [April 11, 2012, 3:39pm UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298 "2012-04-11T15:39:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ed\_Brown\_VSTI](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@Ed\_Brown\_VSTI](https://discuss.elastic.co/u/Ed_Brown_VSTI)
#### Post date: [April 11, 2012, 3:39pm UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298/1 "2012-04-11T15:39:20Z")

</div>

Hello,  
I'm using the SearchRequestBuilder to perform a search and I noticed I'm  
getting multiple versions of the same document returned. I then used  
setVersion(false), recompiled and ran the code again and I got the same  
result.  
A code snippet follows:

=== Start Snippet ===

QueryBuilder qb = QueryBuilders.fieldQuery(idField,  
idValue);  
SearchRequestBuilder srb = tclient.prepareSearch(index);  
srb.setVersion(false);

srb.setSearchType(SearchType.QUERY\_AND\_FETCH);  
srb.setQuery(qb);  
srb.addFields(documentFields);  
srb.addField("\_id");

ListenableActionFuture laf = srb.execute();

SearchResponse resp = laf.actionGet();

SearchHits hits = resp.getHits();

=== End Snippet ===

## What gives? I'm trying to do a query and then an update. With multiple versions being returned, I'm getting nasty results.

Ed Brown

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [April 11, 2012, 3:58pm UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298/2 "2012-04-11T15:58:55Z")

</div>

Are you sure that you are getting multiple versions of the same document?  
Is it possible that you are getting different documents (with different  
values of \_index, \_type or \_id fields) that just look similar?

Elasticsearch stores only the last version. setVersion doesn't control  
saving/retrieving multiple versions; it just indicates if the version value  
should be returned or not.

On Wednesday, April 11, 2012 11:39:20 AM UTC-4, Ed Brown VSTI wrote:

> Hello,  
> I'm using the SearchRequestBuilder to perform a search and I noticed I'm  
> getting multiple versions of the same document returned. I then used  
> setVersion(false), recompiled and ran the code again and I got the same  
> result.  
> A code snippet follows:
> 
> === Start Snippet ===
> 
> QueryBuilder qb = QueryBuilders.fieldQuery(idField,  
> idValue);  
> SearchRequestBuilder srb = tclient.prepareSearch(index);  
> srb.setVersion(false);
> 
> srb.setSearchType(SearchType.QUERY\_AND\_FETCH);  
> srb.setQuery(qb);  
> srb.addFields(documentFields);  
> srb.addField("\_id");
> 
> ListenableActionFuture laf = srb.execute();
> 
> SearchResponse resp = laf.actionGet();
> 
> SearchHits hits = resp.getHits();
> 
> === End Snippet ===
> 
> ## What gives? I'm trying to do a query and then an update. With multiple versions being returned, I'm getting nasty results.
> 
> Ed Brown

---

<div class="post-metadata">

### Author: ![Ed\_Brown\_VSTI](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@Ed\_Brown\_VSTI](https://discuss.elastic.co/u/Ed_Brown_VSTI)
#### Post date: [April 11, 2012, 4:33pm UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298/3 "2012-04-11T16:33:23Z")

</div>

On Wednesday, April 11, 2012 11:58:55 AM UTC-4, Igor Motov wrote:

> Are you sure that you are getting multiple versions of the same document?

Yes. I'm printing out the index id and version number and I'm seeing the  
same index id and increasing version numbers. It is in a very tight loop  
where I query based on id and then update.

> Is it possible that you are getting different documents (with different  
> values of \_index, \_type or \_id fields) that just look similar?

No.

> Elasticsearch stores only the last version. setVersion doesn't control  
> saving/retrieving multiple versions; it just indicates if the version value  
> should be returned or not.

Then that makes what I'm seeing even stranger. The code goes:

Get id of document to update;  
Perform search using document id;  
Get document hit;  
For each document hit {  
Copy all fields returned;  
Put in new fields;  
Index each document hit;  
}

> On Wednesday, April 11, 2012 11:39:20 AM UTC-4, Ed Brown VSTI wrote:
> 
> > Hello,  
> > I'm using the SearchRequestBuilder to perform a search and I noticed I'm  
> > getting multiple versions of the same document returned. I then used  
> > setVersion(false), recompiled and ran the code again and I got the same  
> > result.  
> > A code snippet follows:
> > 
> > === Start Snippet ===
> > 
> > QueryBuilder qb = QueryBuilders.fieldQuery(idField,  
> > idValue);  
> > SearchRequestBuilder srb = tclient.prepareSearch(index);  
> > srb.setVersion(false);
> > 
> > srb.setSearchType(SearchType.QUERY\_AND\_FETCH);  
> > srb.setQuery(qb);  
> > srb.addFields(documentFields);  
> > srb.addField("\_id");
> > 
> > ListenableActionFuture laf = srb.execute();
> > 
> > SearchResponse resp = laf.actionGet();
> > 
> > SearchHits hits = resp.getHits();
> > 
> > === End Snippet ===
> > 
> > ## What gives? I'm trying to do a query and then an update. With multiple versions being returned, I'm getting nasty results.
> > 
> > Ed Brown

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [April 11, 2012, 4:58pm UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298/4 "2012-04-11T16:58:10Z")

</div>

I see. If you are not calling refresh after indexing, you might be actually  
getting the old version. Elasticsearch doesn't save old version in the long  
term, but it takes time (1sec by default) for an updated record to appear  
in the search results. You should, probably, use realtime get instead of  
search or update api.

On Wednesday, April 11, 2012 12:33:23 PM UTC-4, Ed Brown VSTI wrote:

> On Wednesday, April 11, 2012 11:58:55 AM UTC-4, Igor Motov wrote:
> 
> > Are you sure that you are getting multiple versions of the same document?
> 
> Yes. I'm printing out the index id and version number and I'm seeing the  
> same index id and increasing version numbers. It is in a very tight loop  
> where I query based on id and then update.
> 
> > Is it possible that you are getting different documents (with different  
> > values of \_index, \_type or \_id fields) that just look similar?
> 
> No.
> 
> > Elasticsearch stores only the last version. setVersion doesn't control  
> > saving/retrieving multiple versions; it just indicates if the version value  
> > should be returned or not.
> 
> Then that makes what I'm seeing even stranger. The code goes:
> 
> Get id of document to update;  
> Perform search using document id;  
> Get document hit;  
> For each document hit {  
> Copy all fields returned;  
> Put in new fields;  
> Index each document hit;  
> }
> 
> > On Wednesday, April 11, 2012 11:39:20 AM UTC-4, Ed Brown VSTI wrote:
> > 
> > > Hello,  
> > > I'm using the SearchRequestBuilder to perform a search and I noticed I'm  
> > > getting multiple versions of the same document returned. I then used  
> > > setVersion(false), recompiled and ran the code again and I got the same  
> > > result.  
> > > A code snippet follows:
> > > 
> > > === Start Snippet ===
> > > 
> > > QueryBuilder qb = QueryBuilders.fieldQuery(idField,  
> > > idValue);  
> > > SearchRequestBuilder srb = tclient.prepareSearch(index);  
> > > srb.setVersion(false);
> > > 
> > > srb.setSearchType(SearchType.QUERY\_AND\_FETCH);  
> > > srb.setQuery(qb);  
> > > srb.addFields(documentFields);  
> > > srb.addField("\_id");
> > > 
> > > ListenableActionFuture laf = srb.execute();
> > > 
> > > SearchResponse resp = laf.actionGet();
> > > 
> > > SearchHits hits = resp.getHits();
> > > 
> > > === End Snippet ===
> > > 
> > > ## What gives? I'm trying to do a query and then an update. With multiple versions being returned, I'm getting nasty results.
> > > 
> > > Ed Brown

---

<div class="post-metadata">

### Author: ![Ed\_Brown\_VSTI](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@Ed\_Brown\_VSTI](https://discuss.elastic.co/u/Ed_Brown_VSTI)
#### Post date: [April 11, 2012, 5:29pm UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298/5 "2012-04-11T17:29:34Z")

</div>

On Wednesday, April 11, 2012 12:58:10 PM UTC-4, Igor Motov wrote:

> I see. If you are not calling refresh after indexing, you might be  
> actually getting the old version. Elasticsearch doesn't save old version in  
> the long term, but it takes time (1sec by default) for an updated record to  
> appear in the search results. You should, probably, use realtime get  
> instead of search or update api.

When I used get(), the re-index of the document caused all fields to become  
arrays, except the new field added.

At least now I know I'm not losing my mind.

> On Wednesday, April 11, 2012 12:33:23 PM UTC-4, Ed Brown VSTI wrote:
> 
> > On Wednesday, April 11, 2012 11:58:55 AM UTC-4, Igor Motov wrote:
> > 
> > > Are you sure that you are getting multiple versions of the same document?
> > 
> > Yes. I'm printing out the index id and version number and I'm seeing the  
> > same index id and increasing version numbers. It is in a very tight loop  
> > where I query based on id and then update.
> > 
> > > Is it possible that you are getting different documents (with different  
> > > values of \_index, \_type or \_id fields) that just look similar?
> > 
> > No.
> > 
> > > Elasticsearch stores only the last version. setVersion doesn't control  
> > > saving/retrieving multiple versions; it just indicates if the version value  
> > > should be returned or not.
> > 
> > Then that makes what I'm seeing even stranger. The code goes:
> > 
> > Get id of document to update;  
> > Perform search using document id;  
> > Get document hit;  
> > For each document hit {  
> > Copy all fields returned;  
> > Put in new fields;  
> > Index each document hit;  
> > }
> > 
> > > On Wednesday, April 11, 2012 11:39:20 AM UTC-4, Ed Brown VSTI wrote:
> > > 
> > > > Hello,  
> > > > I'm using the SearchRequestBuilder to perform a search and I noticed  
> > > > I'm getting multiple versions of the same document returned. I then used  
> > > > setVersion(false), recompiled and ran the code again and I got the same  
> > > > result.  
> > > > A code snippet follows:
> > > > 
> > > > === Start Snippet ===
> > > > 
> > > > QueryBuilder qb = QueryBuilders.fieldQuery(idField,  
> > > > idValue);  
> > > > SearchRequestBuilder srb = tclient.prepareSearch(index);  
> > > > srb.setVersion(false);
> > > > 
> > > > srb.setSearchType(SearchType.QUERY\_AND\_FETCH);  
> > > > srb.setQuery(qb);  
> > > > srb.addFields(documentFields);  
> > > > srb.addField("\_id");
> > > > 
> > > > ListenableActionFuture laf = srb.execute();
> > > > 
> > > > SearchResponse resp = laf.actionGet();
> > > > 
> > > > SearchHits hits = resp.getHits();
> > > > 
> > > > === End Snippet ===
> > > > 
> > > > ## What gives? I'm trying to do a query and then an update. With multiple versions being returned, I'm getting nasty results.
> > > > 
> > > > Ed Brown

---

<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, 3:32am UTC](https://discuss.elastic.co/t/setversion-false-on-searchrequestbuilder-not-working/7298/6 "2017-07-06T03:32:54Z")

</div>


