elasticsearch api中的delete操作
官網:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-delete.html
The delete API allows one to delete a typed JSON document from a specificindex based on its id. The following example deletes the JSON documentfrom an index called twitter, under a type called tweet, with id valued1:
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").get();For more information on the delete operation, check out thedelete API docs.
Operation Threadingedit
The delete API allows to set the threading model the operation will beperformed when the actual execution of the API is performed on the samenode (the API is executed on a shard that is allocated on the sameserver).
The options are to execute the operation on a different thread, or toexecute it on the calling thread (note that the API is still async). Bydefault, operationThreaded is set to true which means the operationis executed on a different thread. Here is an example that sets it tofalse:
DeleteResponse response = client.prepareDelete("twitter", "tweet", "1").setOperationThreaded(false).get();demo源碼:
package es.documentApi;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class ElasticSearchDelete {
?? ?public static void main(String[] args) {
?? ??? ?try {
?? ??? ??? ?// 設置集群名稱
?? ??? ??? ?Settings settings = Settings.builder().put("cluster.name", "my-elasticsearch")
?? ??? ??? ??? ??? ?.put("client.transport.sniff", true).build();
?? ??? ??? ?// 創建client
?? ??? ??? ?TransportClient client = new PreBuiltTransportClient(settings)
?? ??? ??? ??? ??? ?.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.224.140"), 9300))
?? ??? ??? ??? ??? ?.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.224.141"), 9300))
?? ??? ??? ??? ??? ?.addTransportAddress(
?? ??? ??? ??? ??? ??? ??? ?new InetSocketTransportAddress(InetAddress.getByName("192.168.224.142"), 9300));
?? ??? ??? ?DeleteResponse response = client.prepareDelete("blog", "article", "AVvYUN6QAl9waYY55JSH").get();
?? ??? ??? ?/*DeleteResponse response1 = client.prepareDelete("blog", "article", "1")
?? ??? ??? ???????? .setOperationThreaded(false)
?? ??? ??? ???????? .get();*/
?? ??? ??? ?
?? ??? ??? ?Result res = response.getResult();
?? ??? ??? ?System.out.print(res);
?? ??? ??? ?client.close();
?? ??? ?} catch (UnknownHostException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
}
最后結果:
no modules loaded
loaded plugin [org.elasticsearch.index.reindex.ReindexPlugin]
loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
loaded plugin [org.elasticsearch.script.mustache.MustachePlugin]
loaded plugin [org.elasticsearch.transport.Netty3Plugin]
loaded plugin [org.elasticsearch.transport.Netty4Plugin]
DELETED
總結
以上是生活随笔為你收集整理的elasticsearch api中的delete操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: elasticsearch api中的g
- 下一篇: jquery ajax的例子