yujian
2020-01-18 f4a0f2acc63d7785eab108419a4e16f5f688cb95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.fanli.elastic;
 
import java.io.IOException;
 
import org.apache.http.HttpHost;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
 
public class DeleteUtils {
 
    private static String clusterName = "my-application";
    private static String host = "192.168.1.200";
    private static Integer port = 9200;
 
    // 相当于数据库名称
    public static String indexName = "shose";
 
    // 初始化api客户端
    public static RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost(host, port, "http")));
 
    public static void get1() {
        DeleteRequest request = new DeleteRequest("posts", "2");
 
//        // 可选参数
//        request.timeout(TimeValue.timeValueMinutes(2)); 
//        request.timeout("2m"); 
//        
//        // 策略
//        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); 
//        request.setRefreshPolicy("wait_for");  
//        
//        // 版本
//        request.version(2); 
//        request.versionType(VersionType.EXTERNAL);
 
        try {
            DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
            
            
//            
//            // 返回的值DeleteResponse允许检索有关已执行操作的信息,
//            String index = deleteResponse.getIndex();
//            String id = deleteResponse.getId();
//            long version = deleteResponse.getVersion();
//            ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
//            if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
//                
//                // 处理成功分片数量少于总分片数量的情况
//            }
//            if (shardInfo.getFailed() > 0) {
//                for (ReplicationResponse.ShardInfo.Failure failure :
//                        shardInfo.getFailures()) {
//                    String reason = failure.reason(); 
//                }
//                // 处理潜在的故障
//            }
//            
//            //  可以检查是否找到了该文档:
//            DeleteRequest request = new DeleteRequest("posts", "does_not_exist");
//            DeleteResponse deleteResponse = client.delete(
//                    request, RequestOptions.DEFAULT);
//            if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
//                
//            }
//            
//            try {
//                DeleteResponse deleteResponse = client.delete(
//                    new DeleteRequest("posts", "1").setIfSeqNo(100).setIfPrimaryTerm(2),
//                        RequestOptions.DEFAULT);
//            } catch (ElasticsearchException exception) {
//                if (exception.status() == RestStatus.CONFLICT) {
//                    
//                }
//            }
//            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    public static void main(String[] args) {
        get1();
 
        System.out.println("------------ 测试结束 -------------------------");
    }
 
}