Using UpdateByQueryRequest in java client

621 Views Asked by At

I am trying to use UpdateByQueryRequest in java. The problem is, I am getting different syntax suggestion in eclipse. I am using java RestHighLevelClient. Here is an example

import org.elasticsearch.index.reindex.UpdateByQueryRequest;
SearchRequest searchRequest = new SearchRequest(index).types(type);

There are 2 suggestions I am getting. First one is without SearchRequest object and second one is with SearchRequest object

UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest();
UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(searchRequest);

I am unable to use setQuery like used everywhere else in ES. On the other hand setScript works

updateByQueryRequest.setQuery()

Is there some other syntax to do this?

1

There are 1 best solutions below

0
On BEST ANSWER

You can also use the internal implementation of setQuery():

public UpdateByQueryRequest setQuery(QueryBuilder query) {
  if (query != null) {
    this.getSearchRequest().source().query(query);
  }

  return this;
}

Your final code will look like this:

UpdateByQueryRequest updateByQueryRequest = new UpdateByQueryRequest(searchRequest);
if (query != null) {
   updateByQueryRequest.getSearchRequest().source().query(query);
}

If you don't have access to updateByQueryRequest.getSearchRequest(), maybe the problem is in the library or your IDE, you can try to decompile the class and check if the method exists (ElasticSearch is open source, so it won't be a possible legal problem), also I recommend you to upload the library or download it again and check if the problem persists.