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

      结果:

  • 相关阅读:
    语法专题:错误处理机制
    语法专题:数据类型的转换
    运算符:其他运算符,运算顺序
    运算符:二进制位运算符
    运算符:布尔运算符
    运算符:比较运算符
    day09 继承、super、this、抽象类
    day08 String类、Static关键字、Arrays类、Math类
    day06 Scanner类、Random类、匿名对象、ArrayList类
    day05 encapsulation
  • 原文地址:https://www.cnblogs.com/chenjiahao9527/p/13804330.html
Copyright © 2020-2023  润新知