• springboot 使用 elasticsearch(使用)


    参考:Getting Started With Elasticsearch in Java Spring Boot

    这一篇讲述springboot如何使用ES

    开启ES,前一章已经讲了,不赘述。运行bat文件

    一,POM

            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
                <version>2.6.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-elasticsearch</artifactId>
                <version>4.3.1</version>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>

    二,application.yml

    server:
      port: 9000
     
    spring:
      elasticsearch:
        rest:
          uris: localhost:9200
          connection-timeout: 1s
          read-timeout: 1m
          password:
          username:

    三,测试

    启动项添加代码

    @SpringBootApplication
    public class TestApplication {
     
        public static void main(String[] args) {
            SpringApplication.run(TestApplication.class, args);
        }
        @Bean
        public boolean createTestIndex(RestHighLevelClient restHighLevelClient) throws Exception {
            try {
                DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("hello-world");
                restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); // 1
            } catch (Exception ignored) {
            }
     
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("hello-world");
            createIndexRequest.settings(
                    Settings.builder().put("index.number_of_shards", 1)
                            .put("index.number_of_replicas", 0));
            restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); // 2
     
            return true;
        }
    }

    四,效果

  • 相关阅读:
    CSUST 4005-你真的会!(分治思维+线段树)
    CSUST 4007-你真的会图论吗?(思维-三元环)
    CSUST 4002-你真的会字符串吗?(DP)
    Odoo下拉动作列表
    Odoo Shell
    Odoo report
    Odoo Web Service API
    Odoo启动过程
    Odoo10 变化
    Odoo10尝鲜:出勤登记
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/15878083.html
Copyright © 2020-2023  润新知