使用 Java 在 Elasticsearch 中执行按查询更新时的 NPE
NPE while executing Update By Query in Elasticsearch using Java
我在 Spring 引导应用程序中使用 Elasticsearch 2.4,我需要使用 Java API.[= 对远程 ES 实例执行 _update_by_query
请求。 37=]
我在 找到了完成此任务的方法,但就我的情况而言,我有一个 NPE 试图执行 .get()
函数。
包含一个 ES 模块:
compile 'org.elasticsearch.module:reindex:2.4.1'
这是我现在用于测试的代码片段:
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
这是一个包装好的 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());
}
需要包装器才能通过 Spring 启动配置文件进行配置,所以这只是为了方便。
NullPointerException
是调用了execute(...)
方法引起的
TransportProxyClient
:
final TransportActionNodeProxy<Request, Response> proxy = proxies.get(action); // no proxy found here
...
proxy.execute(node, request, listener); // NPE here
我想知道我是否遗漏了某些步骤,或者自从上述问题以来用法是否发生了变化?
您缺少传输信息的配置。它应该是
TransportClient.builder().addPlugin(ReindexPlugin.class)
.build().addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300));
我在 Spring 引导应用程序中使用 Elasticsearch 2.4,我需要使用 Java API.[= 对远程 ES 实例执行 _update_by_query
请求。 37=]
我在 .get()
函数。
包含一个 ES 模块:
compile 'org.elasticsearch.module:reindex:2.4.1'
这是我现在用于测试的代码片段:
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
这是一个包装好的 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());
}
需要包装器才能通过 Spring 启动配置文件进行配置,所以这只是为了方便。
NullPointerException
是调用了execute(...)
方法引起的
TransportProxyClient
:
final TransportActionNodeProxy<Request, Response> proxy = proxies.get(action); // no proxy found here
...
proxy.execute(node, request, listener); // NPE here
我想知道我是否遗漏了某些步骤,或者自从上述问题以来用法是否发生了变化?
您缺少传输信息的配置。它应该是
TransportClient.builder().addPlugin(ReindexPlugin.class)
.build().addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("127.0.0.1"), 9300));