简体   繁体   中英

NPE while executing Update By Query in Elasticsearch using Java

I'm using Elasticsearch 2.4 in a Spring Boot application and I need to execute _update_by_query request to the remote ES instance using Java API.
I've found the way to accomplish this task at this question , but as for my case I've got an NPE trying to execute the .get() function.

A module for ES included:

compile 'org.elasticsearch.module:reindex:2.4.1'

Here's a code snippet I use for testing right now:

UpdateByQueryRequestBuilder request = UpdateByQueryAction.INSTANCE.newRequestBuilder(clientWrapper.getClient());  // clientWrapper is a bean and gets injected
Script script = new Script(
  "ctx._source.testName = \"TEST HAPPENED\"",
  ScriptService.ScriptType.INLINE, null, null);

request.source().setTypes("type");

BulkIndexByScrollResponse r = request
  .source(ES_INDEX_NAME)
  .filter(QueryBuilders.termQuery("testId", "Sk9lzQHdJT0"))
  .script(script)
  .get();  // the exception gets raised here

Here's a wrapped Client bean:

@Bean
public ClientWrapper elasticsearchClient(Client client) {
return new ClientWrapper(
  TransportClient.builder()
    .addPlugin(ReindexPlugin.class)  // here's the plugin that is supposed to work
    .settings(client.settings())
    .build());
}

Wrapper is needed to allow configuring via Spring Boot configuration files, so it's just for convenience.

The NullPointerException is caused by invoking the execute(...) method of the TransportProxyClient :

final TransportActionNodeProxy<Request, Response> proxy = proxies.get(action);  // no proxy found here
... 
proxy.execute(node, request, listener);  // NPE here

I wonder if I missed some step or maybe the usage has changed since the question mentioned above?

you are missing config for transport information. It should be TransportClient.builder().addPlugin(ReindexPlugin.class) .build().addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("127.0.0.1"), 9300));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM