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.KbsTopicComment;
|
||||||
import com.chatopera.cc.model.Topic;
|
import com.chatopera.cc.model.Topic;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.elasticsearch.index.query.Operator;
|
import org.elasticsearch.index.query.Operator;
|
||||||
import org.elasticsearch.index.query.QueryStringQueryBuilder;
|
import org.elasticsearch.index.query.QueryStringQueryBuilder;
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -37,71 +38,80 @@ import java.util.List;
|
|||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRepository{
|
@RequiredArgsConstructor
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRepository {
|
||||||
|
|
||||||
@Autowired
|
@NonNull
|
||||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
private final UKResultMapper resultMapper;
|
||||||
this.elasticsearchTemplate = elasticsearchTemplate;
|
@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;
|
||||||
|
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid", id)).withSort(new FieldSortBuilder("optimal").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)).build().setPageable(PageRequest.of(p, ps));
|
||||||
|
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||||
|
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class);
|
||||||
|
}
|
||||||
|
return pages;
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public Page<KbsTopicComment> findByDataid(String id , int p , int ps) {
|
|
||||||
Page<KbsTopicComment> pages = null;
|
|
||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid", id)).withSort(new FieldSortBuilder("optimal").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC)).build().setPageable(PageRequest.of(p, ps));
|
|
||||||
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class);
|
|
||||||
}
|
|
||||||
return pages;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<KbsTopicComment> findByOptimal(String dataid) {
|
public List<KbsTopicComment> findByOptimal(String dataid) {
|
||||||
List<KbsTopicComment> commentList = null ;
|
List<KbsTopicComment> commentList = null;
|
||||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid" , dataid)).withQuery(termQuery("optimal" , true)).build();
|
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid", dataid)).withQuery(termQuery("optimal", true)).build();
|
||||||
if(elasticsearchTemplate.indexExists(KbsTopicComment.class)){
|
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||||
commentList = elasticsearchTemplate.queryForList(searchQuery, KbsTopicComment.class);
|
commentList = elasticsearchTemplate.queryForList(searchQuery, KbsTopicComment.class);
|
||||||
}
|
}
|
||||||
return commentList ;
|
return commentList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<KbsTopicComment> findByCon(NativeSearchQueryBuilder searchQueryBuilder , String field , String aggname, String q , final int p , final int ps) {
|
public Page<KbsTopicComment> findByCon(NativeSearchQueryBuilder searchQueryBuilder, String field, String aggname, String q, final int p, final int ps) {
|
||||||
Page<KbsTopicComment> pages = null ;
|
Page<KbsTopicComment> pages = null;
|
||||||
if(!StringUtils.isBlank(q)){
|
if (!StringUtils.isBlank(q)) {
|
||||||
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
}
|
}
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build();
|
SearchQuery searchQuery = searchQueryBuilder.build();
|
||||||
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||||
if (!StringUtils.isBlank(q)) {
|
if (!StringUtils.isBlank(q)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, resultMapper);
|
||||||
} else {
|
} 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
|
return pages;
|
||||||
public Page<KbsTopicComment> findByCon(
|
}
|
||||||
NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
|
|
||||||
searchQueryBuilder.withPageable(PageRequest.of(p, ps)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
|
|
||||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("content").fragmentSize(200));
|
|
||||||
if (!StringUtils.isBlank(q)) {
|
|
||||||
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
|
||||||
}
|
|
||||||
return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), KbsTopicComment.class, new UKResultMapper());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<KbsTopicComment> countByCon(
|
public Page<KbsTopicComment> findByCon(
|
||||||
NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
|
NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
|
||||||
Page<KbsTopicComment> pages = null;
|
searchQueryBuilder.withPageable(PageRequest.of(p, ps)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
|
||||||
if (!StringUtils.isBlank(q)) {
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("content").fragmentSize(200));
|
||||||
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
if (!StringUtils.isBlank(q)) {
|
||||||
}
|
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
}
|
||||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
return elasticsearchTemplate.queryForPage(searchQueryBuilder.build(), KbsTopicComment.class, resultMapper);
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKAggResultExtractor("creater"));
|
}
|
||||||
}
|
|
||||||
return pages;
|
@Override
|
||||||
}
|
public Page<KbsTopicComment> countByCon(
|
||||||
|
NativeSearchQueryBuilder searchQueryBuilder, String q, int p, int ps) {
|
||||||
|
Page<KbsTopicComment> pages = null;
|
||||||
|
if (!StringUtils.isBlank(q)) {
|
||||||
|
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
|
}
|
||||||
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
|
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||||
|
ukAggResultExtractor.setTerm("creater");
|
||||||
|
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, ukAggResultExtractor);
|
||||||
|
}
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,17 +17,18 @@
|
|||||||
package com.chatopera.cc.persistence.es;
|
package com.chatopera.cc.persistence.es;
|
||||||
|
|
||||||
import com.chatopera.cc.model.KbsTopic;
|
import com.chatopera.cc.model.KbsTopic;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.elasticsearch.index.query.*;
|
import org.elasticsearch.index.query.*;
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -36,13 +37,12 @@ import java.util.List;
|
|||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
@NonNull
|
||||||
|
private final UKResultMapper ukResultMapper;
|
||||||
@Autowired
|
@NonNull
|
||||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||||
this.elasticsearchTemplate = elasticsearchTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<KbsTopic> getTopicByCate(String cate, String q, final int p, final int ps) {
|
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));
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, ukResultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
@ -80,7 +80,7 @@ public class KbsTopicRepositoryImpl implements KbsTopicEsCommonRepository {
|
|||||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, ukResultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
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));
|
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));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
if (elasticsearchTemplate.indexExists(KbsTopic.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, new UKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopic.class, ukResultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
|
@ -18,12 +18,12 @@ package com.chatopera.cc.persistence.es;
|
|||||||
|
|
||||||
import com.chatopera.cc.basic.MainContext;
|
import com.chatopera.cc.basic.MainContext;
|
||||||
import com.chatopera.cc.model.QuickReply;
|
import com.chatopera.cc.model.QuickReply;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.elasticsearch.index.query.*;
|
import org.elasticsearch.index.query.*;
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
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.DeleteQuery;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -39,176 +40,179 @@ import java.util.List;
|
|||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
@RequiredArgsConstructor
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository {
|
||||||
|
@NonNull
|
||||||
|
private final UKResultMapper ukResultMapper;
|
||||||
|
|
||||||
@Autowired
|
@NonNull
|
||||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||||
this.elasticsearchTemplate = elasticsearchTemplate ;
|
|
||||||
|
@Override
|
||||||
|
public Page<QuickReply> getByOrgiAndCate(String orgi, String cate, String q, Pageable page) {
|
||||||
|
|
||||||
|
Page<QuickReply> pages = null;
|
||||||
|
|
||||||
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
|
boolQueryBuilder.must(termQuery("cate", cate));
|
||||||
|
|
||||||
|
if (!StringUtils.isBlank(q)) {
|
||||||
|
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
|
}
|
||||||
|
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
||||||
|
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, ukResultMapper);
|
||||||
|
}
|
||||||
|
return pages;
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public Page<QuickReply> getByOrgiAndCate(String orgi , String cate , String q, Pageable page) {
|
|
||||||
|
|
||||||
Page<QuickReply> pages = null ;
|
@Override
|
||||||
|
public List<QuickReply> findByOrgiAndCreater(String orgi, String creater, String q) {
|
||||||
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
List<QuickReply> pages = null;
|
||||||
boolQueryBuilder.must(termQuery("cate" , cate)) ;
|
|
||||||
|
|
||||||
if (!StringUtils.isBlank(q)) {
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
boolQueryBuilder.must(termQuery("orgi", orgi));
|
||||||
}
|
|
||||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
return pages;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
BoolQueryBuilder quickQueryBuilder = QueryBuilders.boolQuery();
|
||||||
public List<QuickReply> findByOrgiAndCreater(String orgi ,String creater , String q) {
|
|
||||||
|
|
||||||
List<QuickReply> pages = null ;
|
quickQueryBuilder.should(termQuery("type", MainContext.QuickType.PUB.toString()));
|
||||||
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
BoolQueryBuilder priQueryBuilder = QueryBuilders.boolQuery();
|
||||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
|
||||||
|
|
||||||
BoolQueryBuilder quickQueryBuilder = QueryBuilders.boolQuery();
|
priQueryBuilder.must(termQuery("type", MainContext.QuickType.PRI.toString()));
|
||||||
|
priQueryBuilder.must(termQuery("creater", creater));
|
||||||
|
|
||||||
quickQueryBuilder.should(termQuery("type" , MainContext.QuickType.PUB.toString())) ;
|
quickQueryBuilder.should(priQueryBuilder);
|
||||||
|
|
||||||
BoolQueryBuilder priQueryBuilder = QueryBuilders.boolQuery();
|
boolQueryBuilder.must(quickQueryBuilder);
|
||||||
|
|
||||||
priQueryBuilder.must(termQuery("type" , MainContext.QuickType.PRI.toString())) ;
|
if (!StringUtils.isBlank(q)) {
|
||||||
priQueryBuilder.must(termQuery("creater" , creater)) ;
|
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
|
}
|
||||||
quickQueryBuilder.should(priQueryBuilder);
|
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
||||||
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||||
boolQueryBuilder.must(quickQueryBuilder);
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(0, 10000));
|
||||||
|
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||||
if (!StringUtils.isBlank(q)) {
|
pages = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
|
||||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
}
|
||||||
}
|
return pages;
|
||||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
}
|
||||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(0, 10000));
|
|
||||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
|
||||||
pages = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
|
|
||||||
}
|
|
||||||
return pages;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<QuickReply> getByQuicktype(String quicktype , final int p , final int ps) {
|
public Page<QuickReply> getByQuicktype(String quicktype, final int p, final int ps) {
|
||||||
|
|
||||||
Page<QuickReply> pages = null;
|
Page<QuickReply> pages = null;
|
||||||
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
boolQueryBuilder.must(termQuery("type", quicktype));
|
boolQueryBuilder.must(termQuery("type", quicktype));
|
||||||
|
|
||||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
||||||
|
|
||||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, ukResultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<QuickReply> getByCateAndUser(String cate , String q , String user ,final int p , final int ps) {
|
public Page<QuickReply> getByCateAndUser(String cate, String q, String user, final int p, final int ps) {
|
||||||
|
|
||||||
Page<QuickReply> pages = null;
|
Page<QuickReply> pages = null;
|
||||||
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
boolQueryBuilder.must(termQuery("cate", cate));
|
boolQueryBuilder.must(termQuery("cate", cate));
|
||||||
|
|
||||||
if (!StringUtils.isBlank(q)) {
|
if (!StringUtils.isBlank(q)) {
|
||||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
}
|
}
|
||||||
|
|
||||||
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));
|
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));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, ukResultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<QuickReply> getByCon(BoolQueryBuilder boolQueryBuilder, final int p , final int ps) {
|
public Page<QuickReply> getByCon(BoolQueryBuilder boolQueryBuilder, final int p, final int ps) {
|
||||||
|
|
||||||
Page<QuickReply> pages = null;
|
Page<QuickReply> pages = null;
|
||||||
|
|
||||||
QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.existsQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime()));
|
QueryBuilder beginFilter = QueryBuilders.boolQuery().should(QueryBuilders.existsQuery("begintime")).should(QueryBuilders.rangeQuery("begintime").from(new Date().getTime()));
|
||||||
QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.existsQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime()));
|
QueryBuilder endFilter = QueryBuilders.boolQuery().should(QueryBuilders.existsQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(new Date().getTime()));
|
||||||
|
|
||||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withFilter(QueryBuilders.boolQuery().must(beginFilter).must(endFilter)).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
||||||
|
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
|
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
@Override
|
|
||||||
public Page<QuickReply> getByOrgiAndType(String orgi ,String type, String q , Pageable page) {
|
|
||||||
|
|
||||||
Page<QuickReply> list = null ;
|
@Override
|
||||||
|
public Page<QuickReply> getByOrgiAndType(String orgi, String type, String q, Pageable page) {
|
||||||
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
Page<QuickReply> list = null;
|
||||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
|
||||||
if(!StringUtils.isBlank(type)) {
|
|
||||||
boolQueryBuilder.must(termQuery("type" , type)) ;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!StringUtils.isBlank(q)) {
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
boolQueryBuilder.must(termQuery("orgi", orgi));
|
||||||
}
|
if (!StringUtils.isBlank(type)) {
|
||||||
|
boolQueryBuilder.must(termQuery("type", type));
|
||||||
|
}
|
||||||
|
|
||||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
if (!StringUtils.isBlank(q)) {
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page);
|
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
}
|
||||||
list = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
|
|
||||||
}
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void deleteByCate(String cate ,String orgi) {
|
|
||||||
DeleteQuery deleteQuery = new DeleteQuery();
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
|
||||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
|
||||||
boolQueryBuilder.must(termQuery("cate" , cate)) ;
|
|
||||||
deleteQuery.setQuery(boolQueryBuilder);
|
|
||||||
elasticsearchTemplate.delete(deleteQuery);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
||||||
public List<QuickReply> getQuickReplyByOrgi(String orgi , String cate,String type, String q) {
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page);
|
||||||
|
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||||
|
list = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
List<QuickReply> list = null ;
|
@Override
|
||||||
|
public void deleteByCate(String cate, String orgi) {
|
||||||
|
DeleteQuery deleteQuery = new DeleteQuery();
|
||||||
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
|
boolQueryBuilder.must(termQuery("orgi", orgi));
|
||||||
|
boolQueryBuilder.must(termQuery("cate", cate));
|
||||||
|
deleteQuery.setQuery(boolQueryBuilder);
|
||||||
|
elasticsearchTemplate.delete(deleteQuery);
|
||||||
|
}
|
||||||
|
|
||||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
@Override
|
||||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
public List<QuickReply> getQuickReplyByOrgi(String orgi, String cate, String type, String q) {
|
||||||
|
|
||||||
if(!StringUtils.isBlank(cate)){
|
List<QuickReply> list = null;
|
||||||
boolQueryBuilder.must(termQuery("cate", cate));
|
|
||||||
}
|
|
||||||
if (!StringUtils.isBlank(type)) {
|
|
||||||
boolQueryBuilder.must(termQuery("type", type));
|
|
||||||
}
|
|
||||||
if (!StringUtils.isBlank(q)) {
|
|
||||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
|
||||||
}
|
|
||||||
|
|
||||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("top").unmappedType("boolean").order(SortOrder.DESC)).withSort(new FieldSortBuilder("updatetime").unmappedType("date").order(SortOrder.DESC));
|
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(0, 10000));
|
boolQueryBuilder.must(termQuery("orgi", orgi));
|
||||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
|
||||||
list = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
|
if (!StringUtils.isBlank(cate)) {
|
||||||
}
|
boolQueryBuilder.must(termQuery("cate", cate));
|
||||||
return list;
|
}
|
||||||
}
|
if (!StringUtils.isBlank(type)) {
|
||||||
|
boolQueryBuilder.must(termQuery("type", type));
|
||||||
|
}
|
||||||
|
if (!StringUtils.isBlank(q)) {
|
||||||
|
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||||
|
}
|
||||||
|
|
||||||
|
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).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(0, 10000));
|
||||||
|
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||||
|
list = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,17 +18,18 @@ package com.chatopera.cc.persistence.es;
|
|||||||
|
|
||||||
import com.chatopera.cc.model.Topic;
|
import com.chatopera.cc.model.Topic;
|
||||||
import com.chatopera.cc.persistence.repository.XiaoEUKResultMapper;
|
import com.chatopera.cc.persistence.repository.XiaoEUKResultMapper;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.elasticsearch.index.query.*;
|
import org.elasticsearch.index.query.*;
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||||
import org.elasticsearch.search.sort.SortOrder;
|
import org.elasticsearch.search.sort.SortOrder;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.PageRequest;
|
import org.springframework.data.domain.PageRequest;
|
||||||
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
||||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||||
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -37,13 +38,12 @@ import java.util.List;
|
|||||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
||||||
private ElasticsearchTemplate elasticsearchTemplate;
|
@NonNull
|
||||||
|
private final ElasticsearchTemplate elasticsearchTemplate;
|
||||||
@Autowired
|
@NonNull
|
||||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
private final XiaoEUKResultMapper resultMapper;
|
||||||
this.elasticsearchTemplate = elasticsearchTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<Topic> getTopicByCateAndOrgi(String cate, String orgi, String q, final int p, final int ps) {
|
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));
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, resultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
@ -88,7 +88,7 @@ public class TopicRepositoryImpl implements TopicEsCommonRepository {
|
|||||||
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
searchQueryBuilder.withHighlightFields(new HighlightBuilder.Field("title").fragmentSize(200));
|
||||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, resultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
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));
|
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));
|
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||||
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
if (elasticsearchTemplate.indexExists(Topic.class)) {
|
||||||
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, new XiaoEUKResultMapper());
|
pages = elasticsearchTemplate.queryForPage(searchQuery, Topic.class, resultMapper);
|
||||||
}
|
}
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
|
@ -18,70 +18,79 @@ package com.chatopera.cc.persistence.es;
|
|||||||
|
|
||||||
import com.chatopera.cc.model.KbsTopic;
|
import com.chatopera.cc.model.KbsTopic;
|
||||||
import com.chatopera.cc.model.KbsTopicComment;
|
import com.chatopera.cc.model.KbsTopicComment;
|
||||||
|
import lombok.Setter;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.search.aggregations.Aggregations;
|
import org.elasticsearch.search.aggregations.Aggregations;
|
||||||
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
|
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
|
||||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
import org.springframework.data.domain.Pageable;
|
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.AggregatedPage;
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UKAggResultExtractor extends UKResultMapper {
|
||||||
|
|
||||||
public class UKAggResultExtractor extends UKResultMapper{
|
@Nullable
|
||||||
|
@Setter
|
||||||
|
private String term;
|
||||||
|
|
||||||
private String term ;
|
public UKAggResultExtractor(EntityMapper entityMapper, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||||
|
super(entityMapper, mappingContext);
|
||||||
|
}
|
||||||
|
|
||||||
public UKAggResultExtractor(String term){
|
@SuppressWarnings("unchecked")
|
||||||
this.term = term ;
|
@Override
|
||||||
}
|
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||||
|
Aggregations aggregations = response.getAggregations();
|
||||||
@SuppressWarnings("unchecked")
|
List<T> results = new ArrayList<T>();
|
||||||
@Override
|
long total = 0;
|
||||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
if (aggregations != null && aggregations.get(term) != null) {
|
||||||
Aggregations aggregations = response.getAggregations();
|
if (aggregations.get(term) instanceof Terms) {
|
||||||
List<T> results = new ArrayList<T>();
|
Terms agg = aggregations.get(term);
|
||||||
long total = 0 ;
|
if (agg != null) {
|
||||||
if(aggregations!=null && aggregations.get(term)!=null){
|
total = agg.getSumOfOtherDocCounts();
|
||||||
if(aggregations.get(term) instanceof Terms){
|
if (agg.getBuckets() != null && agg.getBuckets().size() > 0) {
|
||||||
Terms agg = aggregations.get(term) ;
|
for (Terms.Bucket entry : agg.getBuckets()) {
|
||||||
if(agg!=null){
|
if (clazz.equals(KbsTopic.class)) {
|
||||||
total = agg.getSumOfOtherDocCounts() ;
|
KbsTopic topic = new KbsTopic();
|
||||||
if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
topic.setCreater(entry.getKeyAsString());
|
||||||
for (Terms.Bucket entry : agg.getBuckets()) {
|
topic.setRowcount((int) entry.getDocCount());
|
||||||
if(clazz.equals(KbsTopic.class)){
|
results.add((T) topic);
|
||||||
KbsTopic topic = new KbsTopic();
|
} else if (clazz.equals(KbsTopicComment.class)) {
|
||||||
topic.setCreater(entry.getKeyAsString());
|
KbsTopicComment topicComment = new KbsTopicComment();
|
||||||
topic.setRowcount((int) entry.getDocCount());
|
topicComment.setCreater(entry.getKeyAsString());
|
||||||
results.add((T) topic) ;
|
topicComment.setRowcount((int) entry.getDocCount());
|
||||||
}else if(clazz.equals(KbsTopicComment.class)){
|
results.add((T) topicComment);
|
||||||
KbsTopicComment topicComment = new KbsTopicComment();
|
}
|
||||||
topicComment.setCreater(entry.getKeyAsString());
|
}
|
||||||
topicComment.setRowcount((int) entry.getDocCount());
|
}
|
||||||
results.add((T) topicComment) ;
|
}
|
||||||
}
|
} else if (aggregations.get(term) instanceof InternalDateHistogram) {
|
||||||
}
|
InternalDateHistogram agg = aggregations.get(term);
|
||||||
}
|
total = response.getHits().getTotalHits();
|
||||||
}
|
if (agg != null) {
|
||||||
}else if(aggregations.get(term) instanceof InternalDateHistogram){
|
// if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
||||||
InternalDateHistogram agg = aggregations.get(term) ;
|
// for (DateHistogram.Bucket entry : agg.getBuckets()) {
|
||||||
total = response.getHits().getTotalHits() ;
|
// if(clazz.equals(KbsTopic.class)){
|
||||||
if(agg!=null){
|
// KbsTopic topic = new KbsTopic();
|
||||||
// if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
// topic.setKey(entry.getKey().substring(0 , 10));
|
||||||
// for (DateHistogram.Bucket entry : agg.getBuckets()) {
|
// topic.setRowcount((int) entry.getDocCount());
|
||||||
// if(clazz.equals(KbsTopic.class)){
|
// results.add((T) topic) ;
|
||||||
// KbsTopic topic = new KbsTopic();
|
// }
|
||||||
// topic.setKey(entry.getKey().substring(0 , 10));
|
// }
|
||||||
// topic.setRowcount((int) entry.getDocCount());
|
// }
|
||||||
// results.add((T) topic) ;
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
return new AggregatedPageImpl<T>(results, pageable, total);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
return new AggregatedPageImpl<T>(results, pageable, total);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
package com.chatopera.cc.persistence.es;
|
package com.chatopera.cc.persistence.es;
|
||||||
|
|
||||||
import com.chatopera.cc.model.UKAgg;
|
import com.chatopera.cc.model.UKAgg;
|
||||||
|
import lombok.Setter;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
@ -24,43 +25,49 @@ import org.elasticsearch.search.aggregations.Aggregations;
|
|||||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||||
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
||||||
import org.springframework.data.domain.Pageable;
|
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.AggregatedPage;
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class UKAggTopResultExtractor extends UKResultMapper {
|
||||||
|
|
||||||
public class UKAggTopResultExtractor extends UKResultMapper{
|
@Setter
|
||||||
|
private String term, name;
|
||||||
|
|
||||||
private String term , name ;
|
public UKAggTopResultExtractor(EntityMapper entityMapper, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||||
|
super(entityMapper, mappingContext);
|
||||||
|
}
|
||||||
|
|
||||||
public UKAggTopResultExtractor(String term , String name){
|
|
||||||
this.term = term ;
|
|
||||||
this.name = name ;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||||
Aggregations aggregations = response.getAggregations();
|
Aggregations aggregations = response.getAggregations();
|
||||||
Terms agg = aggregations.get(term) ;
|
Terms agg = aggregations.get(term);
|
||||||
long total = agg.getSumOfOtherDocCounts() ;
|
long total = agg.getSumOfOtherDocCounts();
|
||||||
List<T> results = new ArrayList<T>();
|
List<T> results = new ArrayList<>();
|
||||||
if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
if (agg.getBuckets() != null && agg.getBuckets().size() > 0) {
|
||||||
for (int i = pageable.getPageNumber()*pageable.getPageSize();i<agg.getBuckets().size() ; i++) {
|
for (int i = pageable.getPageNumber() * pageable.getPageSize(); i < agg.getBuckets().size(); i++) {
|
||||||
Terms.Bucket entry = agg.getBuckets().get(i) ;
|
Terms.Bucket entry = agg.getBuckets().get(i);
|
||||||
if(!StringUtils.isBlank(name) && entry.getAggregations().get(name)!=null){
|
if (!StringUtils.isBlank(name) && entry.getAggregations().get(name) != null) {
|
||||||
TopHits topHits = entry.getAggregations().get(name);
|
TopHits topHits = entry.getAggregations().get(name);
|
||||||
for (SearchHit hit : topHits.getHits().getHits()) {
|
for (SearchHit hit : topHits.getHits().getHits()) {
|
||||||
T data = mapEntity(hit.getSourceAsString() , hit , clazz) ;
|
T data = mapEntity(hit.getSourceAsString(), hit, clazz);
|
||||||
if(data instanceof UKAgg){
|
if (data instanceof UKAgg) {
|
||||||
((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
|
((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
|
||||||
}
|
}
|
||||||
results.add(data) ;
|
results.add(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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.MultiGetItemResponse;
|
||||||
import org.elasticsearch.action.get.MultiGetResponse;
|
import org.elasticsearch.action.get.MultiGetResponse;
|
||||||
import org.elasticsearch.action.search.SearchResponse;
|
import org.elasticsearch.action.search.SearchResponse;
|
||||||
|
import org.elasticsearch.common.Nullable;
|
||||||
import org.elasticsearch.common.document.DocumentField;
|
import org.elasticsearch.common.document.DocumentField;
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
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.Document;
|
||||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||||
import org.springframework.data.elasticsearch.core.AbstractResultMapper;
|
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.EntityMapper;
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
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.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.mapping.PersistentProperty;
|
import org.springframework.data.mapping.PersistentProperty;
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
import org.springframework.data.mapping.context.MappingContext;
|
||||||
|
import org.springframework.lang.NonNull;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -48,26 +50,12 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class UKResultMapper extends AbstractResultMapper {
|
public class UKResultMapper extends AbstractResultMapper {
|
||||||
|
@Nullable
|
||||||
|
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||||
|
|
||||||
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
public UKResultMapper(@NonNull EntityMapper entityMapper, @Nullable 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) {
|
|
||||||
super(entityMapper);
|
super(entityMapper);
|
||||||
this.mappingContext = mappingContext;
|
this.mappingContext = mappingContext;
|
||||||
}
|
}
|
||||||
@ -75,10 +63,10 @@ public class UKResultMapper extends AbstractResultMapper {
|
|||||||
@Override
|
@Override
|
||||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||||
long totalHits = response.getHits().getTotalHits();
|
long totalHits = response.getHits().getTotalHits();
|
||||||
List<T> results = new ArrayList<T>();
|
List<T> results = new ArrayList<>();
|
||||||
for (SearchHit hit : response.getHits()) {
|
for (SearchHit hit : response.getHits()) {
|
||||||
if (hit != null) {
|
if (hit != null) {
|
||||||
T result = null;
|
T result;
|
||||||
if (StringUtils.isNotBlank(hit.getSourceAsString())) {
|
if (StringUtils.isNotBlank(hit.getSourceAsString())) {
|
||||||
result = mapEntity(hit.getSourceAsString(), hit, clazz);
|
result = mapEntity(hit.getSourceAsString(), hit, clazz);
|
||||||
} else {
|
} 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) {
|
public <T> T mapEntity(String source, SearchHit hit, Class<T> clazz) {
|
||||||
@ -181,7 +169,7 @@ public class UKResultMapper extends AbstractResultMapper {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
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()) {
|
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
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) {
|
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
|
||||||
|
|
||||||
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
||||||
|
if (persistentEntity == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
||||||
|
|
||||||
// Only deal with String because ES generated Ids are strings !
|
// 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.Document;
|
||||||
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
import org.springframework.data.elasticsearch.annotations.ScriptedField;
|
||||||
import org.springframework.data.elasticsearch.core.AbstractResultMapper;
|
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.EntityMapper;
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
|
||||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
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.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
||||||
import org.springframework.data.mapping.PersistentProperty;
|
import org.springframework.data.mapping.PersistentProperty;
|
||||||
import org.springframework.data.mapping.context.MappingContext;
|
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.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -48,161 +50,151 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class XiaoEUKResultMapper extends AbstractResultMapper {
|
public class XiaoEUKResultMapper extends AbstractResultMapper {
|
||||||
|
|
||||||
private MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
@Nullable
|
||||||
|
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext;
|
||||||
|
|
||||||
public XiaoEUKResultMapper() {
|
public XiaoEUKResultMapper(@NonNull EntityMapper entityMapper, @Nullable MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||||
super(new DefaultEntityMapper());
|
super(entityMapper);
|
||||||
}
|
this.mappingContext = mappingContext;
|
||||||
|
}
|
||||||
|
|
||||||
public XiaoEUKResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
@Override
|
||||||
super(new DefaultEntityMapper());
|
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||||
this.mappingContext = mappingContext;
|
long totalHits = response.getHits().getTotalHits();
|
||||||
}
|
List<T> results = new ArrayList<>();
|
||||||
|
for (SearchHit hit : response.getHits()) {
|
||||||
|
if (hit != null) {
|
||||||
|
T result;
|
||||||
|
if (StringUtils.isNotBlank(hit.getSourceAsString())) {
|
||||||
|
result = mapEntity(hit.getSourceAsString(), hit, clazz);
|
||||||
|
} else {
|
||||||
|
result = mapEntity(hit.getFields().values(), hit, clazz);
|
||||||
|
}
|
||||||
|
setPersistentEntityId(result, hit.getId(), clazz);
|
||||||
|
populateScriptFields(result, hit);
|
||||||
|
results.add(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public XiaoEUKResultMapper(EntityMapper entityMapper) {
|
return new AggregatedPageImpl<>(results, pageable, totalHits);
|
||||||
super(entityMapper);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public XiaoEUKResultMapper(
|
public <T> T mapEntity(String source, SearchHit hit, Class<T> clazz) {
|
||||||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
|
T t = mapEntity(source, clazz);
|
||||||
EntityMapper entityMapper) {
|
|
||||||
super(entityMapper);
|
|
||||||
this.mappingContext = mappingContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
|
||||||
long totalHits = response.getHits().getTotalHits();
|
|
||||||
List<T> results = new ArrayList<T>();
|
|
||||||
for (SearchHit hit : response.getHits()) {
|
|
||||||
if (hit != null) {
|
|
||||||
T result = null;
|
|
||||||
if (StringUtils.isNotBlank(hit.getSourceAsString())) {
|
|
||||||
result = mapEntity(hit.getSourceAsString(), hit, clazz);
|
|
||||||
} else {
|
|
||||||
result = mapEntity(hit.getFields().values(), hit, clazz);
|
|
||||||
}
|
|
||||||
setPersistentEntityId(result, hit.getId(), clazz);
|
|
||||||
populateScriptFields(result, hit);
|
|
||||||
results.add(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return new AggregatedPageImpl<T>(results, pageable, totalHits);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> T mapEntity(String source , SearchHit hit , Class<T> clazz) {
|
|
||||||
T t = mapEntity(source , clazz) ;
|
|
||||||
|
|
||||||
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
|
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
|
||||||
HighlightField highlightNameField = highlightFields.get("title");
|
HighlightField highlightNameField = highlightFields.get("title");
|
||||||
HighlightField contentHightlightField = highlightFields.get("content");
|
HighlightField contentHightlightField = highlightFields.get("content");
|
||||||
try {
|
try {
|
||||||
if(highlightNameField!=null&&highlightNameField.fragments()!=null){
|
if (highlightNameField != null && highlightNameField.fragments() != null) {
|
||||||
PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string());
|
PropertyUtils.setProperty(t, "title", highlightNameField.fragments()[0].string());
|
||||||
}
|
}
|
||||||
if(contentHightlightField!=null){
|
if (contentHightlightField != null) {
|
||||||
PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string());
|
PropertyUtils.setProperty(t, "content", contentHightlightField.fragments()[0].string());
|
||||||
}
|
}
|
||||||
PropertyUtils.setProperty(t, "id" , hit.getId());
|
PropertyUtils.setProperty(t, "id", hit.getId());
|
||||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void populateScriptFields(T result, SearchHit hit) {
|
private <T> void populateScriptFields(T result, SearchHit hit) {
|
||||||
if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) {
|
if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) {
|
||||||
for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
|
for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
|
||||||
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
|
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
|
||||||
if (scriptedField != null) {
|
if (scriptedField != null) {
|
||||||
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
|
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
|
||||||
DocumentField DocumentField = hit.getFields().get(name);
|
DocumentField DocumentField = hit.getFields().get(name);
|
||||||
if (DocumentField != null) {
|
if (DocumentField != null) {
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
try {
|
try {
|
||||||
if (name.equals("title") && hit.getHighlightFields().get("title") != null) {
|
if (name.equals("title") && hit.getHighlightFields().get("title") != null) {
|
||||||
field.set(result, hit.getHighlightFields().get("title").fragments()[0].string());
|
field.set(result, hit.getHighlightFields().get("title").fragments()[0].string());
|
||||||
} else {
|
} else {
|
||||||
field.set(result, DocumentField.getValue());
|
field.set(result, DocumentField.getValue());
|
||||||
}
|
}
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
throw new ElasticsearchException("failed to set scripted field: " + name + " with value: "
|
throw new ElasticsearchException("failed to set scripted field: " + name + " with value: "
|
||||||
+ DocumentField.getValue(), e);
|
+ DocumentField.getValue(), e);
|
||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
throw new ElasticsearchException("failed to access scripted field: " + name, e);
|
throw new ElasticsearchException("failed to access scripted field: " + name, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public <T> T mapEntity(Collection<DocumentField> values, SearchHit hit, Class<T> clazz) {
|
public <T> T mapEntity(Collection<DocumentField> values, SearchHit hit, Class<T> clazz) {
|
||||||
return mapEntity(buildJSONFromFields(values), hit, clazz);
|
return mapEntity(buildJSONFromFields(values), hit, clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String buildJSONFromFields(Collection<DocumentField> values) {
|
private String buildJSONFromFields(Collection<DocumentField> values) {
|
||||||
JsonFactory nodeFactory = new JsonFactory();
|
JsonFactory nodeFactory = new JsonFactory();
|
||||||
try {
|
try {
|
||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
|
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
|
||||||
generator.writeStartObject();
|
generator.writeStartObject();
|
||||||
for (DocumentField value : values) {
|
for (DocumentField value : values) {
|
||||||
if (value.getValues().size() > 1) {
|
if (value.getValues().size() > 1) {
|
||||||
generator.writeArrayFieldStart(value.getName());
|
generator.writeArrayFieldStart(value.getName());
|
||||||
for (Object val : value.getValues()) {
|
for (Object val : value.getValues()) {
|
||||||
generator.writeObject(val);
|
generator.writeObject(val);
|
||||||
}
|
}
|
||||||
generator.writeEndArray();
|
generator.writeEndArray();
|
||||||
} else {
|
} else {
|
||||||
generator.writeObjectField(value.getName(), value.getValue());
|
generator.writeObjectField(value.getName(), value.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
generator.writeEndObject();
|
generator.writeEndObject();
|
||||||
generator.flush();
|
generator.flush();
|
||||||
return new String(stream.toByteArray(), StandardCharsets.UTF_8);
|
return new String(stream.toByteArray(), StandardCharsets.UTF_8);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T mapResult(GetResponse response, Class<T> clazz) {
|
public <T> T mapResult(GetResponse response, Class<T> clazz) {
|
||||||
T result = mapEntity(response.getSourceAsString(), clazz);
|
T result = mapEntity(response.getSourceAsString(), clazz);
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
setPersistentEntityId(result, response.getId(), clazz);
|
setPersistentEntityId(result, response.getId(), clazz);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
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()) {
|
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
||||||
setPersistentEntityId(result, response.getResponse().getId(), clazz);
|
setPersistentEntityId(result, response.getResponse().getId(), clazz);
|
||||||
list.add(result);
|
list.add(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
|
private <T> void setPersistentEntityId(T result, String id, Class<T> clazz) {
|
||||||
|
|
||||||
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
if (mappingContext != null && clazz.isAnnotationPresent(Document.class)) {
|
||||||
|
|
||||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
||||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
if (persistentEntity == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
||||||
|
|
||||||
// Only deal with String because ES generated Ids are strings !
|
// Only deal with String because ES generated Ids are strings !
|
||||||
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
|
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
|
||||||
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
|
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user