springboot 使用 elasticsearch(使用)
参考:Getting Started With Elasticsearch in Java Spring Boot
这一篇讲述springboot如何使用ES
开启ES,前一章已经讲了,不赘述。运行bat文件
一,POM
org.springframework.boot spring-boot-starter-data-elasticsearch 2.6.3 org.springframework.data spring-data-elasticsearch 4.3.1 org.projectlombok lombok
二,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; } }
四,效果