Commit 525fd3ca by 姚冰

[chg] 添加全天分析查询接口,添加personType字段

1 parent b7916465
......@@ -4,7 +4,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<version>2.5.15</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.viontech</groupId>
......
......@@ -5,14 +5,11 @@ import com.viontech.match.entity.vo.ResponseVo;
import com.viontech.match.service.PersonService;
import com.viontech.match.service.PoolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
/**
* .
......@@ -53,7 +50,7 @@ public class MainController {
return ResponseVo.commandNotFound(rid);
}
} catch (Exception e) {
log.error("", e);
log.error("match exception", e);
return ResponseVo.error(rid, e.getMessage());
}
}
......@@ -66,4 +63,9 @@ public class MainController {
return ResponseVo.error("error", e.getMessage());
}
}
@GetMapping("/compare")
public Object compare(@RequestParam(name = "poolId")List<String> poolId, @RequestParam(name = "unid")String unid, @RequestParam(name = "mallId")Long mallId, @RequestParam(name = "zoneId", required = false)List<Long> zoneId, @RequestParam(name = "gateId")List<Long> gateId, @RequestParam(name = "direction")List<Integer> direction, @RequestParam(name = "size")Integer size) throws IOException {
return personService.compare(poolId, unid, mallId, zoneId, gateId, direction,size);
}
}
package com.viontech.match.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
@JsonIgnoreProperties(ignoreUnknown = true)
@Data
public class PersonInfo {
private String unid;
......@@ -17,13 +19,15 @@ public class PersonInfo {
private String gender;
private Integer bodyType;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date counttime;
private String fid;
private String channelSerialNum;
private Long gateId;
private String direction;
private Integer personType;
public PersonInfo(String unid, String personId, Double[] body,Double[] data, Integer age, String gender, Integer bodyType, Date countTime, String fid, String channelSerialNum, Long gateId, String direction) {
public PersonInfo(String unid, String personId, Double[] body,Double[] data, Integer age, String gender, Integer bodyType, Date countTime, String fid, String channelSerialNum, Long gateId, String direction, Integer personType) {
this.unid = unid;
this.personId = personId;
this.body = body;
......@@ -37,5 +41,10 @@ public class PersonInfo {
this.gateId = gateId;
this.direction = direction;
this._score = "1";
this.personType = personType;
}
public PersonInfo() {
this._score = "1";
}
}
......@@ -16,7 +16,7 @@ public class SearchResultHit {
private String _score;
private Integer age;
private String gender;
private Integer bodyType;
private Integer body_type;
// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
private String counttime;
private String fid;
......
......@@ -257,6 +257,7 @@ public class PersonService {
String direction = person.getDirection();
//Long gateId = person.getGateId();
Integer bodyType = person.getBodyType();
Integer personType = 0;//person.getPersonType();
String personChannelSerialNum = person.getChannelSerialNum();
List<FaceFeature> faceFeatures = person.getFaceFeatures();
if (CollectionUtils.isNotEmpty(faceFeatures)) {
......@@ -273,7 +274,7 @@ public class PersonService {
// "counttime", personCountTime == null ? null : Constant.DATE_FORMAT.get().format(personCountTime),
// "channelSerialNum", personChannelSerialNum, "gateId", gateId, "direction", direction);
// bulkRequest.add(indexRequest);
operationList.add(new BulkOperation.Builder().index(new IndexOperation.Builder<PersonInfo>().index(poolId).document(new PersonInfo(unid, personId, feature, null, age, gender, bodyType, personCountTime, fid, personChannelSerialNum, gateId, direction)).build()).build());
operationList.add(new BulkOperation.Builder().index(new IndexOperation.Builder<PersonInfo>().index(poolId).document(new PersonInfo(unid, personId, feature, null, age, gender, bodyType, personCountTime, fid, personChannelSerialNum, gateId, direction,personType)).build()).build());
}
}
}
......@@ -303,7 +304,7 @@ public class PersonService {
// "counttime", counttime == null ? null : Constant.DATE_FORMAT.get().format(counttime)
// , "channelSerialNum", channelSerialNum, "gateId", gateId, "direction", direction);
// bulkRequest.add(indexRequest);
operationList.add(new BulkOperation.Builder().index(new IndexOperation.Builder<PersonInfo>().index(poolId).document(new PersonInfo(unid, personId, feature, null, age, gender, bodyType, personCountTime, fid, personChannelSerialNum, gateId, direction)).build()).build());
operationList.add(new BulkOperation.Builder().index(new IndexOperation.Builder<PersonInfo>().index(poolId).document(new PersonInfo(unid, personId, feature, null, age, gender, bodyType, personCountTime, fid, personChannelSerialNum, gateId, direction,personType)).build()).build());
}
}
}
......@@ -402,7 +403,6 @@ public class PersonService {
}
SearchRequest searchRequest = getSearchRequest(rid, poolId, 1, feature, person, 1, agg);
matchResult.addAll(match0(searchRequest, agg));
}
}
......@@ -572,7 +572,7 @@ public class PersonService {
p.setAge(hit.getAge());
p.setGender((String) hit.getGender());
p.setChannelSerialNum(hit.getChannelSerialNum());
p.setBodyType(hit.getBodyType());
p.setBodyType(hit.getBody_type());
p.setCaptureUnid(hit.getUnid());
p.setCounttime(Optional.ofNullable((String)hit.getCounttime())
......@@ -593,5 +593,106 @@ public class PersonService {
return persons;
}
public ResponseVo compare(List<String> poolId, String unid, Long mallId, List<Long> zoneId, List<Long> gateId, List<Integer> direction, Integer size){
String customIndex = poolId.stream().filter(v -> v.contains("custompool")).collect(Collectors.toList()).get(0);
SearchRequest getPerson = new SearchRequest.Builder()
.index(poolId)
.query(q -> q.term(t -> t.field("unid").value(unid)))
.ignoreUnavailable(true)
.size(1)
.build();
log.info("request:{}", getPerson);
try {
SearchResponse<PersonInfo> search = client.search(getPerson, PersonInfo.class);
if (search.hits().total().value() <= 0) {
log.info("未找到相应的人员:{}",unid);
return ResponseVo.error(unid, "相关人员不存在");
}
PersonInfo person = search.hits().hits().get(0).source();
if (person == null || person.getBody() == null) {
log.info("相应的人员无特征:{}",unid);
return ResponseVo.error(unid, "相关人员不存在!");
}
log.info("获取人员特征");
List<Float> feature = getFeature(person.getBody());
List<Query> queries = new ArrayList<>();
// if (mallId != null) {
// queries.add(Query.of(q -> q.term(t -> t.field("mallId").value(mallId))));
// }
if (zoneId != null) {
// queries.add(Query.of(q -> q.term(t -> t.field("zoneId").value(zoneId))));
}
if (gateId != null && !gateId.isEmpty()) {
// queries.add(Query.of(q -> q.term(t -> t.field("gateId").value(gateId))));
BoolQuery boolQuery = new BoolQuery.Builder()
.should(Query.of(q -> q.terms(t -> t.field("gateId").terms(v -> v.value(gateId.stream().map(FieldValue::of).collect(Collectors.toList()))))),
new BoolQuery.Builder().mustNot(q -> q.exists(e -> e.field("gateId"))).build()._toQuery())
// .mustNot(Query.of(q -> q.exists(e -> e.field("gateId"))))
.minimumShouldMatch("1")
.build();
queries.add(boolQuery._toQuery());
}
if (direction != null && !direction.equals("")) {
BoolQuery boolQuery = new BoolQuery.Builder()
.should(Query.of(q -> q.terms(t -> t.field("direction").terms(v -> v.value(direction.stream().map(FieldValue::of).collect(Collectors.toList()))))),
new BoolQuery.Builder().mustNot(q -> q.exists(e -> e.field("direction"))).build()._toQuery())
.minimumShouldMatch("1")
.build();
queries.add(boolQuery._toQuery());
// queries.add(Query.of(q -> q.terms(t -> t.field("direction").terms(v -> v.value(direction.stream().map(FieldValue::of).collect(Collectors.toList()))))));
}
KnnQuery query = new KnnQuery.Builder()
.filter(queries)
.field("body").queryVector(feature)
.build();
String[] FETCH_SOURCE = new String[]{"personId", "unid"};
List<Query> mustList = new ArrayList<>();
mustList.add(query._toQuery());
BoolQuery boolQuery = new BoolQuery.Builder()
.must(mustList)
.mustNot(Query.of( q -> q.term(t -> t.field("unid").value(unid))))
.build();
SourceConfig sourceConfig = new SourceConfig.Builder().filter(s -> s.includes(Arrays.asList(FETCH_SOURCE))).build();
SearchRequest request = new SearchRequest.Builder()
.index(poolId)
.query(boolQuery._toQuery())
.source(sourceConfig)
.ignoreUnavailable(true)
.size(size).build();
log.info("rid:{} poolId:{} 开始匹配", unid, poolId);
SearchResponse<SearchResultHit> response = client.search(request, SearchResultHit.class);
log.info("rid:{} poolId:{} 匹配结果:{}", unid, poolId, response.hits().hits().size());
List<Hit<SearchResultHit>> resultList = response.hits().hits();
List<Person> persons = new ArrayList<>();
for (Hit<SearchResultHit> item : resultList) {
SearchResultHit hit = item.source();
Person p = new Person();
p.setPersonId((String) hit.getPersonId());
// p.setAge(hit.getAge());
// p.setGender((String) hit.getGender());
// p.setChannelSerialNum(hit.getChannelSerialNum());
// p.setBodyType(hit.getBody_type());
p.setCaptureUnid(hit.getUnid());
p.setCounttime(Optional.ofNullable((String)hit.getCounttime())
.map(x -> {
try {
return Constant.DATE_FORMAT.get().parse(x);
} catch (ParseException e) {
return null;
}
})
.orElse(null));
p.setScore(item.score().floatValue() * 100);
p.setPersonPoolId(item.index());
persons.add(p);
}
ResponseVo success = ResponseVo.success(unid, "success");
success.setMatchBodies(persons);
return success;
} catch (IOException e) {
log.error("search exception", e);
return ResponseVo.error(unid, "异常");
}
}
}
......@@ -59,9 +59,9 @@ public class PoolService {
// @Value("${vion.index.refresh_interval}")
// private String refreshInterval;
@Value("${vion.index.translog.sync_interval}")
@Value("${vion.index.translog.sync_interval:30s}")
private String translogSyncInterval;
@Value("${vion.index.merge.scheduler.max_thread_count}")
@Value("${vion.index.merge.scheduler.max_thread_count:50}")
private Integer mergeThreadCount;
/**
......@@ -307,6 +307,7 @@ public class PoolService {
properties1.put("gender", new Property.Builder().keyword(k -> k.index(false).docValues(false)).build());
properties1.put("counttime", new Property.Builder().date(d -> d.format("yyyy-MM-dd HH:mm:ss")).build());
properties1.put("unid", new Property.Builder().keyword(k -> k.index(true)).build());
properties1.put("personType", new Property.Builder().keyword(k -> k.index(true)).build());
// builder.properties(properties1);
TypeMapping.Builder builder = new TypeMapping.Builder().properties(properties1);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!