SpringBoot 2.0集成spring-data-elasticsearch

一、配置

spring-boot 2.0.2
spring-data-elasticsearch 3.0.7
elasticsearch 5.6.9

1. Maven依赖



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.2.RELEASE
        
    
    
    
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
    

2. application.yml配置

spring:
   data:
        elasticsearch:
            #cluster-name: #默认为elasticsearch
            cluster-nodes: 127.0.0.1:9300 #配置es节点信息,逗号分隔,如果没有指定,则启动ClientNode(9200端口是http查询使用的。9300集群使用。这里使用9300.)
            properties:
                path:
                  logs: ./elasticsearch/log #elasticsearch日志存储目录
                  data: ./elasticsearch/data #elasticsearch数据存储目录

二、使用

官方文档:https://docs.spring.io/spring...
中文翻译:https://www.jianshu.com/p/27e...
入门参考:https://www.cnblogs.com/guozp...

1. @Document

@Document注解里面的几个属性,类比mysql的话是这样:

index –> DB   
type –> Table   
Document –> row   

加上@Id注解后,在Elasticsearch里对应的该列就是主键了,在查询时就可以直接用主键查询。其实和mysql非常类似,基本就是一个数据库。

@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";//索引文件存储类型
}

2. @Field

加上了@Document注解之后,默认情况下这个实体中所有的属性都会被建立索引、并且分词。
通过@Field注解来进行详细的指定,如果没有特殊需求,那么只需要添加@Document即可。

@Field注解的定义如下:  

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Documented
@Inherited
public @interface Field {

    FieldType type() default FieldType.Auto;#自动检测属性的类型
    FieldIndex index() default FieldIndex.analyzed;#默认情况下分词
    DateFormat format() default DateFormat.none;
    String pattern() default "";
    boolean store() default false;#默认情况下不存储原文
    String searchAnalyzer() default "";#指定字段搜索时使用的分词器
    String indexAnalyzer() default "";#指定字段建立索引时指定的分词器
    String[] ignoreFields() default {};#如果某个字段需要被忽略
    boolean includeInParent() default false;
}

3. ElasticsearchRepository

//不需要加@Component,直接可以@Autowared
public interface ArticleSearchRepository extends ElasticsearchRepository {
    List findByName(String name);
    //使用 Page countrys = articleSearchRepository.findByName("测试",  PageRequest.of(0, 10)); //分页是从0开始的
    Page findByName(String name, Pageable pageable); 
    Country findProductById(String name);

}

Page的方法:

  • getTotalElements() 匹配的总共有多少条数据
  • getTotalPages() 匹配的总共有多少页
  • getSize() 用户想在当前页获取的数量
  • getNumberOfElements() 当前页实际获取的数量
  • getPageable().getPageSize() 当前页获取的数量
  • getPageable().getPageNumber() 当前是多少页(从0开始,使用的时候需要+1)

4. 示例

Country.java

@Document(indexName = "world", type = "country")
public class Country implements Serializable {

    @Id
    private Integer id;

    @Field(searchAnalyzer = "ik_max_word",analyzer = "ik_smart")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

CountrySearchRepository.java

public interface CountrySearchRepository extends ElasticsearchRepository {
    List findCountryByName(String name);
    //使用 Page countrys = countrySearchRepository.findByName("测试",  PageRequest.of(0, 10)); //分页是从0开始的
    Page findCountryByName(String name, Pageable pageable); 
    Country findCountryById(String name);

}

SearchService.java

public class SearchService{

    @Autowared
    CountrySearchRepository countrySearchRepository;
  
    public Page getCountryByName(String name) {
        Page countrys = countrySearchRepository.findCountryByName("测试",  PageRequest.of(0, 10));
        return countrys;
    }
}

你可能感兴趣的:(springboot)