setVersion(false) on SearchRequestBuilder Not Working


(Ed Brown VSTI) #1

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


(Igor Motov) #2

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


(Ed Brown VSTI) #3

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


(Igor Motov) #4

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


(Ed Brown VSTI) #5

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


(system) #6