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());
    }

  结果:

 

posted @ 2020-10-12 18:04  chenjiahao  阅读(277)  评论(0编辑  收藏  举报