Commit 8ef31fb8 by xmh

聚合,只返回分数和personId

1 parent ca508586
...@@ -29,14 +29,18 @@ import org.elasticsearch.index.reindex.DeleteByQueryRequest; ...@@ -29,14 +29,18 @@ import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.script.Script; import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType; import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.Max;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
...@@ -325,7 +329,7 @@ public class PersonService { ...@@ -325,7 +329,7 @@ public class PersonService {
if (channelSerialNums != null && channelSerialNums.size() > 0) { if (channelSerialNums != null && channelSerialNums.size() > 0) {
boolQuery.filter(QueryBuilders.termsQuery("channelSerialNum", channelSerialNums)); boolQuery.filter(QueryBuilders.termsQuery("channelSerialNum", channelSerialNums));
} }
if(person.getPersonUnid() != null && !person.getPersonUnid().equals("")){ if (person.getPersonUnid() != null && !person.getPersonUnid().equals("")) {
boolQuery.filter(QueryBuilders.termQuery("personId", person.getPersonUnid())); boolQuery.filter(QueryBuilders.termQuery("personId", person.getPersonUnid()));
} }
...@@ -345,10 +349,16 @@ public class PersonService { ...@@ -345,10 +349,16 @@ public class PersonService {
ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script); ScriptScoreQueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(boolQuery, script);
MaxAggregationBuilder maxScoreAgg = AggregationBuilders.max("max_score").script(new Script("_score"));
TermsAggregationBuilder personIdAgg = AggregationBuilders.terms("by_personId").field("personId");
personIdAgg.subAggregation(maxScoreAgg);
personIdAgg.order(BucketOrder.aggregation("max_score", false));
SearchSourceBuilder builder = new SearchSourceBuilder() SearchSourceBuilder builder = new SearchSourceBuilder()
.size(matchResultSize) .size(matchResultSize)
.query(queryBuilder) .query(queryBuilder)
.fetchSource(FETCH_SOURCE, null); // .fetchSource(FETCH_SOURCE, null)
.aggregation(personIdAgg);
return new SearchRequest(poolId).source(builder); return new SearchRequest(poolId).source(builder);
} }
...@@ -356,32 +366,42 @@ public class PersonService { ...@@ -356,32 +366,42 @@ public class PersonService {
private List<Person> match0(SearchRequest searchRequest) throws Exception { private List<Person> match0(SearchRequest searchRequest) throws Exception {
List<Person> persons = new ArrayList<>(); List<Person> persons = new ArrayList<>();
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
Aggregations aggregations = search.getAggregations();
SearchHits hits = search.getHits(); Terms byPersonId = aggregations.get("by_personId");
SearchHit[] hits1 = hits.getHits(); List<? extends Terms.Bucket> buckets = byPersonId.getBuckets();
for (SearchHit item : hits1) { for (Terms.Bucket bucket : buckets) {
Map<String, Object> source = item.getSourceAsMap(); String personId = (String) bucket.getKey();
Person p = new Person(); Max maxScore = bucket.getAggregations().get("max_score");
p.setPersonId((String) source.get("personId")); double value = maxScore.getValue();
p.setAge((Integer) source.get("age")); Person person = new Person().setPersonId(personId).setScore((float) value);
p.setGender((String) source.get("gender")); persons.add(person);
p.setChannelSerialNum((String) source.get("channelSerialNum"));
p.setBodyType((Integer) source.get("body_type"));
p.setCounttime(Optional.ofNullable((String) source.get("counttime"))
.map(x -> {
try {
return Constant.DATE_FORMAT.get().parse(x);
} catch (ParseException ignore) {
}
return null;
})
.orElse(null));
p.setScore(item.getScore());
p.setPersonPoolId(item.getIndex());
persons.add(p);
} }
// SearchHits hits = search.getHits();
// SearchHit[] hits1 = hits.getHits();
// for (SearchHit item : hits1) {
// Map<String, Object> source = item.getSourceAsMap();
// Person p = new Person();
// p.setPersonId((String) source.get("personId"));
// p.setAge((Integer) source.get("age"));
// p.setGender((String) source.get("gender"));
// p.setChannelSerialNum((String) source.get("channelSerialNum"));
// p.setBodyType((Integer) source.get("body_type"));
//
// p.setCounttime(Optional.ofNullable((String) source.get("counttime"))
// .map(x -> {
// try {
// return Constant.DATE_FORMAT.get().parse(x);
// } catch (ParseException ignore) {
// }
// return null;
// })
// .orElse(null));
//
// p.setScore(item.getScore());
// p.setPersonPoolId(item.getIndex());
// persons.add(p);
// }
return persons; return persons;
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!