SpringBoot: ElasticSearch integration

1 Introduction

1.1 Integrated mode

Integrated Elasticsearch There are four ways in Spring Boot:

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

Herein respectively connected to the rear and two modes operate Elasticsearch

1.2. Environment and Configuration

Server: elasticsearch-7.3.2 1 Taiwan
Client: elasticsearch 6.8.4

Server configuration file: elasticsearch.yml

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

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

1.3 Basic Concepts

Elasticsearch also a full-text search based on Lucene library, but also the nature of the stored data, many of the concepts and MySQL similar.

concept Control mysql Explanation
indices (index) Databases Database indices is the index of the complex, on behalf of many of the index,
type (type) Table Data Sheet Simulation type is the mysql table concept, a lower index database can have different types of indexes, such as commodity index, index order, which is different data formats. But this can lead to confusion index library, so future versions of this concept will be removed
document (document) Row row The original data is stored in the index database. For example, each piece of product information is a document
field (field) Columns Column Document properties
mappings (mapping)   Field's data type, attributes, whether the index, whether to store other properties

In addition, there are a number of clusters in Elasticsearch related concepts:

  • Index set (Indices, index complex): the complete logical index
  • Fragment (shard): parts of the split data
  • Copy (replica): copying the or each slice

Note: Elasticsearch itself is distributed, so even if you have only one node, Elasticsearch will default to your data fragmentation and copy operations when you add new data to the cluster, the data will be carried out in the new node's balance.

2. code implementation

First we have to create a new SpringBoot project, and then integrate the Elasticsearch.

2.1 pom-dependent

<?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 starter (must) ->
        <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>

 

Guess you like

Origin www.cnblogs.com/mengY/p/11962487.html