springboot 使用 elasticsearch(使用)
Posted 正怒月神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
四,效果
以上是关于springboot 使用 elasticsearch(使用)的主要内容,如果未能解决你的问题,请参考以下文章
Spring Cloud 分布式链路跟踪 Sleuth + Zipkin + Elasticsear