Spring boot整合ElasticSearch教程

本文介绍了Elasticsearch的基本概念,如分布式数据库特性、反向索引等,并详细阐述了Springboot集成Elasticsearch的过程,包括依赖配置、yml设置、存储对象定义、Repository接口及访问接口的实现。通过案例展示了如何保存和查询数据,为后续的自定义查询和DSL语法搜索奠定了基础。
摘要由CSDN通过智能技术生成

目录

1、什么是ElasticSearch?

2、ElasticSearch的优缺点有哪些?

2.1 优点

2.2 缺点

3、Spring boot集成ElasticSearch

3.1 依赖配置

3.2 yml配置

3.3 存储对象

3.4 Repository接口

3.5 访问接口

3.6 测试结果

4、案例总结

参考文献


1、什么是ElasticSearch?

ElasticSearch本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个Elastic实例,是目前行业中非常热门的一个技术。ElasticSearch是一种分布式的海量数据搜索与分析的技术,可以用于电商网站、门户网站、企业IT系统等各种场景下的搜索引擎,也可以用于对海量的数据进行近实时的数据分析。相较于Lucene来说,ElasticSearch天然的分布式特性,让其可以支持海量的、PB级的大数据搜索。以下是个人学习ES时的笔记整理:

ElasticSearch在创建索引的过程中,采用了反向索引的方式创建索引,并具有以下特点:

(1) 反向索引又叫倒排索引,是根据文章内容中的关键字建立索引。

(2) 搜索引擎原理就是建立反向索引。

(3) Elasticsearch 在 Lucene 的基础上进行封装,实现了分布式搜索引擎。

(4) Elasticsearch 中的索引、类型和文档的概念比较重要,类似于 MySQL 中的数据库、表和行。

(5) Elasticsearch 也是 Master-slave 架构,也实现了数据的分片和备份。

(6) Elasticsearch 一个典型应用就是 ELK 日志分析系统。

2、ElasticSearch的优缺点有哪些?

2.1 优点

(1) 开箱即用,天生集群

(2) 横向扩展性好:增加机器后,只需修改配置重启服务即可

(3) 分片机制:同一个索引分布在多个分片,提供了处理效率

(4) 高可用:提供分片复制功能,服务宕机后,集群可以照常运行

(5) 速度快、负载能力强:面对海量数据时,能够快速搜索

(6) 插件支持

2.2 缺点

(1) 在需要添加新数据与新字段的时候,如果elasticSearch进行搜索是可能需要重新修改格式。之前的数据需要重新同步,对数据的管理有很多困难

(2) 字段类型不能变更

(3) 默认不支持汉字词语搜素,需要配置分词器

3、Spring boot集成ElasticSearch

3.1 依赖配置

Spring boot集成ElasticSearch的过程中需要引入web、elasticsearch、lombok、guava依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--elasticsearch-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
    </dependencies>

3.2 yml配置

yml中配置了服务启动端口、访问路径、服务名、elasticsearch集群名和节点信息

server:
  port: 8080
  servlet:
    context-path: /search
spring:
  application:
    name: search
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0.0.1:9200
      repositories:
        enabled: true

备注:在各自试验时,cluster-name属性值填写本地安装后的服务名即可。

3.3 存储对象

注解介绍:

(1) @Data:使用这个注解,就不用再去手写Getter,Setter,equals,canEqual,hasCode,toString等方法了,注解后在编译时会自动加进去;

(2) @AllArgsConstructor:使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数;

(3) @NoArgsConstructor:使用后创建一个无参构造函数;

(4) @Document:指定了Elasticsearch的索引名和类型名

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "notice_info", type = "doc")
public class Notice {

    //id
    @JsonProperty("auto_id")
    private Long id;

    //标题
    @JsonProperty("title")
    private String title;

    //公告标签
    @JsonProperty("exchange_mc")
    private String exchangeMc;

    //公告发布时间
    @JsonProperty("create_time")
    private String originCreateTime;

    //公告阅读数量
    @JsonProperty("read_count")
    private Integer readCount;

}

3.4 Repository接口

此案例中定义接口NoticeRepository接口

(1) NoticeRepository接口继承了ElasticsearchRepository接口

(2) ElasticsearchRepository接口继承了PagingAndSortingRepository接口

(3) 接口PagingAndSortingRepository继承了CrudRepository接口

(4) 接口CrudRepository继承了Repository接口。

因此服务可以调用NoticeRepository继承自CrudRepository的CRUD方法对索引进行操作,CRUD方法通过AbstractElasticsearchRepository抽象类进行实现。

@Component
public interface NoticeRepository extends ElasticsearchRepository<Notice,Long> {
}

3.5 访问接口

@RestController
@RequestMapping("/api/notice")
public class NoticeController {

    private static final Logger logger = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeRepository noticeRepository;

    @GetMapping("/save")
    public void save(long id, String title) {
        Notice notice = new Notice();
        notice.setId(id);
        notice.setReadCount(123);
        notice.setTitle(title);
        noticeRepository.save(notice);
    }

    @GetMapping("/search")
    public Optional<Notice> search(Long id, @PageableDefault(page = 0, value = 10) Pageable pageable) {
        //如果实体和数据的名称对应就会自动封装,pageable分页参数
        return noticeRepository.findById(id);

    }

}

/save接口:将信息存储在Elasticsearch

/search接口:通过Id查询通知信息

3.6 测试结果

4、案例总结

为避免篇幅过长,因此本文中的demo较简单,只是调用CRUD默认方法进行数据的新增和查询,下期将讲解以下两点

(1) 如何自定义接口方法进行数据查询

(2) 如何通过DSL语法进行Elasticsearch数据搜索。

参考文献

1、https://blog.csdn.net/uniquewonderq/article/details/107989857

2、https://developer.51cto.com/art/201904/594615.htm


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值