spring boot整合ElascticSearch

来源:互联网 发布:东北歌手最火的网络歌 编辑:程序博客网 时间:2024/06/13 18:31

忙了一天,继续我们的spring boot之旅,这一篇,我们主要讲解ElascticSearch在spring boot中的使用微笑..使用起来很简单,也没有前面的什么集群和单机配置,但是我特么竟然没成功大哭...服务器上的版本太低和本地ElascticSearch.jar包不匹配,改了半天不是影响这就影响那的....懒得弄了!就直接说下整个配置过程吧,别喷我,其实我还是挺负责的!尴尬...

等下次项目用到了,我再来补充



1:老规矩.pom.xml增加配置依赖

<!-- elasticsearch依赖包 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>

2:application.properties增加配置文件

#elasticsearch配置文件spring.data.elasticsearch.cluster-name=elasticsearch  spring.data.elasticsearch.cluster-nodes=116.62.11.42:9200,116.62.11.42:9201spring.data.elasticsearch.local=false  spring.data.elasticsearch.repositories.enabled=true  spring.data.elasticsearch.transport.ping_timeout=120

3:实体上增加ElascticSearch索引注解

package com.xyy.model;import java.io.Serializable;import java.util.Date;import org.springframework.data.elasticsearch.annotations.Document;@Document(indexName = "customer", type = "external", shards = 1, replicas = 0, refreshInterval = "-1") public class Favorite implements Serializable {    private static final long serialVersionUID = 1L;    /** 主键 */    private Long id;    /** 商品编号 */    private Long productId;    /** 商户编号 */    private Integer merchantId;    /** 状态:1-收藏,2-取消收藏 */    private Integer status;    /** 创建人 */    private String creator;    /** 创建时间 */    private Date createTime;    /**     *     * @return 返回数据库表tb_favorite的id字段值     */    public Long getId() {        return id;    }    /**     * @param id 对应数据库表tb_favorite的id字段     */    public void setId(Long id) {        this.id = id;    }    /**     *     * @return 返回数据库表tb_favorite的product_id字段值     */    public Long getProductId() {        return productId;    }    /**     * @param productId 对应数据库表tb_favorite的product_id字段     */    public void setProductId(Long productId) {        this.productId = productId;    }    /**     *     * @return 返回数据库表tb_favorite的merchant_id字段值     */    public Integer getMerchantId() {        return merchantId;    }    /**     * @param merchantId 对应数据库表tb_favorite的merchant_id字段     */    public void setMerchantId(Integer merchantId) {        this.merchantId = merchantId;    }    /**     *     * @return 返回数据库表tb_favorite的status字段值     */    public Integer getStatus() {        return status;    }    /**     * @param status 对应数据库表tb_favorite的status字段     */    public void setStatus(Integer status) {        this.status = status;    }    /**     *     * @return 返回数据库表tb_favorite的creator字段值     */    public String getCreator() {        return creator;    }    /**     * @param creator 对应数据库表tb_favorite的creator字段     */    public void setCreator(String creator) {        this.creator = creator;    }    /**     *     * @return 返回数据库表tb_favorite的create_time字段值     */    public Date getCreateTime() {        return createTime;    }    /**     * @param createTime 对应数据库表tb_favorite的create_time字段     */    public void setCreateTime(Date createTime) {        this.createTime = createTime;    }}
这个是重点:

@Document(indexName = "customer", type = "external", shards = 1, replicas = 0, refreshInterval = "-1")


具体的含义参考如下:

@Persistent@Inherited@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE})public @interface Document {         String indexName();//索引库的名称,个人建议以项目的名称命名         String type() default "";//类型,个人建议以实体的名称命名         short shards() default 5;//默认分区数         short replicas() default 1;//每个分区默认的备份数         String refreshInterval() default "1s";//刷新间隔         String indexStoreType() default "fs";//索引文件存储类型}

4:创建ElascticSearch查询接口,继承ElasticsearchRepository<T,Object>

package com.xyy.dao;import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;import com.xyy.model.Favorite;/** * Favorite持久化层接口 * @ClassName: FavoriteMapper  * @author wujing * @date 2017-07-13 15:09:50 */public interface FavoriteMapper extends ElasticsearchRepository<Favorite,Long>{Favorite selectByPrimaryKey(Long id);}

使用,直接和,普通接口调用一样就行了!

害羞草草而过,不要介意!点个关注,后期补上!