我在Spring Boot应用程序中使用Elasticsearch 2.4,我需要_update_by_query使用Java API 执行对远程ES实例的请求。 我已经找到了解决这个问题的方法,但是就我而言,我有一个NPE试图执行该.get()功能。
_update_by_query
.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豆:
Client
@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 Boot配置文件进行配置,因此只是为了方便。
在NullPointerException通过调用引起execute(...)的方法 TransportProxyClient:
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));
TransportClient.builder().addPlugin(ReindexPlugin.class) .build().addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("127.0.0.1"), 9300));