admin
2020-05-06 24a8d17e007545f7426c48352109aa1a9c6587ee
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package org.fanli.elastic;
 
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;
import org.yeshi.utils.JsonUtil;
 
public class ADDUtils {
 
    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 add1() {
        IndexRequest request = new IndexRequest("posts");
        request.id("1");
        String jsonString = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\","
                + "\"message\":\"trying out Elasticsearch\"" + "}";
        request.source(jsonString, XContentType.JSON);
        
        try {
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public void add2() {
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("user", "kimchy");
        jsonMap.put("postDate", new Date());
        jsonMap.put("message", "trying out Elasticsearch");
        IndexRequest request = new IndexRequest("posts").id("1").source(jsonMap);
        
        /* 可选参数 */
        // 路由
        request.routing("routing"); 
        
        // 超时
        request.timeout(TimeValue.timeValueSeconds(1)); 
        request.timeout("1s"); 
        
        // 刷新策略
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); 
        request.setRefreshPolicy("wait_for");    
        
        // 版本要求
        request.version(2);
        // 版本类型
        request.versionType(VersionType.EXTERNAL);
        
        // 操作类型
        request.opType(DocWriteRequest.OpType.CREATE); 
        request.opType("create"); 
        
        // 索引文档之前要执行的线的名称
        request.setPipeline("pipeline");
    }
 
    public static void add3() {
        // 提供为的文档源,该源Map自动转换为JSON格式
        try {
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                builder.field("user", "kimchy2");
                builder.timeField("postDate", new Date());
                builder.field("message", "trying ");
            }
            builder.endObject();
            IndexRequest request = new IndexRequest("posts").id("2").source(builder);
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
 
//        // 作为Object密钥对提供的文档源,被转换为JSON格式
//        IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "kimchy", "postDate", new Date(),
//                "message", "trying out Elasticsearch");
    }
 
    
    public static void add4() {
//        Product product = new Product();
//        product.setId("995");
//        product.setName("测试中的5");
//        product.setPrice("494985");
//        product.setDetail("测试进欧冠任何5");
//        
        String json = JsonUtil.getSimpleGson().toJson("");
        
        IndexRequest request = new IndexRequest("shose").id("1");
        request.source(json, XContentType.JSON);
        
        
        
        // 同步执行
        try {
            IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
//        // 异步执行
//        ActionListener listener = new ActionListener<IndexResponse>() {
//            @Override
//            public void onResponse(IndexResponse indexResponse) {
//                
//            }
//
//            @Override
//            public void onFailure(Exception e) {
//                
//            }
//        };
//        client.indexAsync(request, RequestOptions.DEFAULT, listener);
    }
    
    public static void main(String[] args) {
//        add1();
        
        add3();
        
//        add4();
        System.out.println("添加成功");
    }
    
}