use of com.moxi.mogublog.search.pojo.ESBlogIndex in project mogu_blog_v2 by moxi624.
the class ElasticSearchRestApi method addElasticSearchIndexByUid.
@ApiOperation(value = "ElasticSearch通过博客Uid添加索引", notes = "添加博客", response = String.class)
@PostMapping("/addElasticSearchIndexByUid")
public String addElasticSearchIndexByUid(@RequestParam(required = true) String uid) {
String result = webFeignClient.getBlogByUid(uid);
Blog eblog = WebUtils.getData(result, Blog.class);
if (eblog == null) {
return ResultUtil.result(SysConf.ERROR, MessageConf.INSERT_FAIL);
}
ESBlogIndex blog = searchService.buidBlog(eblog);
blogRepository.save(blog);
return ResultUtil.result(SysConf.SUCCESS, MessageConf.INSERT_SUCCESS);
}
use of com.moxi.mogublog.search.pojo.ESBlogIndex in project mogu_blog_v2 by moxi624.
the class ElasticSearchService method search.
public Map<String, Object> search(String keywords, Integer currentPage, Integer pageSize) {
currentPage = Math.max(currentPage - 1, 0);
List<HighlightBuilder.Field> highlightFields = new ArrayList<>();
HighlightBuilder.Field titleField = new HighlightBuilder.Field(SysConf.TITLE).preTags("<span style='color:red'>").postTags("</span>");
HighlightBuilder.Field summaryField = new HighlightBuilder.Field(SysConf.SUMMARY).preTags("<span style='color:red'>").postTags("</span>");
highlightFields.add(titleField);
highlightFields.add(summaryField);
HighlightBuilder.Field[] highlightFieldsAry = highlightFields.toArray(new HighlightBuilder.Field[highlightFields.size()]);
// 创建查询构造器
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
queryBuilder.withPageable(PageRequest.of(currentPage, pageSize));
// 过滤
QueryStringQueryBuilder queryStrBuilder = new QueryStringQueryBuilder(keywords);
queryStrBuilder.field("title", 0.75F).field("summary", 0.75F).field("content", 0.1F);
queryBuilder.withQuery(queryStrBuilder);
queryBuilder.withHighlightFields(highlightFieldsAry);
log.error("查询语句:{}", queryBuilder.build().getQuery().toString());
// 查询
AggregatedPage<ESBlogIndex> result = elasticsearchTemplate.queryForPage(queryBuilder.build(), ESBlogIndex.class, highlightResultHelper);
// 解析结果
long total = result.getTotalElements();
int totalPage = result.getTotalPages();
List<ESBlogIndex> blogList = result.getContent();
Map<String, Object> map = new HashMap<>();
map.put(SysConf.TOTAL, total);
map.put(SysConf.TOTAL_PAGE, totalPage);
map.put(SysConf.PAGE_SIZE, pageSize);
map.put(SysConf.CURRENT_PAGE, currentPage + 1);
map.put(SysConf.BLOG_LIST, blogList);
return map;
}
use of com.moxi.mogublog.search.pojo.ESBlogIndex in project mogu_blog_v2 by moxi624.
the class ElasticSearchService method buidBlog.
public ESBlogIndex buidBlog(Blog eblog) {
// 构建blog对象
ESBlogIndex blog = new ESBlogIndex();
blog.setId(eblog.getUid());
blog.setOid(eblog.getOid());
blog.setUid(eblog.getUid());
blog.setTitle(eblog.getTitle());
blog.setType(eblog.getType());
blog.setSummary(eblog.getSummary());
blog.setContent(eblog.getContent());
if (eblog.getBlogSort() != null) {
blog.setBlogSortName(eblog.getBlogSort().getSortName());
blog.setBlogSortUid(eblog.getBlogSortUid());
}
if (eblog.getTagList() != null) {
List<Tag> tagList = eblog.getTagList();
List<String> tagUidList = new ArrayList<>();
List<String> tagNameList = new ArrayList<>();
tagList.forEach(item -> {
if (item != null) {
tagUidList.add(item.getUid());
tagNameList.add(item.getContent());
}
});
blog.setTagNameList(tagNameList);
blog.setTagUidList(tagUidList);
}
blog.setIsPublish(eblog.getIsPublish());
blog.setAuthor(eblog.getAuthor());
blog.setCreateTime(eblog.getCreateTime());
if (eblog.getPhotoList() != null && eblog.getPhotoList().size() > 0) {
blog.setPhotoUrl(eblog.getPhotoList().get(0));
} else {
blog.setPhotoUrl("");
}
return blog;
}
Aggregations