首页 > 编程语言 > 详细

spring-boot-starter-data-elasticsearch

时间:2020-07-09 01:14:48      阅读:509      评论:0      收藏:0      [点我收藏+]

spring-boot-starter-data-elasticsearch

版本问题可查https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#preface.versions
这里的版本采用 springboot2.2.0 -> spring-boot-starter-data-elasticsearch2.3.1 -> spring-data-elasticsearch4.0 ->es7.8.0
实测api差异不大可以正常运行

依赖

        <!--es-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>

配置

spring:
  application:
    name: springboot-es-start
  elasticsearch:
    rest:
      uris: docker:9200

实体

注解说明:

  Spring Data通过注解来声明字段的映射属性,有下面的三个注解:
  @Document 作用在类,标记实体类为文档对象,一般有两个属性
    indexName:对应索引库名称
    type:对应在索引库中的类型
    shards:分片数量,默认5
    replicas:副本数量,默认1
  @Id 作用在属性,标记id主键
  @Field 作用在属性,标记为文档字段,并指定字段映射属性:
    type:字段类型,是枚举:FieldType,可以是text、long、short、date、integer、object等
    text:存储数据时候,会自动分词,并生成索引
    keyword:存储数据时候,不会分词建立索引
    Numerical:数值类型,分两类
      基本数据类型:long、interger、short、byte、double、float、half_float
      浮点数的高精度类型:scaled_float
      需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
    Date:日期类型
      elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
    index:是否索引,布尔类型,默认是true
    store:是否存储,布尔类型,默认是false
    analyzer:分词器名称,这里的ik_max_word即使用ik分词器

举个栗子
@Data
@Document(indexName = "index001", type = "_doc", shards = 1, replicas = 0)
public class User implements Serializable {

    @Id
    private Integer id;

    /*
     *   姓名
     * */
    @Field(type = FieldType.Keyword)
    private String name;

    /*
     *   爱好
     * */
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_smart")
    private String hobby;
}

dao

@Repository
public interface UserRepository extends ElasticsearchRepository<User,Integer> {
}

简易集成提供了俩个api ElasticSearchRepositoryElasticSearchRestTemplate

  • ElasticSearchRepository只需要继承即可,jpa一致

  • ElasticSearchRestTemplate简介

spring-boot-starter-data-elasticsearch

原文:https://www.cnblogs.com/renjiafu/p/13270173.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!