mirror of
https://github.com/chatopera/cosin.git
synced 2025-08-01 16:38:02 +08:00
Fix AbstractResultMapper related class
This commit is contained in:
parent
b4a9a78ec9
commit
0a11285ac7
@ -18,18 +18,19 @@ package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.KbsTopicComment;
|
||||
import com.chatopera.cc.model.Topic;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.elasticsearch.index.query.Operator;
|
||||
import org.elasticsearch.index.query.QueryStringQueryBuilder;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
@ -37,13 +38,18 @@ import java.util.List;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRepository {
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
||||
this.elasticsearchTemplate = elasticsearchTemplate;
|
||||
}
|
||||
@NonNull
|
||||
private final UKResultMapper resultMapper;
|
||||
@NonNull
|
||||
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||
@NonNull
|
||||
private final UKAggResultExtractor ukAggResultExtractor;
|
||||
@NonNull
|
||||
private final UKAggTopResultExtractor ukAggTopResultExtractor;
|
||||
|
||||
@Override
|
||||
public Page<KbsTopicComment> findByDataid(String id, int p, int ps) {
|
||||
Page<KbsTopicComment> pages = null;
|
||||
@ -73,13 +79,16 @@ public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRep
|
||||
SearchQuery searchQuery = searchQueryBuilder.build();
|
||||
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, resultMapper);
|
||||
} else {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKAggTopResultExtractor(field, aggname));
|
||||
ukAggTopResultExtractor.setTerm(field);
|
||||
ukAggTopResultExtractor.setName(aggname);
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, ukAggTopResultExtractor);
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<KbsTopicComment> findByCon(
|
||||
NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
|
||||
@ -88,7 +97,7 @@ public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRep
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||
}
|
||||
return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), KbsTopicComment.class, new UKResultMapper());
|
||||
return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), KbsTopicComment.class, resultMapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -100,7 +109,8 @@ public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRep
|
||||
}
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKAggResultExtractor("creater"));
|
||||
ukAggResultExtractor.setTerm("creater");
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, ukAggResultExtractor);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
@ -17,17 +17,18 @@
|
||||
package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.KbsTopic;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.elasticsearch.index.query.*;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
@ -36,13 +37,12 @@ import java.util.List;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
||||
this.elasticsearchTemplate = elasticsearchTemplate;
|
||||
}
|
||||
@NonNull
|
||||
private final UKResultMapper ukResultMapper;
|
||||
@NonNull
|
||||
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Override
|
||||
public Page<KbsTopic> getTopicByCate(String cate, String q, final int p, final int ps) {
|
||||
@ -59,7 +59,7 @@ public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -80,7 +80,7 @@ public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -100,7 +100,7 @@ public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withQuery(termQuery("creater", user)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
@ -18,12 +18,12 @@ package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.basic.MainContext;
|
||||
import com.chatopera.cc.model.QuickReply;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.elasticsearch.index.query.*;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@ -31,6 +31,7 @@ import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.query.DeleteQuery;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
@ -39,13 +40,14 @@ import java.util.List;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository {
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@NonNull
|
||||
private final UKResultMapper ukResultMapper;
|
||||
|
||||
@NonNull
|
||||
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
||||
this.elasticsearchTemplate = elasticsearchTemplate ;
|
||||
}
|
||||
@Override
|
||||
public Page<QuickReply> getByOrgiAndCate(String orgi, String cate, String q, Pageable page) {
|
||||
|
||||
@ -61,7 +63,7 @@ public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page);
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -113,7 +115,7 @@ public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -133,7 +135,7 @@ public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withQuery(termQuery("creater", user)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -154,6 +156,7 @@ public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<QuickReply> getByOrgiAndType(String orgi, String type, String q, Pageable page) {
|
||||
|
||||
@ -176,6 +179,7 @@ public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByCate(String cate, String orgi) {
|
||||
DeleteQuery deleteQuery = new DeleteQuery();
|
||||
|
@ -18,17 +18,18 @@ package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.Topic;
|
||||
import com.chatopera.cc.persistence.repository.XiaoEUKResultMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.elasticsearch.index.query.*;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
@ -37,13 +38,12 @@ import java.util.List;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
||||
this.elasticsearchTemplate = elasticsearchTemplate;
|
||||
}
|
||||
@NonNull
|
||||
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||
@NonNull
|
||||
private final XiaoEUKResultMapper resultMapper;
|
||||
|
||||
@Override
|
||||
public Page<Topic> getTopicByCateAndOrgi(String cate, String orgi, String q, final int p, final int ps) {
|
||||
@ -62,7 +62,7 @@ public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, resultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -88,7 +88,7 @@ public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, resultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@ -108,7 +108,7 @@ public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withQuery(termQuery("creater", user)).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, resultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
@ -18,24 +18,33 @@ package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.KbsTopic;
|
||||
import com.chatopera.cc.model.KbsTopicComment;
|
||||
import lombok.Setter;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.core.EntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Component
|
||||
public class UKAggResultExtractor extends UKResultMapper {
|
||||
|
||||
@Nullable
|
||||
@Setter
|
||||
private String term;
|
||||
|
||||
public UKAggResultExtractor(String term){
|
||||
this.term = term ;
|
||||
public UKAggResultExtractor(EntityMapper entityMapper, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(entityMapper, mappingContext);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.UKAgg;
|
||||
import lombok.Setter;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
@ -24,28 +25,34 @@ import org.elasticsearch.search.aggregations.Aggregations;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.core.EntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity;
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Component
|
||||
public class UKAggTopResultExtractor extends UKResultMapper {
|
||||
|
||||
@Setter
|
||||
private String term, name;
|
||||
|
||||
public UKAggTopResultExtractor(String term , String name){
|
||||
this.term = term ;
|
||||
this.name = name ;
|
||||
public UKAggTopResultExtractor(EntityMapper entityMapper, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(entityMapper, mappingContext);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
Aggregations aggregations = response.getAggregations();
|
||||
Terms agg = aggregations.get(term);
|
||||
long total = agg.getSumOfOtherDocCounts();
|
||||
List<T> results = new ArrayList<T>();
|
||||
List<T> results = new ArrayList<>();
|
||||
if (agg.getBuckets() != null && agg.getBuckets().size() > 0) {
|
||||
for (int i = pageable.getPageNumber() * pageable.getPageSize(); i < agg.getBuckets().size(); i++) {
|
||||
Terms.Bucket entry = agg.getBuckets().get(i);
|
||||
@ -61,6 +68,6 @@ public class UKAggTopResultExtractor extends UKResultMapper{
|
||||
}
|
||||
}
|
||||
}
|
||||
return new AggregatedPageImpl<T>(results, pageable, total);
|
||||
return new AggregatedPageImpl<>(results, pageable, total);
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,7 @@ import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.get.MultiGetItemResponse;
|
||||
import org.elasticsearch.action.get.MultiGetResponse;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.document.DocumentField;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
||||
@ -33,7 +34,6 @@ import org.springframework.data.elasticsearch.ElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||
import org.springframework.data.elasticsearch.core.AbstractResultMapper;
|
||||
import org.springframework.data.elasticsearch.core.DefaultEntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.EntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
@ -41,6 +41,8 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@ -48,26 +50,12 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
public class UKResultMapper extends AbstractResultMapper {
|
||||
@Nullable
|
||||
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||
|
||||
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||
|
||||
public UKResultMapper() {
|
||||
super(new DefaultEntityMapper());
|
||||
}
|
||||
|
||||
public UKResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(new DefaultEntityMapper());
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
public UKResultMapper(EntityMapper entityMapper) {
|
||||
super(entityMapper);
|
||||
}
|
||||
|
||||
public UKResultMapper(
|
||||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
|
||||
EntityMapper entityMapper) {
|
||||
public UKResultMapper(@NonNull EntityMapper entityMapper, @Nullable MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(entityMapper);
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
@ -75,10 +63,10 @@ public class UKResultMapper extends AbstractResultMapper {
|
||||
@Override
|
||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
long totalHits = response.getHits().getTotalHits();
|
||||
List<T> results = new ArrayList<T>();
|
||||
List<T> results = new ArrayList<>();
|
||||
for (SearchHit hit : response.getHits()) {
|
||||
if (hit != null) {
|
||||
T result = null;
|
||||
T result;
|
||||
if (StringUtils.isNotBlank(hit.getSourceAsString())) {
|
||||
result = mapEntity(hit.getSourceAsString(), hit, clazz);
|
||||
} else {
|
||||
@ -90,7 +78,7 @@ public class UKResultMapper extends AbstractResultMapper {
|
||||
}
|
||||
}
|
||||
|
||||
return new AggregatedPageImpl<T>(results, pageable, totalHits);
|
||||
return new AggregatedPageImpl<>(results, pageable, totalHits);
|
||||
}
|
||||
|
||||
public <T> T mapEntity(String source, SearchHit hit, Class<T> clazz) {
|
||||
@ -181,7 +169,7 @@ public class UKResultMapper extends AbstractResultMapper {
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
||||
LinkedList<T> list = new LinkedList<T>();
|
||||
LinkedList<T> list = new LinkedList<>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
||||
@ -193,10 +181,11 @@ public class UKResultMapper extends AbstractResultMapper {
|
||||
}
|
||||
|
||||
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
|
||||
|
||||
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
||||
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
||||
if (persistentEntity == null) {
|
||||
return;
|
||||
}
|
||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
||||
|
||||
// Only deal with String because ES generated Ids are strings !
|
||||
|
@ -33,7 +33,6 @@ import org.springframework.data.elasticsearch.ElasticsearchException;
|
||||
import org.springframework.data.elasticsearch.annotations.Document;
|
||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||
import org.springframework.data.elasticsearch.core.AbstractResultMapper;
|
||||
import org.springframework.data.elasticsearch.core.DefaultEntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.EntityMapper;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
@ -41,6 +40,9 @@ import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersiste
|
||||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.mapping.context.MappingContext;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@ -48,26 +50,13 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
|
||||
@Component
|
||||
public class XiaoEUKResultMapper extends AbstractResultMapper {
|
||||
|
||||
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||
@Nullable
|
||||
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||
|
||||
public XiaoEUKResultMapper() {
|
||||
super(new DefaultEntityMapper());
|
||||
}
|
||||
|
||||
public XiaoEUKResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(new DefaultEntityMapper());
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
public XiaoEUKResultMapper(EntityMapper entityMapper) {
|
||||
super(entityMapper);
|
||||
}
|
||||
|
||||
public XiaoEUKResultMapper(
|
||||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
|
||||
EntityMapper entityMapper) {
|
||||
public XiaoEUKResultMapper(@NonNull EntityMapper entityMapper, @Nullable MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(entityMapper);
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
@ -75,10 +64,10 @@ public class XiaoEUKResultMapper extends AbstractResultMapper {
|
||||
@Override
|
||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
long totalHits = response.getHits().getTotalHits();
|
||||
List<T> results = new ArrayList<T>();
|
||||
List<T> results = new ArrayList<>();
|
||||
for (SearchHit hit : response.getHits()) {
|
||||
if (hit != null) {
|
||||
T result = null;
|
||||
T result;
|
||||
if (StringUtils.isNotBlank(hit.getSourceAsString())) {
|
||||
result = mapEntity(hit.getSourceAsString(), hit, clazz);
|
||||
} else {
|
||||
@ -90,7 +79,7 @@ public class XiaoEUKResultMapper extends AbstractResultMapper {
|
||||
}
|
||||
}
|
||||
|
||||
return new AggregatedPageImpl<T>(results, pageable, totalHits);
|
||||
return new AggregatedPageImpl<>(results, pageable, totalHits);
|
||||
}
|
||||
|
||||
public <T> T mapEntity(String source, SearchHit hit, Class<T> clazz) {
|
||||
@ -181,7 +170,7 @@ public class XiaoEUKResultMapper extends AbstractResultMapper {
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
||||
LinkedList<T> list = new LinkedList<T>();
|
||||
LinkedList<T> list = new LinkedList<>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
||||
@ -197,6 +186,9 @@ public class XiaoEUKResultMapper extends AbstractResultMapper {
|
||||
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
||||
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
||||
if (persistentEntity == null) {
|
||||
return;
|
||||
}
|
||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
||||
|
||||
// Only deal with String because ES generated Ids are strings !
|
||||
|
Loading…
x
Reference in New Issue
Block a user