1. 程式人生 > 實用技巧 >springboot整合elasticsearch 01

springboot整合elasticsearch 01

建立springboot專案:

pom.xml

<?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.5.RELEASE</version> //這裡我降低了springboot的版本
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cjh</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>elasticsearch</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <!--自定義es版本依賴,保證和安裝的版本一致-->
        <elasticsearch.version>7.6.0</elasticsearch.version>
    </properties>

    <dependencies>

   

        <!--fastajson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>

        <!-- 匯入了 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-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <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>
            <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>

  把連線es的ip和埠交給spring容器:

package com.cjh.elasticsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author
 * @site
 * @company
 * @create 2020-10-08 21:57
 */
@Configuration
public class ElasticSearchClientConfig {


    @Bean
    public RestHighLevelClient restHighLevelClient(){
       RestHighLevelClient client = new RestHighLevelClient(
               RestClient.builder(
                       new HttpHost("192.168.198.151",9200,"http")));
       return client;
    }
}

  測試:

 @Autowired
    @Qualifier("restHighLevelClient") //來指出我們想要使用哪個 bean 來解決問題
    private RestHighLevelClient client;

  測試索引的建立:

    @Test
    void testCreateIndex() throws IOException {
        //建立索引請求
        CreateIndexRequest request = new CreateIndexRequest("index1索引");
        //客戶端執行請求
        CreateIndexResponse response = client.indices().create(request,RequestOptions.DEFAULT);
        System.out.println(response);
    }

  結果:

測試索引是否存在:

  @Test
    void testGetIndex() throws IOException {
        //測試索引
        GetIndexRequest request = new GetIndexRequest("index1");
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }

  結果:

刪除索引:

/*刪除索引*/
    @Test
    void testDeleteIndex() throws IOException {
        DeleteIndexRequest request = new DeleteIndexRequest("index");
        /*刪除*/
        AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);

        System.out.println(delete.isAcknowledged());
    }

  結果: