PoolService.java
8.04 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.viontech.match.service;
import com.viontech.keliu.model.Person;
import com.viontech.match.entity.PoolInfo;
import com.viontech.match.entity.vo.RequestVo;
import com.viontech.match.entity.vo.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.core.CountRequest;
import org.elasticsearch.client.core.CountResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.rest.RestStatus;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
@Service
@Slf4j
public class PoolService {
@Resource
private PersonService personService;
@Resource
private RestHighLevelClient client;
/**
* 添加特征池
*/
public ResponseVo createPool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
String poolId = requestVo.getPoolId();
log.info("特征池创建操作开始:[{}}", poolId);
try {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(poolId);
XContentBuilder builder = getCreateIndexXContentBuilder();
createIndexRequest.mapping(builder);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
List<Person> personPool = requestVo.getPersonPool();
if (personPool != null && personPool.size() > 0) {
BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool);
if (bulkItemResponses != null) {
log.info(bulkItemResponses.buildFailureMessage());
}
}
log.info("特征池创建操作完成:[{}]", poolId);
return ResponseVo.success(rid);
} catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.BAD_REQUEST) {
return ResponseVo.error(rid, 20, "already exit poolId");
} else {
log.error("createPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
}
/**
* 删除特征池
*/
public ResponseVo deletePool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
Integer flushPool = requestVo.getFlushPool();
String poolId = requestVo.getPoolId();
log.info("特征池删除操作开始:[{}],FlushPool:[{}]", poolId, flushPool);
try {
if (0 == flushPool) {
List<HashMap<String, String>> personIds = requestVo.getPersonIds();
for (HashMap<String, String> item : personIds) {
String personId = item.get("personId");
if (personId != null) {
personService.deletePerson(poolId, personId);
}
}
} else if (flushPool == 1) {
AcknowledgedResponse delete = client.indices().delete(new DeleteIndexRequest(poolId), RequestOptions.DEFAULT);
}
log.info("特征池删除操作完成:[{}],FlushPool:[{}]", poolId, flushPool);
return ResponseVo.success(rid);
} catch (ElasticsearchStatusException e) {
if (e.status() == RestStatus.NOT_FOUND) {
return ResponseVo.error(rid, 4, "pollID not exist");
} else {
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
}
/**
* 修改特征池(添加人员)
*/
public ResponseVo modifyPool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
String poolId = requestVo.getPoolId();
List<Person> personPool = requestVo.getPersonPool();
Integer updateType = requestVo.getUpdateType();
log.info("特征池修改操作开始:[{}],updateType:[{}]", poolId, updateType);
try {
BulkResponse bulkItemResponses = personService.addPerson(poolId, personPool);
if (bulkItemResponses != null && bulkItemResponses.hasFailures()) {
log.info(bulkItemResponses.buildFailureMessage());
}
log.info("特征池修改操作完成:[{}],updateType:[{}]", poolId, updateType);
return ResponseVo.success(rid, "success");
} catch (ElasticsearchStatusException e) {
log.error("modifyPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
/**
* 查询特征池信息
*/
public ResponseVo queryPool(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
Integer listAll = requestVo.getListAll();
String poolId = requestVo.getPoolId();
try {
List<PoolInfo> poolInfos = new ArrayList<>();
if (listAll != 0) {
poolId = "*";
}
log.info("查询特征池操作开始:[{}],ListAll:[{}]", poolId, listAll);
GetIndexResponse response = client.indices().get(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
String[] indices = response.getIndices();
for (String index : indices) {
PoolInfo poolInfo = new PoolInfo();
poolInfo.setPoolId(index);
CountRequest countRequest = new CountRequest(index);
CountResponse count = client.count(countRequest, RequestOptions.DEFAULT);
poolInfo.setPersonCount(count.getCount());
poolInfos.add(poolInfo);
}
ResponseVo success = ResponseVo.success(rid, "success");
success.setPoolIds(poolInfos);
log.info("查询特征池操作完成:[{}],ListAll:[{}]", poolId, listAll);
return success;
} catch (ElasticsearchStatusException e) {
log.error("queryPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
public XContentBuilder getCreateIndexXContentBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("data");
{
builder.field("type", "dense_vector");
builder.field("dims", 512);
}
builder.endObject();
builder.startObject("personId");
{
builder.field("type", "keyword");
}
builder.endObject();
builder.startObject("fid");
{
builder.field("type", "text");
}
builder.endObject();
builder.startObject("age");
{
builder.field("type", "integer");
}
builder.endObject();
builder.startObject("gender");
{
builder.field("type", "keyword");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
return builder;
}
public boolean existPool(String poolId) throws IOException {
return client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
}
}