Commit 24f11a6b by 李乾广

[add]添加gateId,direction,添加比对符合条件的数量

1 parent fea2fd70
...@@ -5,7 +5,11 @@ import com.viontech.match.entity.vo.ResponseVo; ...@@ -5,7 +5,11 @@ import com.viontech.match.entity.vo.ResponseVo;
import com.viontech.match.service.PersonService; import com.viontech.match.service.PersonService;
import com.viontech.match.service.PoolService; import com.viontech.match.service.PoolService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*; 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 javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
...@@ -43,6 +47,8 @@ public class MainController { ...@@ -43,6 +47,8 @@ public class MainController {
return personService.matchPerson(requestVo); return personService.matchPerson(requestVo);
case UpdatePerson: case UpdatePerson:
return personService.updatePerson(requestVo); return personService.updatePerson(requestVo);
case CountMatchPerson:
return personService.countMatchPerson(requestVo);
default: default:
return ResponseVo.commandNotFound(rid); return ResponseVo.commandNotFound(rid);
} }
......
...@@ -34,6 +34,9 @@ public class ResponseVo { ...@@ -34,6 +34,9 @@ public class ResponseVo {
private List<Person> matchPersons; private List<Person> matchPersons;
private List<Person> matchBodies; private List<Person> matchBodies;
//获取满足条件的数量 CountMatchPerson时有效
private long count;
public ResponseVo(String rid) { public ResponseVo(String rid) {
this.rid = rid; this.rid = rid;
} }
......
...@@ -13,5 +13,6 @@ public enum CommandEnum { ...@@ -13,5 +13,6 @@ public enum CommandEnum {
ModifyPersonPool, ModifyPersonPool,
QueryPersonPool, QueryPersonPool,
MatchPerson, MatchPerson,
UpdatePerson UpdatePerson,
CountMatchPerson
} }
...@@ -17,6 +17,7 @@ import org.elasticsearch.action.bulk.BulkResponse; ...@@ -17,6 +17,7 @@ import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentType;
...@@ -39,6 +40,8 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms; ...@@ -39,6 +40,8 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.Max; import org.elasticsearch.search.aggregations.metrics.Max;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.ParsedValueCount;
import org.elasticsearch.search.aggregations.metrics.ValueCountAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -50,6 +53,7 @@ import java.util.Arrays; ...@@ -50,6 +53,7 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
...@@ -73,6 +77,65 @@ public class PersonService { ...@@ -73,6 +77,65 @@ public class PersonService {
private PoolService poolService; private PoolService poolService;
/** /**
* 符合条件的数量
* @param requestVo
* @return
* @throws Exception
*/
public ResponseVo countMatchPerson(RequestVo requestVo) throws Exception {
String rid = requestVo.getRid();
String poolId = requestVo.getPersonPoolId();
List<String> unionPersonPoolId = requestVo.getUnionPersonPoolId();
Person person = requestVo.getPerson();
List<String> poolIds = new ArrayList<>();
if (unionPersonPoolId != null && unionPersonPoolId.size() > 0) {
poolIds.addAll(unionPersonPoolId);
}
if (poolId != null) {
poolIds.add(poolId);
}
log.info("人员匹配操作开始,PoolIds:[{}]", poolIds);
try {
String[] indices = poolIds.toArray(new String[poolIds.size()]);
SearchRequest request = new SearchRequest(indices);
IndicesOptions defaultIndicesOptions = request.indicesOptions();
EnumSet<IndicesOptions.Option> options = defaultIndicesOptions.getOptions();
options.add(IndicesOptions.Option.IGNORE_UNAVAILABLE);
EnumSet<IndicesOptions.WildcardStates> expandWildcards = defaultIndicesOptions.getExpandWildcards();
IndicesOptions newIndicesOptions = new IndicesOptions(options, expandWildcards);
request.indicesOptions(newIndicesOptions);
long count = 0;
List<BodyFeature> bodyFeatures = person.getBodyFeatures();
if (CollectionUtils.isEmpty(bodyFeatures)) {
log.info("CountMatchPerson人体特征为空,PoolIds:[{}],count:{}", poolIds, count);
} else {
Double[] feature = bodyFeatures.get(0).getFeature();
SearchSourceBuilder builder = getSearchSourceBuilder(feature, person, 1);
ValueCountAggregationBuilder countAggregationBuilder = AggregationBuilders.count("countId").field("_id");
builder.aggregation(countAggregationBuilder);
request.source(builder);
log.debug("CountMatchPerson条件{}:{}", indices, builder.toString());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
ParsedValueCount countId = response.getAggregations().get("countId");
count = countId.getValue();
}
ResponseVo result = ResponseVo.success(rid, "success");
result.setCount(count);
log.info("CountMatchPerson完成,PoolIds:[{}],count:{}", poolIds, count);
return result;
} catch (ElasticsearchStatusException e) {
ResponseVo error = ResponseVo.error(rid, e.getDetailedMessage());
error.setCount(0);
return error;
}
}
/**
* 人员比对 * 人员比对
* *
* @param requestVo rid,personPoolId,unionPersonPoolId,person * @param requestVo rid,personPoolId,unionPersonPoolId,person
...@@ -188,6 +251,8 @@ public class PersonService { ...@@ -188,6 +251,8 @@ public class PersonService {
String gender = person.getGender(); String gender = person.getGender();
String personId = person.getPersonId(); String personId = person.getPersonId();
Date personCountTime = person.getCounttime(); Date personCountTime = person.getCounttime();
String direction = person.getDirection();
//Long gateId = person.getGateId();
Integer bodyType = person.getBodyType(); Integer bodyType = person.getBodyType();
String personChannelSerialNum = person.getChannelSerialNum(); String personChannelSerialNum = person.getChannelSerialNum();
List<FaceFeature> faceFeatures = person.getFaceFeatures(); List<FaceFeature> faceFeatures = person.getFaceFeatures();
...@@ -202,7 +267,7 @@ public class PersonService { ...@@ -202,7 +267,7 @@ public class PersonService {
.source(XContentType.JSON, "personId", personId, "unid", unid, .source(XContentType.JSON, "personId", personId, "unid", unid,
"data", feature, "fid", fid, "age", age, "gender", gender, "body_type", bodyType, "data", feature, "fid", fid, "age", age, "gender", gender, "body_type", bodyType,
"counttime", personCountTime == null ? null : Constant.DATE_FORMAT.get().format(personCountTime), "counttime", personCountTime == null ? null : Constant.DATE_FORMAT.get().format(personCountTime),
"channelSerialNum", personChannelSerialNum, "gateId", gateId); "channelSerialNum", personChannelSerialNum, "gateId", gateId, "direction", direction);
bulkRequest.add(indexRequest); bulkRequest.add(indexRequest);
} }
} }
...@@ -230,7 +295,7 @@ public class PersonService { ...@@ -230,7 +295,7 @@ public class PersonService {
.source(XContentType.JSON, "personId", personId, "unid", unid, .source(XContentType.JSON, "personId", personId, "unid", unid,
"body", feature, "fid", fid, "age", age, "gender", gender, "body_type", bodyType, "body", feature, "fid", fid, "age", age, "gender", gender, "body_type", bodyType,
"counttime", counttime == null ? null : Constant.DATE_FORMAT.get().format(counttime) "counttime", counttime == null ? null : Constant.DATE_FORMAT.get().format(counttime)
, "channelSerialNum", channelSerialNum, "gateId", gateId); , "channelSerialNum", channelSerialNum, "gateId", gateId, "direction", direction);
bulkRequest.add(indexRequest); bulkRequest.add(indexRequest);
} }
} }
...@@ -334,7 +399,14 @@ public class PersonService { ...@@ -334,7 +399,14 @@ public class PersonService {
} }
} }
private SearchRequest getSearchRequest(String poolId, Integer matchResultSize, Double[] feature, Person person, int type, Boolean agg) { /**
*
* @param feature
* @param person
* @param type 0,人脸,1人体
* @return
*/
private SearchSourceBuilder getSearchSourceBuilder(Double[] feature, Person person, int type) {
Script script; Script script;
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if (type == 0) { if (type == 0) {
...@@ -366,6 +438,10 @@ public class PersonService { ...@@ -366,6 +438,10 @@ public class PersonService {
boolQuery.filter(QueryBuilders.termQuery("personId", person.getPersonUnid())); boolQuery.filter(QueryBuilders.termQuery("personId", person.getPersonUnid()));
} }
if (StringUtils.isNotBlank(person.getCompareDirection())) {
boolQuery.filter(QueryBuilders.termQuery("direction", person.getCompareDirection()));
}
// 根据时间过滤 // 根据时间过滤
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("counttime"); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("counttime");
Date counttimeGTE = person.getCounttimeGTE(); Date counttimeGTE = person.getCounttimeGTE();
...@@ -382,6 +458,17 @@ public class PersonService { ...@@ -382,6 +458,17 @@ public class PersonService {
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script); ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script);
SearchSourceBuilder builder = new SearchSourceBuilder().query(queryBuilder); SearchSourceBuilder builder = new SearchSourceBuilder().query(queryBuilder);
//控制最小分数
if (0 == type && null != person.getFaceMinScore()) {
builder.minScore(person.getFaceMinScore());
} else if (1 == type && null != person.getBodyMinScore()) {
builder.minScore(person.getBodyMinScore());
}
return builder;
}
private SearchRequest getSearchRequest(String poolId, Integer matchResultSize, Double[] feature, Person person, int type, Boolean agg) {
SearchSourceBuilder builder = getSearchSourceBuilder(feature, person, type);
if (agg) { if (agg) {
MaxAggregationBuilder maxScoreAgg = AggregationBuilders.max("max_score").script(new Script("_score")); MaxAggregationBuilder maxScoreAgg = AggregationBuilders.max("max_score").script(new Script("_score"));
TermsAggregationBuilder personIdAgg = AggregationBuilders.terms("by_personId").field("personId"); TermsAggregationBuilder personIdAgg = AggregationBuilders.terms("by_personId").field("personId");
...@@ -394,6 +481,8 @@ public class PersonService { ...@@ -394,6 +481,8 @@ public class PersonService {
.size(matchResultSize); .size(matchResultSize);
} }
log.debug("poolId:{} 匹配时参数:{}", poolId, builder.toString());
return new SearchRequest(poolId).source(builder); return new SearchRequest(poolId).source(builder);
} }
......
...@@ -312,6 +312,13 @@ public class PoolService { ...@@ -312,6 +312,13 @@ public class PoolService {
} }
builder.endObject(); builder.endObject();
// 方向 direction
builder.startObject("direction");
{
builder.field("type", "keyword");
}
builder.endObject();
} }
builder.endObject(); builder.endObject();
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!