ElasticSearch实战:个人博客搜索和首页内容展示

前言

最近学习了SpringBoot,搞了一个小型博客系统,在这个系统中的首页内容展示、全文搜索用到了elasticsearch。系统中持久层使用的是Springdata,但是没有使用spring-data-elasticsearch,而是单独引入的elasticsearch、transport依赖。下面就将我系统中涉及到的关于elasticsearch的代码贴出来、供大家一起学习。

准备环境

Elasticsearch5.5.2下载(windows系统直接解压进行安装,linux系统安装参考

es中myBlog的mapping

    {  
        "settings":{  
             "analysis" : {  
                "analyzer" : {  
                    "ik" : {  
                        "tokenizer" : "ik_max_word" }  
                }  
            }  
        },  
        "mappings":{  
            "blog":{  
                "_all": {  
                    "enabled": true  
                },  
            "properties":{  
                "id":{  
                    "type":"keyword" },  
                "blogId":{  
                    "type":"keyword" },  
                "title":{  
                    "type":"text",  
                    "analyzer": "ik_max_word" },  
                "summary":{  
                    "type":"text",  
                    "analyzer": "ik_max_word" },  
                "content":{
                    "type":"text",  
                    "analyzer": "ik_max_word" },
                "username":{
                    "type":"keyword" },  
                "avatar":{
                    "type":"keyword" }, 
                "createTime":{  
                    "type":"date" },
                "readSize":{  
                    "type":"integer" },  
                "commentSize":{  
                    "type":"integer" },
                "voteSize":{  
                    "type":"integer" },
                "tags":{  
                    "type":"text",  
                    "analyzer": "ik_max_word" ,
                    "fielddata": true }  
               }  
            }  
        }  
    } 

引入依赖

<!-- es -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>
        <dependency>
           <groupId>org.elasticsearch</groupId>
           <artifactId>elasticsearch</artifactId>
           <version>5.5.2</version>
        </dependency>

返回数据包装类EsBlogVO:

public class EsBlogVO implements Serializable {
   

    private static final long serialVersionUID = 1L;

    private String id;

    private Long blogId; // Blog 的 id

    private String title;

    private String summary;

    private String content;

    private String username;

    private String avatar;

    private Timestamp createTime;

    private Integer readSize = 0; // 访问量、阅读量

    private Integer commentSize = 0; // 评论量

    private Integer voteSize = 0; // 点赞量

    private String tags; // 标签  

    public EsBlogVO(){

    }

    public EsBlogVO(Blog blog){
        this.blogId = blog.getId();
        this.title = blog.getTitle();
        this.summary = blog.getSummary();
        this.content = blog.getContent();
        this.username = blog.getUser().getUsername();
        this.avatar = blog.getUser().getAvatar();
        this.createTime = blog.getCreateTime();
        this.readSize = blog.getReading();
        this.commentSize = blog.getCommentSize();
        this.voteSize = blog.getVoteSize();
        this.tags = blog.getTags();
    }

    public void update(Blog blog){
        this.blogId = blog.getId();
        this.title = blog.getTitle();
        this.summary = blog.getSummary();
        this.content = blog.getContent();
        this.username = blog.getUser().getUsername();
        this.avatar = blog.getUser().getAvatar();
        this.createTime = blog.getCreateTime();
        this.readSize = blog.getReading();
        this.commentSize = blog.getCommentSize();
        this.voteSize = blog.getVoteSize();
        this.tags = blog.getTags();
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public Long getBlogId() {
        return blogId;
    }
    public void setBlogId(Long blogId) {
        this.blogId = blogId;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }
    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    public String getAvatar() {
        return avatar;
    }
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public Timestamp getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Timestamp createTime) {
        this.createTime = createTime;
    }

    public Integer getReadSize() {
        
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值