PoolService.java
10.3 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.viontech.match.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.viontech.keliu.model.Person;
import com.viontech.match.config.Constant;
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.apache.http.HttpEntity;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
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.io.InputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* .
*
* @author 谢明辉
* @date 2020/11/27
*/
@Service
@Slf4j
public class PoolService {
@Resource
private PersonService personService;
@Resource
private RestHighLevelClient client;
@Resource
private ObjectMapper objectMapper;
/**
* 添加特征池
*/
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 = getCreateIndexContentBuilder();
createIndexRequest.mapping(builder);
Settings.Builder setting = Settings.builder();
setting.put("index.number_of_shards", 1);
setting.put("index.number_of_replicas", 0);
createIndexRequest.settings(setting);
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 && e.getMessage().contains(Constant.STR_POOL_ID_ALREADY_EXISTS)) {
return ResponseVo.error(rid, 20, "already exit poolId");
} else {
throw e;
}
}
}
/**
* 删除特征池
*/
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.poolIdNotExists(rid);
} 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);
if (!client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT)) {
return ResponseVo.poolIdNotExists(rid);
}
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;
log.info("查询特征池操作开始:[{}],rid:[{}]", poolId, rid);
if (listAll != 0) {
poolInfos = queryPoolInfo(null);
} else {
if (!existPool(poolId)) {
return ResponseVo.poolIdNotExists(rid);
}
poolInfos = queryPoolInfo(poolId);
}
ResponseVo success = ResponseVo.success(rid, "success");
success.setPoolIds(poolInfos);
log.info("查询特征池操作完成:[{}],rid:[{}]", poolId, rid);
return success;
} catch (ElasticsearchStatusException e) {
log.error("queryPool", e);
return ResponseVo.error(rid, e.getDetailedMessage());
}
}
public void refreshPool(String... poolIds) throws IOException {
RefreshRequest refreshRequest = new RefreshRequest(poolIds);
RefreshResponse refresh = client.indices().refresh(refreshRequest, RequestOptions.DEFAULT);
log.info("刷新索引:{},成功:{},失败:{}", poolIds, refresh.getSuccessfulShards(), refresh.getFailedShards());
}
public XContentBuilder getCreateIndexContentBuilder() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
// 人脸特征
builder.startObject("data");
{
builder.field("type", "dense_vector");
builder.field("dims", Constant.FACE_FEATURE_DIMS);
}
builder.endObject();
// 人体特征
builder.startObject("body");
{
builder.field("type", "dense_vector");
builder.field("dims", Constant.BODY_FEATURE_DIMS);
}
builder.endObject();
// 人员id
builder.startObject("personId");
{
builder.field("type", "keyword");
}
builder.endObject();
// 特征id
builder.startObject("fid");
{
builder.field("type", "text");
}
builder.endObject();
builder.startObject("age");
{
builder.field("type", "integer");
builder.field("doc_values", false);
builder.field("index", false);
}
builder.endObject();
builder.startObject("gender");
{
builder.field("type", "keyword");
builder.field("doc_values", false);
builder.field("index", false);
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
return builder;
}
public boolean existPool(String poolId) throws IOException {
return client.indices().exists(new GetIndexRequest(poolId), RequestOptions.DEFAULT);
}
public List<PoolInfo> queryPoolInfo(String poolId) throws IOException {
String endPoint = poolId == null ? "/_stats" : "/" + poolId + "/_stats";
LinkedList<PoolInfo> poolInfos = new LinkedList<>();
RestClient lowLevelClient = client.getLowLevelClient();
Request request = new Request("GET", endPoint);
Response response = lowLevelClient.performRequest(request);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
HashMap responseMap = objectMapper.readValue(content, HashMap.class);
HashMap<String, HashMap> indices1 = (HashMap<String, HashMap>) responseMap.get("indices");
for (Map.Entry<String, HashMap> entry : indices1.entrySet()) {
String id = entry.getKey();
HashMap value = entry.getValue();
HashMap<String, HashMap> primaries = (HashMap<String, HashMap>) value.get("primaries");
HashMap docs = primaries.get("docs");
Integer count = (Integer) docs.get("count");
PoolInfo poolInfo = new PoolInfo();
poolInfo.setPersonCount(count.longValue());
poolInfo.setPoolId(id);
poolInfos.add(poolInfo);
}
return poolInfos;
}
}