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,71 +38,80 @@ import java.util.List;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
@Component
|
||||
public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRepository{
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@RequiredArgsConstructor
|
||||
public class KbsTopicCommentRepositoryImpl implements KbsTopicCommentEsCommonRepository {
|
||||
|
||||
@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;
|
||||
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
|
||||
public List<KbsTopicComment> findByOptimal(String dataid) {
|
||||
List<KbsTopicComment> commentList = null ;
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid" , dataid)).withQuery(termQuery("optimal" , true)).build();
|
||||
if(elasticsearchTemplate.indexExists(KbsTopicComment.class)){
|
||||
commentList = elasticsearchTemplate.queryForList(searchQuery, KbsTopicComment.class);
|
||||
}
|
||||
return commentList ;
|
||||
}
|
||||
@Override
|
||||
public List<KbsTopicComment> findByOptimal(String dataid) {
|
||||
List<KbsTopicComment> commentList = null;
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(termQuery("dataid", dataid)).withQuery(termQuery("optimal", true)).build();
|
||||
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||
commentList = elasticsearchTemplate.queryForList(searchQuery, KbsTopicComment.class);
|
||||
}
|
||||
return commentList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<KbsTopicComment> findByCon(NativeSearchQueryBuilder searchQueryBuilder , String field , String aggname, String q , final int p , final int ps) {
|
||||
Page<KbsTopicComment> pages = null ;
|
||||
if(!StringUtils.isBlank(q)){
|
||||
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||
}
|
||||
SearchQuery searchQuery = searchQueryBuilder.build();
|
||||
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKResultMapper());
|
||||
} else {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKAggTopResultExtractor(field, aggname));
|
||||
}
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@Override
|
||||
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
|
||||
public Page<KbsTopicComment> findByCon(NativeSearchQueryBuilder searchQueryBuilder, String field, String aggname, String q, final int p, final int ps) {
|
||||
Page<KbsTopicComment> pages = null;
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
searchQueryBuilder.withQuery(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||
}
|
||||
SearchQuery searchQuery = searchQueryBuilder.build();
|
||||
if (elasticsearchTemplate.indexExists(KbsTopicComment.class)) {
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, resultMapper);
|
||||
} else {
|
||||
ukAggTopResultExtractor.setTerm(field);
|
||||
ukAggTopResultExtractor.setName(aggname);
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, ukAggTopResultExtractor);
|
||||
}
|
||||
}
|
||||
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)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, KbsTopicComment.class, new UKAggResultExtractor("creater"));
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@Override
|
||||
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, resultMapper);
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
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,176 +40,179 @@ import java.util.List;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
||||
|
||||
@Component
|
||||
public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository{
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
@RequiredArgsConstructor
|
||||
public class QuickReplyRepositoryImpl implements QuickReplyEsCommonRepository {
|
||||
@NonNull
|
||||
private final UKResultMapper ukResultMapper;
|
||||
|
||||
@Autowired
|
||||
public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
|
||||
this.elasticsearchTemplate = elasticsearchTemplate ;
|
||||
@NonNull
|
||||
private final 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();
|
||||
boolQueryBuilder.must(termQuery("cate" , cate)) ;
|
||||
List<QuickReply> pages = null;
|
||||
|
||||
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, new UKResultMapper());
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(termQuery("orgi", orgi));
|
||||
|
||||
@Override
|
||||
public List<QuickReply> findByOrgiAndCreater(String orgi ,String creater , String q) {
|
||||
BoolQueryBuilder quickQueryBuilder = QueryBuilders.boolQuery();
|
||||
|
||||
List<QuickReply> pages = null ;
|
||||
quickQueryBuilder.should(termQuery("type", MainContext.QuickType.PUB.toString()));
|
||||
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
||||
BoolQueryBuilder priQueryBuilder = QueryBuilders.boolQuery();
|
||||
|
||||
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())) ;
|
||||
priQueryBuilder.must(termQuery("creater" , creater)) ;
|
||||
|
||||
quickQueryBuilder.should(priQueryBuilder);
|
||||
|
||||
boolQueryBuilder.must(quickQueryBuilder);
|
||||
|
||||
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(PageRequest.of(0, 10000));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
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(PageRequest.of(0, 10000));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForList(searchQuery, QuickReply.class);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Page<QuickReply> getByQuicktype(String quicktype , final int p , final int ps) {
|
||||
@Override
|
||||
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.must(termQuery("type", quicktype));
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
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));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
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, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<QuickReply> getByCateAndUser(String cate , String q , String user ,final int p , final int ps) {
|
||||
@Override
|
||||
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.must(termQuery("cate", cate));
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(termQuery("cate", cate));
|
||||
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||
}
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
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));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class, new UKResultMapper());
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
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, ukResultMapper);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<QuickReply> getByCon(BoolQueryBuilder boolQueryBuilder, final int p , final int ps) {
|
||||
@Override
|
||||
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 endFilter = QueryBuilders.boolQuery().should(QueryBuilders.existsQuery("endtime")).should(QueryBuilders.rangeQuery("endtime").to(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()));
|
||||
|
||||
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));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
@Override
|
||||
public Page<QuickReply> getByOrgiAndType(String orgi ,String type, String q , Pageable page) {
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(PageRequest.of(p, ps));
|
||||
if (elasticsearchTemplate.indexExists(QuickReply.class)) {
|
||||
pages = elasticsearchTemplate.queryForPage(searchQuery, QuickReply.class);
|
||||
}
|
||||
return pages;
|
||||
}
|
||||
|
||||
Page<QuickReply> list = null ;
|
||||
@Override
|
||||
public Page<QuickReply> getByOrgiAndType(String orgi, String type, String q, Pageable page) {
|
||||
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
||||
if(!StringUtils.isBlank(type)) {
|
||||
boolQueryBuilder.must(termQuery("type" , type)) ;
|
||||
}
|
||||
Page<QuickReply> list = null;
|
||||
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||
}
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
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));
|
||||
SearchQuery searchQuery = searchQueryBuilder.build().setPageable(page);
|
||||
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);
|
||||
}
|
||||
if (!StringUtils.isBlank(q)) {
|
||||
boolQueryBuilder.must(new QueryStringQueryBuilder(q).defaultOperator(Operator.AND));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<QuickReply> getQuickReplyByOrgi(String orgi , String cate,String type, String q) {
|
||||
NativeSearchQueryBuilder searchQueryBuilder = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).withSort(new FieldSortBuilder("createtime").unmappedType("date").order(SortOrder.DESC));
|
||||
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();
|
||||
boolQueryBuilder.must(termQuery("orgi" , orgi)) ;
|
||||
@Override
|
||||
public List<QuickReply> getQuickReplyByOrgi(String orgi, String cate, String type, String q) {
|
||||
|
||||
if(!StringUtils.isBlank(cate)){
|
||||
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));
|
||||
}
|
||||
List<QuickReply> list = null;
|
||||
|
||||
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;
|
||||
}
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(termQuery("orgi", orgi));
|
||||
|
||||
if (!StringUtils.isBlank(cate)) {
|
||||
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));
|
||||
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.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;
|
||||
}
|
||||
|
@ -1,87 +1,96 @@
|
||||
/*
|
||||
* Copyright (C) 2017 优客服-多渠道客服系统
|
||||
* Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.KbsTopic;
|
||||
import com.chatopera.cc.model.KbsTopicComment;
|
||||
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.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class UKAggResultExtractor extends UKResultMapper{
|
||||
|
||||
private String term ;
|
||||
|
||||
public UKAggResultExtractor(String term){
|
||||
this.term = term ;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
Aggregations aggregations = response.getAggregations();
|
||||
List<T> results = new ArrayList<T>();
|
||||
long total = 0 ;
|
||||
if(aggregations!=null && aggregations.get(term)!=null){
|
||||
if(aggregations.get(term) instanceof Terms){
|
||||
Terms agg = aggregations.get(term) ;
|
||||
if(agg!=null){
|
||||
total = agg.getSumOfOtherDocCounts() ;
|
||||
if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
||||
for (Terms.Bucket entry : agg.getBuckets()) {
|
||||
if(clazz.equals(KbsTopic.class)){
|
||||
KbsTopic topic = new KbsTopic();
|
||||
topic.setCreater(entry.getKeyAsString());
|
||||
topic.setRowcount((int) entry.getDocCount());
|
||||
results.add((T) topic) ;
|
||||
}else if(clazz.equals(KbsTopicComment.class)){
|
||||
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){
|
||||
// if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
||||
// for (DateHistogram.Bucket entry : agg.getBuckets()) {
|
||||
// if(clazz.equals(KbsTopic.class)){
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2017 优客服-多渠道客服系统
|
||||
* Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
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(EntityMapper entityMapper, MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(entityMapper, mappingContext);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
|
||||
Aggregations aggregations = response.getAggregations();
|
||||
List<T> results = new ArrayList<T>();
|
||||
long total = 0;
|
||||
if (aggregations != null && aggregations.get(term) != null) {
|
||||
if (aggregations.get(term) instanceof Terms) {
|
||||
Terms agg = aggregations.get(term);
|
||||
if (agg != null) {
|
||||
total = agg.getSumOfOtherDocCounts();
|
||||
if (agg.getBuckets() != null && agg.getBuckets().size() > 0) {
|
||||
for (Terms.Bucket entry : agg.getBuckets()) {
|
||||
if (clazz.equals(KbsTopic.class)) {
|
||||
KbsTopic topic = new KbsTopic();
|
||||
topic.setCreater(entry.getKeyAsString());
|
||||
topic.setRowcount((int) entry.getDocCount());
|
||||
results.add((T) topic);
|
||||
} else if (clazz.equals(KbsTopicComment.class)) {
|
||||
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) {
|
||||
// if(agg.getBuckets()!=null && agg.getBuckets().size()>0){
|
||||
// for (DateHistogram.Bucket entry : agg.getBuckets()) {
|
||||
// if(clazz.equals(KbsTopic.class)){
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
@ -1,66 +1,73 @@
|
||||
/*
|
||||
* Copyright (C) 2017 优客服-多渠道客服系统
|
||||
* Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.chatopera.cc.persistence.es;
|
||||
|
||||
import com.chatopera.cc.model.UKAgg;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
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.aggregation.AggregatedPage;
|
||||
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class UKAggTopResultExtractor extends UKResultMapper{
|
||||
|
||||
private String term , name ;
|
||||
|
||||
public UKAggTopResultExtractor(String term , String name){
|
||||
this.term = term ;
|
||||
this.name = name ;
|
||||
}
|
||||
|
||||
@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>();
|
||||
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) ;
|
||||
if(!StringUtils.isBlank(name) && entry.getAggregations().get(name)!=null){
|
||||
TopHits topHits = entry.getAggregations().get(name);
|
||||
for (SearchHit hit : topHits.getHits().getHits()) {
|
||||
T data = mapEntity(hit.getSourceAsString() , hit , clazz) ;
|
||||
if(data instanceof UKAgg){
|
||||
((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
|
||||
}
|
||||
results.add(data) ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new AggregatedPageImpl<T>(results, pageable, total);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* Copyright (C) 2017 优客服-多渠道客服系统
|
||||
* Modifications copyright (C) 2018-2019 Chatopera Inc, <https://www.chatopera.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
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;
|
||||
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(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<>();
|
||||
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);
|
||||
if (!StringUtils.isBlank(name) && entry.getAggregations().get(name) != null) {
|
||||
TopHits topHits = entry.getAggregations().get(name);
|
||||
for (SearchHit hit : topHits.getHits().getHits()) {
|
||||
T data = mapEntity(hit.getSourceAsString(), hit, clazz);
|
||||
if (data instanceof UKAgg) {
|
||||
((UKAgg) data).setRowcount((int) topHits.getHits().getTotalHits());
|
||||
}
|
||||
results.add(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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,161 +50,151 @@ 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(@NonNull EntityMapper entityMapper, @Nullable MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(entityMapper);
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
public XiaoEUKResultMapper(MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) {
|
||||
super(new DefaultEntityMapper());
|
||||
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<>();
|
||||
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) {
|
||||
super(entityMapper);
|
||||
}
|
||||
return new AggregatedPageImpl<>(results, pageable, totalHits);
|
||||
}
|
||||
|
||||
public XiaoEUKResultMapper(
|
||||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext,
|
||||
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) ;
|
||||
public <T> T mapEntity(String source, SearchHit hit, Class<T> clazz) {
|
||||
T t = mapEntity(source, clazz);
|
||||
|
||||
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
|
||||
HighlightField highlightNameField = highlightFields.get("title");
|
||||
HighlightField contentHightlightField = highlightFields.get("content");
|
||||
try {
|
||||
if(highlightNameField!=null&&highlightNameField.fragments()!=null){
|
||||
PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string());
|
||||
}
|
||||
if(contentHightlightField!=null){
|
||||
PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string());
|
||||
}
|
||||
PropertyUtils.setProperty(t, "id" , hit.getId());
|
||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
HighlightField highlightNameField = highlightFields.get("title");
|
||||
HighlightField contentHightlightField = highlightFields.get("content");
|
||||
try {
|
||||
if (highlightNameField != null && highlightNameField.fragments() != null) {
|
||||
PropertyUtils.setProperty(t, "title", highlightNameField.fragments()[0].string());
|
||||
}
|
||||
if (contentHightlightField != null) {
|
||||
PropertyUtils.setProperty(t, "content", contentHightlightField.fragments()[0].string());
|
||||
}
|
||||
PropertyUtils.setProperty(t, "id", hit.getId());
|
||||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return t;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
private <T> void populateScriptFields(T result, SearchHit hit) {
|
||||
if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) {
|
||||
for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
|
||||
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
|
||||
if (scriptedField != null) {
|
||||
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
|
||||
DocumentField DocumentField = hit.getFields().get(name);
|
||||
if (DocumentField != null) {
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
if (name.equals("title") && hit.getHighlightFields().get("title") != null) {
|
||||
field.set(result, hit.getHighlightFields().get("title").fragments()[0].string());
|
||||
} else {
|
||||
field.set(result, DocumentField.getValue());
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ElasticsearchException("failed to set scripted field: " + name + " with value: "
|
||||
+ DocumentField.getValue(), e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new ElasticsearchException("failed to access scripted field: " + name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private <T> void populateScriptFields(T result, SearchHit hit) {
|
||||
if (hit.getFields() != null && !hit.getFields().isEmpty() && result != null) {
|
||||
for (java.lang.reflect.Field field : result.getClass().getDeclaredFields()) {
|
||||
ScriptedField scriptedField = field.getAnnotation(ScriptedField.class);
|
||||
if (scriptedField != null) {
|
||||
String name = scriptedField.name().isEmpty() ? field.getName() : scriptedField.name();
|
||||
DocumentField DocumentField = hit.getFields().get(name);
|
||||
if (DocumentField != null) {
|
||||
field.setAccessible(true);
|
||||
try {
|
||||
if (name.equals("title") && hit.getHighlightFields().get("title") != null) {
|
||||
field.set(result, hit.getHighlightFields().get("title").fragments()[0].string());
|
||||
} else {
|
||||
field.set(result, DocumentField.getValue());
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ElasticsearchException("failed to set scripted field: " + name + " with value: "
|
||||
+ DocumentField.getValue(), e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new ElasticsearchException("failed to access scripted field: " + name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public <T> T mapEntity(Collection<DocumentField> values, SearchHit hit, Class<T> clazz) {
|
||||
return mapEntity(buildJSONFromFields(values), hit, clazz);
|
||||
}
|
||||
public <T> T mapEntity(Collection<DocumentField> values, SearchHit hit, Class<T> clazz) {
|
||||
return mapEntity(buildJSONFromFields(values), hit, clazz);
|
||||
}
|
||||
|
||||
private String buildJSONFromFields(Collection<DocumentField> values) {
|
||||
JsonFactory nodeFactory = new JsonFactory();
|
||||
try {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
|
||||
generator.writeStartObject();
|
||||
for (DocumentField value : values) {
|
||||
if (value.getValues().size() > 1) {
|
||||
generator.writeArrayFieldStart(value.getName());
|
||||
for (Object val : value.getValues()) {
|
||||
generator.writeObject(val);
|
||||
}
|
||||
generator.writeEndArray();
|
||||
} else {
|
||||
generator.writeObjectField(value.getName(), value.getValue());
|
||||
}
|
||||
}
|
||||
private String buildJSONFromFields(Collection<DocumentField> values) {
|
||||
JsonFactory nodeFactory = new JsonFactory();
|
||||
try {
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
|
||||
generator.writeStartObject();
|
||||
for (DocumentField value : values) {
|
||||
if (value.getValues().size() > 1) {
|
||||
generator.writeArrayFieldStart(value.getName());
|
||||
for (Object val : value.getValues()) {
|
||||
generator.writeObject(val);
|
||||
}
|
||||
generator.writeEndArray();
|
||||
} else {
|
||||
generator.writeObjectField(value.getName(), value.getValue());
|
||||
}
|
||||
}
|
||||
generator.writeEndObject();
|
||||
generator.flush();
|
||||
return new String(stream.toByteArray(), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T mapResult(GetResponse response, Class<T> clazz) {
|
||||
T result = mapEntity(response.getSourceAsString(), clazz);
|
||||
if (result != null) {
|
||||
setPersistentEntityId(result, response.getId(), clazz);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@Override
|
||||
public <T> T mapResult(GetResponse response, Class<T> clazz) {
|
||||
T result = mapEntity(response.getSourceAsString(), clazz);
|
||||
if (result != null) {
|
||||
setPersistentEntityId(result, response.getId(), clazz);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
||||
LinkedList<T> list = new LinkedList<T>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
||||
setPersistentEntityId(result, response.getResponse().getId(), clazz);
|
||||
list.add(result);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
@Override
|
||||
public <T> LinkedList<T> mapResults(MultiGetResponse responses, Class<T> clazz) {
|
||||
LinkedList<T> list = new LinkedList<>();
|
||||
for (MultiGetItemResponse response : responses.getResponses()) {
|
||||
if (!response.isFailed() && response.getResponse().isExists()) {
|
||||
T result = mapEntity(response.getResponse().getSourceAsString(), clazz);
|
||||
setPersistentEntityId(result, response.getResponse().getId(), clazz);
|
||||
list.add(result);
|
||||
}
|
||||
}
|
||||
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);
|
||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
||||
ElasticsearchPersistentEntity<?> persistentEntity = mappingContext.getPersistentEntity(clazz);
|
||||
if (persistentEntity == null) {
|
||||
return;
|
||||
}
|
||||
PersistentProperty<?> idProperty = persistentEntity.getIdProperty();
|
||||
|
||||
// Only deal with String because ES generated Ids are strings !
|
||||
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
|
||||
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (idProperty != null && idProperty.getType().isAssignableFrom(String.class)) {
|
||||
persistentEntity.getPropertyAccessor(result).setProperty(idProperty, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user