我正在尝试升级到ES 2.0。我已经下载了ES 2.0并将其安装在我的Windows机器上。
在我的pom.xml中,我有以下内容:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.0.0-rc1</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>delete-by-query</artifactId>
<version>2.0.0-rc1</version>
</dependency>
在我的Java代码中,使用ES 1.7.3时,我按以下方式通过查询删除:
StringBuilder b = new StringBuilder("");
b.append("{");
b.append(" \"query\": {");
b.append(" \"term\": {");
b.append(" \"category\": " + category_value );
b.append(" }");
b.append(" }");
b.append("}");
client = getClient();
DeleteByQueryResponse response = client.prepareDeleteByQuery("myindex")
.setTypes("mydocytype")
.setSource(b.toString())
.execute()
.actionGet();
我希望能够取代它:
DeleteByQueryResponse response = client.prepareDeleteByQuery("myindex")
.setTypes("mydocytype")
.setSource(b.toString())
.execute()
.actionGet();
采用ES 2.0方式。谷歌搜索但没有找到它的例子。在线API文档对我来说太抽象了。我该怎么办?
另一个问题:我是否必须在Elasticsearch服务器中安装delete-by-query插件?
感谢任何指针!
更新
我遵循了Max的建议,这就是我现在所拥有的:
首先,在创建客户端时,使设置如下所示:
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "mycluster")
.put("plugin.types", DeleteByQueryPlugin.class.getName())
.build();
其次,在按查询删除的地方:
DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
.setIndices("myindex")
.setTypes("mydoctype")
.setSource(b.toString())
.execute()
.actionGet();
我还通过在ES的根目录中运行以下命令来安装查询插件:
bin\plugin install delete-by-query
如果我不安装此插件,我会收到错误。
完成所有这些步骤后,ES相关部分的工作正常。
答案 0 :(得分:12)
plugin.types
。因此,接受的解决方案将导致NullPointerException
。
解决方案是使用addPlugin
方法:
Client client = TransportClient.builder().settings(settings())
.addPlugin(DeleteByQueryPlugin.class)
.build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host",9300));
答案 1 :(得分:10)
我相信你可以用这个:
DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
.setTypes("mydocytype")
.setSource(b.toString())
.execute()
.actionGet();
您必须在设置中添加插件类型:
Settings settings = Settings.settingsBuilder()
.put("plugin.types", DeleteByQueryPlugin.class.getName())
如果您有远程服务器,则必须安装插件。
答案 2 :(得分:5)
从弹性5开始......
final BulkIndexByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(super.transportClient)
.filter(
QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("_type", "MY_TYPE")) // Trick to define and ensure the type.
.must(QueryBuilders.termQuery("...", "...")))
.source("MY_INDEX")
.get();
return response.getDeleted() > 0;
答案 3 :(得分:2)
首先: 添加elasticsearch-2.3.3 / plugins / delete-by-query / delete-by-query-2.3.3.jar来构建路径。
然后:
Client client = TransportClient.builder().settings(settings)
.addPlugin(DeleteByQueryPlugin.class)
.build()
.addTransportAddress(new InetSocketTransportAddress(
InetAddress.getByName("192.168.0.224"), 9300));