Commit 9998e1cb by 姚冰

[chg] 字段修正,结果序列化修改,数量计算修正

1 parent f79c2950
......@@ -16,9 +16,8 @@ public class PersonInfo {
private Integer age;
private String gender;
private Integer bodyType;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date countTime;
private Date counttime;
private String fid;
private String channelSerialNum;
private Long gateId;
......@@ -32,7 +31,7 @@ public class PersonInfo {
this.age = age;
this.gender = gender;
this.bodyType = bodyType;
this.countTime = countTime;
this.counttime = countTime;
this.fid = fid;
this.channelSerialNum = channelSerialNum;
this.gateId = gateId;
......
package com.viontech.match.entity;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
......@@ -16,7 +17,8 @@ public class SearchResultHit {
private Integer age;
private String gender;
private Integer bodyType;
private Date countTime;
// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
private String counttime;
private String fid;
private String channelSerialNum;
private Long gateId;
......
......@@ -7,6 +7,7 @@ import co.elastic.clients.elasticsearch._types.query_dsl.*;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.bulk.IndexOperation;
import co.elastic.clients.elasticsearch.core.knn_search.KnnSearchQuery;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.core.search.SourceConfig;
......@@ -48,7 +49,7 @@ import java.util.stream.Stream;
@Service
@Slf4j
public class PersonService {
private static final String[] FETCH_SOURCE = new String[]{"personId", "age", "gender", "fid", "counttime", "channelSerialNum", "body_type"};
private static final String[] FETCH_SOURCE = new String[]{"personId", "age", "gender", "fid", "counttime", "channelSerialNum", "body_type","unid"};
@Resource
private ElasticsearchClient client;
......@@ -109,14 +110,20 @@ public class PersonService {
Aggregation countAggregation = AggregationBuilders.valueCount(s -> s.field("unid"));
// BoolQuery.Builder builder = getSearchSourceBuilder(feature, person, 1);
Query query = null;
if (feature == null) {
BoolQuery boolQuery = getSearchSourceCountBuilder(feature, person, 1);
query = boolQuery._toQuery();
} else {
ScriptScoreQuery scriptScoreQuery = getScriptScoreQuery(feature, person, 1);
SearchRequest request = new SearchRequest.Builder().index(Arrays.asList(indices)).ignoreUnavailable(true).aggregations("countId", countAggregation).size(0).query(scriptScoreQuery._toQuery()).build();
log.debug("rid:{} CountMatchPerson条件{}:{}", rid, indices, scriptScoreQuery.toString());
query = scriptScoreQuery._toQuery();
}
SearchRequest request = new SearchRequest.Builder().index(Arrays.asList(indices)).ignoreUnavailable(true).aggregations("countId", countAggregation).size(0).query(query).build();
// log.debug("rid:{} CountMatchPerson条件{}:{}", rid, indices, query.toString());
SearchResponse<SearchResultHit> response = client.search(request, SearchResultHit.class);
// ParsedValueCount countId = response.aggregations().get("countId").valueCount().value();
count = (long) response.aggregations().get("countId").valueCount().value();
count = response.aggregations().size() == 0 ? 0 :(long) response.aggregations().get("countId").valueCount().value();
ResponseVo result = ResponseVo.success(rid, "success");
result.setCount(count);
......@@ -406,20 +413,7 @@ public class PersonService {
return list.stream().map(x -> x.floatValue()).collect(Collectors.toList());
}
private KnnSearch getKnnSearch(Double[] feature, Person person, int type) {
return new KnnSearch.Builder()
.field("body")
.queryVector(getFeature(feature))
.numCandidates(type == 0 ? Constant.FACE_MATCH_RESULT_SIZE : Constant.BODY_MATCH_RESULT_SIZE)
.build();
}
private ScriptScoreQuery getScriptScoreQuery(Double[] feature, Person person, int type) {
// return new Script.Builder()
// .lang("painless")
// .source("(cosineSimilarity(params.face, 'data') + 1) / 2 * 100")
// .params(Collections.singletonMap("body", feature))
// .build();
return new ScriptScoreQuery.Builder()
.query(getSearchSourceBuilder(feature, person, type)._toQuery())
.script(new Script.Builder()
......@@ -431,6 +425,40 @@ public class PersonService {
.build();
}
private BoolQuery getSearchSourceCountBuilder(Double[] feature, Person person, int type) {
List<Query> queries = new ArrayList<>();
BoolQuery.Builder boolQuery = new BoolQuery.Builder();
// 根据通道号过滤
List<String> channelSerialNums = person.getChannelSerialNums();
if (CollectionUtils.isNotEmpty(channelSerialNums)) {
queries.add(QueryBuilders.terms().field("channelSerialNum").terms(new TermsQueryField.Builder().value(channelSerialNums.stream().map(FieldValue::of).collect(Collectors.toList())).build()).build()._toQuery());
}
// 根据监控点id进行过滤
List<Long> gateIdIn = person.getGateIdIn();
if (CollectionUtils.isNotEmpty(gateIdIn)) {
queries.add(QueryBuilders.terms().field("gateId").terms(new TermsQueryField.Builder().value(gateIdIn.stream().map(FieldValue::of).collect(Collectors.toList())).build()).build()._toQuery());
}
// 根据人的ID进行过滤
if (StringUtils.isNotEmpty(person.getPersonUnid())) {
queries.add(QueryBuilders.term().field("personId").value(FieldValue.of(person.getPersonUnid())).build()._toQuery());
}
if (StringUtils.isNotBlank(person.getCompareDirection())) {
queries.add(QueryBuilders.term().field("direction").value(FieldValue.of(person.getCompareDirection())).build()._toQuery());
}
Date counttimeGTE = person.getCounttimeGTE();
Date counttimeLTE = person.getCounttimeLTE();
if (counttimeGTE != null || counttimeLTE != null) {
queries.add(QueryBuilders.range().term(s -> s.field("counttime").gte(Constant.DATE_FORMAT.get().format(counttimeGTE)).lte(Constant.DATE_FORMAT.get().format(counttimeLTE))).build()._toQuery());
}
return boolQuery.filter(queries).build();
}
/**
*
* @param feature
......@@ -439,21 +467,12 @@ public class PersonService {
* @return
*/
private KnnQuery getSearchSourceBuilder(Double[] feature, Person person, int type) {
// Script script = null;
// QueryBuilders boolQuery = QueryBuilders.bool();
List<Query> queries = new ArrayList<>();
KnnQuery.Builder knnQuery = new KnnQuery.Builder();
if (null != feature && type == 0) {
// Query query = QueryBuilders.knn()
// .field("data")
// .queryVector(getFeature(feature))
// .numCandidates(Constant.FACE_MATCH_RESULT_SIZE)
// .similarity(person.getFaceMinScore())
// .build()._toQuery();
// queries.add(query);
knnQuery.field("data").queryVector(getFeature(feature)).numCandidates(Constant.FACE_MATCH_RESULT_SIZE);
} else if (null != feature && type == 1){
knnQuery.field("body").queryVector(getFeature(feature)).numCandidates(Constant.FACE_MATCH_RESULT_SIZE);
knnQuery.field("body").queryVector(getFeature(feature)).numCandidates(Constant.BODY_MATCH_RESULT_SIZE);
queries.add(new Query.Builder().exists(s -> s.field("body").boost(1.0f)).build());
}
......@@ -466,8 +485,6 @@ public class PersonService {
// 根据监控点id进行过滤
List<Long> gateIdIn = person.getGateIdIn();
if (CollectionUtils.isNotEmpty(gateIdIn)) {
// boolQuery.filter(QueryBuilders.termsQuery("gateId", gateIdIn));
;
queries.add(QueryBuilders.terms().field("gateId").terms(new TermsQueryField.Builder().value(gateIdIn.stream().map(FieldValue::of).collect(Collectors.toList())).build()).build()._toQuery());
}
......@@ -478,51 +495,17 @@ public class PersonService {
}
if (StringUtils.isNotBlank(person.getCompareDirection())) {
// boolQuery.filter(QueryBuilders.term().field("direction")., person.getCompareDirection()));
// QueryBuilders.terms().field("gateId").terms(Arrays.asList(person.getCompareDirection().split(",")))
queries.add(QueryBuilders.term().field("direction").value(FieldValue.of(person.getCompareDirection())).build()._toQuery());
}
// RangeQuery.Builder builder = QueryBuilders.range().term(s -> s.field("counttime"));
// 根据时间过滤
// RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("counttime");
Date counttimeGTE = person.getCounttimeGTE();
Date counttimeLTE = person.getCounttimeLTE();
// if (counttimeGTE != null) {
//// rangeQueryBuilder.gte(Constant.DATE_FORMAT.get().format(counttimeGTE));
// builder.term(s -> s.gte(Constant.DATE_FORMAT.get().format(counttimeGTE)));
// }
// if (counttimeLTE != null) {
//// rangeQueryBuilder.lte(Constant.DATE_FORMAT.get().format(counttimeLTE));
// builder.term(s -> s.lte(Constant.DATE_FORMAT.get().format(counttimeLTE)));
// }
if (counttimeGTE != null || counttimeLTE != null) {
// boolQuery.filter(rangeQueryBuilder);
queries.add(QueryBuilders.range().term(s -> s.field("counttime").gte(Constant.DATE_FORMAT.get().format(counttimeGTE)).lte(Constant.DATE_FORMAT.get().format(counttimeLTE))).build()._toQuery());
}
// queryBuilder.filter(queries);
// SearchSourceBuilder builder;
// BoolQuery.Builder boolQuery = new BoolQuery.Builder().filter(queries);
// if (null == script) {
// builder = new SearchSourceBuilder().query(boolQuery);
// query = new BoolQuery.Builder().filter(queries).build();
// } else
{
// ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script);
// builder = new SearchSourceBuilder().query(queryBuilder);
//控制最小分数,只有特征比对时才有分数
// if (0 == type && null != person.getFaceMinScore()) {
//// builder.minScore(person.getFaceMinScore());
// queries.add(QueryBuilders.scriptScore().minScore(person.getFaceMinScore()).build().query());
// } else if (1 == type && null != person.getBodyMinScore()) {
//// builder.minScore(person.getBodyMinScore());
// queries.add(QueryBuilders.scriptScore().minScore(person.getBodyMinScore()).build().query());
// }
}
return knnQuery.filter(queries).build();
// new SearchRequest.Builder().index(poolId).query(q -> q.knn(k -> k.field("body").queryVector(getFeature(feature)).numCandidates(Constant.FACE_MATCH_RESULT_SIZE))).scriptFields("", new ScriptField.Builder().build()).size(matchResultSize).minScore(person.getBodyMinScore());
}
private SearchRequest getSearchRequest(String rid, String poolId, Integer matchResultSize, Double[] feature, Person person, int type, Boolean agg) {
......@@ -559,7 +542,7 @@ public class PersonService {
private List<Person> match0(SearchRequest searchRequest, Boolean agg) throws Exception {
String poolId = searchRequest.index().get(0);
List<Person> persons = new ArrayList<>();
// log.info("request:{}", searchRequest.toString());
// log.info("request:{}", JSON.tojsonS);
SearchResponse<SearchResultHit> search = client.search(searchRequest, SearchResultHit.class);
if (agg) {
Map aggregations = search.aggregations();
......@@ -585,10 +568,15 @@ public class PersonService {
p.setGender((String) hit.getGender());
p.setChannelSerialNum(hit.getChannelSerialNum());
p.setBodyType(hit.getBodyType());
p.setCaptureUnid(hit.getUnid());
p.setCounttime(Optional.ofNullable(hit.getCountTime())
p.setCounttime(Optional.ofNullable((String)hit.getCounttime())
.map(x -> {
return x;
try {
return Constant.DATE_FORMAT.get().parse(x);
} catch (ParseException e) {
return null;
}
})
.orElse(null));
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!