NPE while executing Update By Query in Elasticsearch using Java

て烟熏妆下的殇ゞ 提交于 2019-12-22 14:16:37

问题


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?


回答1:


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));



来源:https://stackoverflow.com/questions/40399689/npe-while-executing-update-by-query-in-elasticsearch-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!