Elasticsearch 2.0:如何通过Java中的查询进行删除 [英] Elasticsearch 2.0: how to delete by query in Java
问题描述
我正在尝试升级到ES 2.0。我已经下载了ES 2.0并将其安装在我的Windows机器上。
在我的pom.xml中,我有以下内容:
<依赖关系>
< groupId> org.elasticsearch< / groupId>
< artifactId> elasticsearch< / artifactId>
< version> 2.0.0-rc1< / version>
< / 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服务器中安装一个按照查询的按钮插件吗?
感谢任何指针!
更新
建议,这里是我现在所在:
首先,创建客户端时,使设置如下所示:
设置设置= Settings.settingsBuilder()
.put(cluster.name,mycluster)
.put(plugin.types ,DeleteByQueryPlugin.class.getName())
.build();
第二,在逐个查询的地方:
DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(客户端,DeleteByQueryAction.INSTANCE)
.setIndices(myindex)
.setTypes(mydoctype)
.setSource(b.toString())
.execute()
.actionGet();
我还通过在ES的根目录中运行以下内容,通过查询插件安装删除:
bin\plugin install by-query
如果我不安装此插件,我会收到错误。
在所有这些步骤之后,ES相关的零件工作正常。 p>
我相信你可以使用这个:
DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(客户端,DeleteByQueryAction.INSTANCE)
.setTypes(mydocytype)
.setSource(b.toString())
.execute()
.actionGet();
您必须在您的设置中添加插件类型:
设置设置= Settings.settingsBuilder()
.put(plugin.types,DeleteByQueryPlugin.class.getName())
如果您有远程服务器,则必须安装插件。
I am trying to upgrade to ES 2.0. I have downloaed ES 2.0 and installed it on my Windows machine.
In my pom.xml, I have the following:
<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>
In my Java code, I did delete by query in the following way when using 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();
I am hoping to replace this:
DeleteByQueryResponse response = client.prepareDeleteByQuery("myindex")
.setTypes("mydocytype")
.setSource(b.toString())
.execute()
.actionGet();
with ES 2.0 way. Googled but failed to find an example for it. The online API documentation seems too abstract to me. How can I do it?
Another question: Do I have to install delete-by-query plugin in Elasticsearch server?
Thanks for any pointer!
UPDATE
I followed Max's suggestion, and here is what I have now:
First, when create the client, make settings look like the following:
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "mycluster")
.put("plugin.types", DeleteByQueryPlugin.class.getName())
.build();
Second, at the place doing delete-by-query:
DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
.setIndices("myindex")
.setTypes("mydoctype")
.setSource(b.toString())
.execute()
.actionGet();
I also installed delete by query plugin by running the following in the root directory of ES:
bin\plugin install delete-by-query
I get errors if I do not install this plugin.
After all these steps, ES related parts work just fine.
I believe you can use this:
DeleteByQueryResponse rsp = new DeleteByQueryRequestBuilder(client, DeleteByQueryAction.INSTANCE)
.setTypes("mydocytype")
.setSource(b.toString())
.execute()
.actionGet();
You have to add plugin type to your settings:
Settings settings = Settings.settingsBuilder()
.put("plugin.types", DeleteByQueryPlugin.class.getName())
If you have remote server you have to install the plugin.
这篇关于Elasticsearch 2.0:如何通过Java中的查询进行删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!