Commit 7b18ab5b by 李乾广

[chg]比对符合条件的数量优化

1 parent 24f11a6b
......@@ -108,21 +108,39 @@ public class PersonService {
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());
Double[] feature = null;
if (CollectionUtils.isEmpty(bodyFeatures) && null == person.getBodyMinScore()) {
log.info("CountMatchPerson人体特征为空并且最小分数为空,直接查数量,PoolIds:[{}],count:{}", poolIds, count);
} else if (CollectionUtils.isNotEmpty(bodyFeatures)){
feature = bodyFeatures.get(0).getFeature();
if (feature == null || feature.length < Constant.BODY_FEATURE_DIMS_2048) {
log.info("人体特征维数小于2048,跳过比对");
ResponseVo error = ResponseVo.error(rid, "人体特征维数小于2048,跳过比对");
error.setCount(0);
return error;
} else if (feature.length == Constant.BODY_FEATURE_DIMS_2110) {
feature = Utils.transferBodyFeature(feature);
}
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
ParsedValueCount countId = response.getAggregations().get("countId");
count = countId.getValue();
if (feature.length > Constant.BODY_FEATURE_DIMS_2048) {
feature = Arrays.copyOfRange(feature, 3, Constant.BODY_FEATURE_DIMS_2048 + 3);
}
} else {
log.info("参数错误");
ResponseVo error = ResponseVo.error(rid, "参数错误");
error.setCount(0);
return error;
}
SearchSourceBuilder builder = getSearchSourceBuilder(feature, person, 1);
builder.size(0);
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);
......@@ -407,14 +425,14 @@ public class PersonService {
* @return
*/
private SearchSourceBuilder getSearchSourceBuilder(Double[] feature, Person person, int type) {
Script script;
Script script = null;
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
if (type == 0) {
if (null != feature && type == 0) {
script = new Script(
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
"(cosineSimilarity(params.face, 'data') + 1) / 2 * 100", Collections.singletonMap("face", feature));
boolQuery.filter(QueryBuilders.existsQuery("data"));
} else {
} else if (null != feature && type == 1){
script = new Script(
ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG,
"(cosineSimilarity(params.body, 'body') + 1) / 2 * 100", Collections.singletonMap("body", feature));
......@@ -455,14 +473,18 @@ public class PersonService {
if (counttimeGTE != null || counttimeLTE != null) {
boolQuery.filter(rangeQueryBuilder);
}
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script);
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());
SearchSourceBuilder builder;
if (null == script) {
builder = new SearchSourceBuilder().query(boolQuery);
} else {
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script);
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;
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!