springboot怎样使用ElasticSearch
时间: 2023-05-12 16:06:03 浏览: 115
Spring Boot可以通过Elasticsearch的Java API来使用Elasticsearch。首先,需要在pom.xml文件中添加Elasticsearch的依赖:
```
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.3</version>
</dependency>
```
然后,在Spring Boot的配置文件中配置Elasticsearch的连接信息:
```
spring.data.elasticsearch.cluster-nodes=localhost:9200
```
最后,在代码中使用Elasticsearch的Java API进行操作,例如:
```
@Autowired
private RestHighLevelClient restHighLevelClient;
public void search() throws IOException {
SearchRequest searchRequest = new SearchRequest("index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("field", "value"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// 处理搜索结果
}
```
这样就可以使用Spring Boot来操作Elasticsearch了。
阅读全文