SpringBoot:ElasticSearch集成

1.前言

1.1.  集成方式

Spring Boot中集成Elasticsearch有4种方式:

  1. REST Client
  2. Jest
  3. Spring Data
  4. Spring Data Elasticsearch Repositories

本文用后面两种方式来分别连接并操作Elasticsearch

1.2.  环境与配置

服务端:elasticsearch-7.3.2 1台
客户端:elasticsearch 6.8.4

服务端配置文件:elasticsearch.yml

cluster.name: elasticsearch
node.name: esNode01
network.host: 0.0.0.0

#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"

1.3 基本概念

Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。

概念 对照mysql 说明
indices(索引) Databases 数据库 indices是index的复数,代表许多的索引,
type(类型) Table 数据表 类型是模拟mysql中的table概念,一个索引库下可以有不同类型的索引,比如商品索引,订单索引,其数据格式不同。不过这会导致索引库混乱,因此未来版本中会移除这个概念
document(文档) Row 行 存入索引库原始的数据。比如每一条商品信息,就是一个文档
field(字段) Columns 列 文档中的属性
mappings(映射)   字段的数据类型、属性、是否索引、是否存储等特性

另外,在Elasticsearch有一些集群相关的概念:

  • 索引集(Indices,index的复数):逻辑上的完整索引
  • 分片(shard):数据拆分后的各个部分
  • 副本(replica):每个分片的复制

注意:Elasticsearch本身就是分布式的,因此即便你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。

2.代码实现

首先我们要新建一个SpringBoot项目,再进行Elasticsearch的整合。

2.1 pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.meng</groupId>
    <artifactId>es</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>es</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- elasticsearch启动器 (必须)-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

猜你喜欢

转载自www.cnblogs.com/mengY/p/11962487.html