ElasticSearch High Level REST API(1)文档基本操作

获取ES客户端

ES的提供了四种Java客户端,分别为节点客户端(node client)、传输客户端(Transport Client)、低级REST客户端、高级REST客户端。

节点客户端作为集群节点的一部分,在集群节点较多的情况下会影响集群的反应速度。

传输客户端客户端虽然实现了和集群的解耦,相对节点更“轻”,但是会在ES7.0或8.0之后被移除,被“高级Rest客户端”所取代。

如下为ES 客户端的创建:

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
@Configuration
public class ElasticClient {
private String host = "192.168.165.239";
private int port = 9200;

/**
* 获取Rest高级客户端
* @return
*/
@Bean
public RestHighLevelClient getRestHighLevelClient(){
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
return new RestHighLevelClient(builder);
}

/**
* 获取Rest低级客户端
* @return
*/
@Bean
public RestClient getRestClient(){
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
return builder.build();
}
}

下面介绍ES在Java中基本的文档增删改查操作

索引操作

索引即我们的新增操作,ES提供了三种形式的索引,分别为通过Json字符串、Map集合、XContentBuilder实现索引操作

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
@Resource
private RestHighLevelClient client;

@GetMapping("/index/create")
public String createIndex() {
IndexRequest indexRequest = new IndexRequest("test");
/*
//Json字符串作为数据源
String jsonString = "{" +
"\"name\": \"Joe.Ye\"," +
"\"email\": \"yezhou@yezhou.org\"," +
"\"homepage\": \"http://www.appblog.cn\"" +
"}";
indexRequest.source(jsonString, XContentType.JSON);
*/
//Map集合作为数据源
Map<String, Object> map = new HashMap<>();
map.put("name", "Joe.Ye");
map.put("age", 28);
map.put("homepage", "http://www.appblog.cn");
indexRequest.source(map);
/*
//XContentBuilder作为数据源
XContentBuilder builder = null;
try {
builder = XContentFactory.jsonBuilder();
builder.startObject();
builder.field("name", "Joe.Ye");
builder.timeField("date", new Date());
builder.field("homepage", "http://www.appblog.cn");
builder.endObject();
} catch (IOException e) {
e.printStackTrace();
}
indexRequest.source(builder);
*/
try {
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
log.info("Status: {}", indexResponse.status().getStatus());
//client.close();
return JSON.toJSONString(indexResponse);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
1
Status: 201
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
{
"fragment":false,
"id":"zGMq_WoBzwYnh4XWhOrH",
"index":"test",
"primaryTerm":1,
"result":"CREATED",
"seqNo":0,
"shardId":{
"fragment":true,
"id":-1,
"index":{
"fragment":false,
"name":"test",
"uUID":"_na_"
},
"indexName":"test"
},
"shardInfo":{
"failed":0,
"failures":[

],
"fragment":false,
"successful":1,
"total":2
},
"type":"_doc",
"version":1
}

查询文档

根据索引、ID查询文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@GetMapping("/index/get")
public String getIndex() {
GetRequest getRequest = new GetRequest(
"test",
"zGMq_WoBzwYnh4XWhOrH");
try {
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
Map<String, Object> source = getResponse.getSource();
if (source != null && !source.isEmpty()) {
for (Map.Entry<String, Object> entry : source.entrySet()) {
log.info("Key: {}, Value: {}", entry.getKey(), entry.getValue());
}
}
//client.close();
return JSON.toJSONString(getResponse);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
1
2
3
Key: name, Value: Joe.Ye
Key: email, Value: yezhou@yezhou.org
Key: homepage, Value: http://www.appblog.cn
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
{
"exists":true,
"fields":{

},
"fragment":false,
"id":"zGMq_WoBzwYnh4XWhOrH",
"index":"test",
"primaryTerm":1,
"seqNo":0,
"source":{
"name":"Joe.Ye",
"email":"yezhou@yezhou.org",
"homepage":"http://www.appblog.cn"
},
"sourceAsBytes":"eyJuYW1lIjoiSm9lLlllIiwiZW1haWwiOiJ5ZXpob3VAeWV6aG91Lm9yZyIsImhvbWVwYWdlIjoiaHR0cDovL3d3dy5hcHBibG9nLmNuIn0=",
"sourceAsBytesRef":{
"fragment":true
},
"sourceAsMap":{
"$ref":"$.source"
},
"sourceAsString":"{"name":"Joe.Ye","email":"yezhou@yezhou.org","homepage":"http://www.appblog.cn"}",
"sourceEmpty":false,
"sourceInternal":{
"$ref":"$.sourceAsBytesRef"
},
"type":"_doc",
"version":1
}

删除文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@GetMapping("/index/delete")
public String deleteIndex() {
DeleteRequest deleteRequest = new DeleteRequest("test", "DWMz_WoBzwYnh4XWA-s8");
deleteRequest.timeout(TimeValue.timeValueMinutes(10));
deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
try {
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
log.info("Status: {}", deleteResponse.status().getStatus());
//client.close();
return JSON.toJSONString(deleteResponse);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
1
Status: 200
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
{
"fragment":false,
"id":"DWMz_WoBzwYnh4XWA-s8",
"index":"test",
"primaryTerm":1,
"result":"DELETED",
"seqNo":6,
"shardId":{
"fragment":true,
"id":-1,
"index":{
"fragment":false,
"name":"test",
"uUID":"_na_"
},
"indexName":"test"
},
"shardInfo":{
"failed":0,
"failures":[

],
"fragment":false,
"successful":1,
"total":2
},
"type":"_doc",
"version":2
}

再次查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"exists":false,
"fields":{

},
"fragment":false,
"id":"DWMz_WoBzwYnh4XWA-s8",
"index":"test",
"primaryTerm":0,
"seqNo":-2,
"sourceEmpty":true,
"type":"_doc",
"version":-1
}

更新文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@GetMapping("/index/update")
public String updateIndex() {
UpdateRequest updateRequest = new UpdateRequest("test", "nWNC_WoBzwYnh4XWTeun");
Map<String, Object> map = new HashMap<>();
map.put("name", "Joe.Ye@AppBlog.CN");
map.put("age", 18);
updateRequest.doc(map, XContentType.JSON);
try {
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
log.info("Status: {}", updateResponse.status().getStatus());
//client.close();
return JSON.toJSONString(updateResponse);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
1
Status: 200
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
{
"fragment":false,
"id":"nWNC_WoBzwYnh4XWTeun",
"index":"test",
"primaryTerm":1,
"result":"UPDATED",
"seqNo":7,
"shardId":{
"fragment":true,
"id":-1,
"index":{
"fragment":false,
"name":"test",
"uUID":"_na_"
},
"indexName":"test"
},
"shardInfo":{
"failed":0,
"failures":[

],
"fragment":false,
"successful":1,
"total":2
},
"type":"_doc",
"version":2
}

查询文档是否存在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@GetMapping("/index/exists")
public String existsIndex() {
GetRequest getRequest = new GetRequest("test","zGMq_WoBzwYnh4XWhOrH");
try {
getRequest.fetchSourceContext(new FetchSourceContext(false)); //禁用获取 _source字段
getRequest.storedFields("_none_"); //禁用获取存储字段
boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
//client.close();
return String.valueOf(exists);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
1
true

Bulk批量操作

bulk可以批量执行多条操作语句,bulk支持批量操作不同的命令,例如批量更新和删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@GetMapping("/bulk")
public String bulk() {
Person person1 = new Person("Joe.Ye", 28, 175.2);
Person person2 = new Person("Xiong", 26, 165.5);
BulkRequest bulkRequest = new BulkRequest();
IndexRequest indexRequest1 = new IndexRequest("person").source(JSON.toJSONString(person1), XContentType.JSON);
IndexRequest indexRequest2 = new IndexRequest("person").source(JSON.toJSONString(person2), XContentType.JSON);
DeleteRequest deleteRequest = new DeleteRequest("test", "AGMx_WoBzwYnh4XWx-tL");
bulkRequest.add(indexRequest1);
bulkRequest.add(indexRequest2);
bulkRequest.add(deleteRequest);
try {
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
log.info("Status: {}", bulkResponse.status().getStatus());
//client.close();
return JSON.toJSONString(bulkResponse);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
1
Status: 200
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
154
155
{
"fragment":false,
"ingestTook":{
"days":0,
"daysFrac":-1.1574074074074074e-8,
"hours":0,
"hoursFrac":-2.7777777777777776e-7,
"micros":-1000,
"microsFrac":-1000,
"millis":-1,
"millisFrac":-1,
"minutes":0,
"minutesFrac":-0.000016666666666666667,
"nanos":-1000000,
"seconds":0,
"secondsFrac":-0.001,
"stringRep":"-1"
},
"ingestTookInMillis":-1,
"items":[
{
"failed":false,
"fragment":false,
"id":"n2Nh_WoBzwYnh4XWSuxi",
"index":"person",
"itemId":0,
"opType":"INDEX",
"response":{
"fragment":false,
"id":"n2Nh_WoBzwYnh4XWSuxi",
"index":"person",
"primaryTerm":1,
"result":"CREATED",
"seqNo":0,
"shardId":{
"fragment":true,
"id":-1,
"index":{
"fragment":false,
"name":"person",
"uUID":"_na_"
},
"indexName":"person"
},
"shardInfo":{
"failed":0,
"failures":[

],
"fragment":false,
"successful":1,
"total":2
},
"type":"_doc",
"version":1
},
"type":"_doc",
"version":1
},
{
"failed":false,
"fragment":false,
"id":"oGNh_WoBzwYnh4XWSuxi",
"index":"person",
"itemId":1,
"opType":"INDEX",
"response":{
"fragment":false,
"id":"oGNh_WoBzwYnh4XWSuxi",
"index":"person",
"primaryTerm":1,
"result":"CREATED",
"seqNo":1,
"shardId":{
"fragment":true,
"id":-1,
"index":{
"fragment":false,
"name":"person",
"uUID":"_na_"
},
"indexName":"person"
},
"shardInfo":{
"failed":0,
"failures":[

],
"fragment":false,
"successful":1,
"total":2
},
"type":"_doc",
"version":1
},
"type":"_doc",
"version":1
},
{
"failed":false,
"fragment":false,
"id":"AGMx_WoBzwYnh4XWx-tL",
"index":"test",
"itemId":2,
"opType":"DELETE",
"response":{
"fragment":false,
"id":"AGMx_WoBzwYnh4XWx-tL",
"index":"test",
"primaryTerm":1,
"result":"DELETED",
"seqNo":8,
"shardId":{
"fragment":true,
"id":-1,
"index":{
"fragment":false,
"name":"test",
"uUID":"_na_"
},
"indexName":"test"
},
"shardInfo":{
"failed":0,
"failures":[

],
"fragment":false,
"successful":1,
"total":2
},
"type":"_doc",
"version":2
},
"type":"_doc",
"version":2
}
],
"took":{
"days":0,
"daysFrac":0.000004780092592592592,
"hours":0,
"hoursFrac":0.00011472222222222222,
"micros":413000,
"microsFrac":413000,
"millis":413,
"millisFrac":413,
"minutes":0,
"minutesFrac":0.006883333333333333,
"nanos":413000000,
"seconds":0,
"secondsFrac":0.413,
"stringRep":"413ms"
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :