Search in sources :

Example 1 with ESBlogIndex

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);
}
Also used : ESBlogIndex(com.moxi.mogublog.search.pojo.ESBlogIndex) Blog(com.moxi.mogublog.commons.entity.Blog) ApiOperation(io.swagger.annotations.ApiOperation)

Example 2 with ESBlogIndex

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;
}
Also used : ESBlogIndex(com.moxi.mogublog.search.pojo.ESBlogIndex) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) NativeSearchQueryBuilder(org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 3 with ESBlogIndex

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;
}
Also used : ESBlogIndex(com.moxi.mogublog.search.pojo.ESBlogIndex) ArrayList(java.util.ArrayList) Tag(com.moxi.mogublog.commons.entity.Tag)

Aggregations

ESBlogIndex (com.moxi.mogublog.search.pojo.ESBlogIndex)3 ArrayList (java.util.ArrayList)2 Blog (com.moxi.mogublog.commons.entity.Blog)1 Tag (com.moxi.mogublog.commons.entity.Tag)1 ApiOperation (io.swagger.annotations.ApiOperation)1 HashMap (java.util.HashMap)1 QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)1 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)1 NativeSearchQueryBuilder (org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder)1