springboot整合es
时间: 2023-09-08 14:08:31 浏览: 168
Spring Boot可以很容易地与Elasticsearch集成,以下是一些步骤:
1. 添加Elasticsearch依赖
在pom.xml中添加以下依赖项:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.1</version>
</dependency>
```
2. 配置Elasticsearch连接
在application.properties中添加以下配置:
```properties
spring.elasticsearch.rest.uris=http://localhost:9200
```
3. 创建Elasticsearch客户端
创建一个ElasticsearchClient类来创建RestHighLevelClient实例:
```java
@Configuration
public class ElasticsearchClient {
private RestHighLevelClient restHighLevelClient;
@Bean(destroyMethod = "close")
public RestHighLevelClient restHighLevelClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
restHighLevelClient = RestClients.create(clientConfiguration).rest();
return restHighLevelClient;
}
@PreDestroy
public void close() {
if (restHighLevelClient != null) {
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
4. 创建Elasticsearch存储库
创建一个ElasticsearchRepository类来操作Elasticsearch:
```java
@Repository
public class ElasticsearchRepository {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void index(String indexName, String documentId, Map<String, Object> document) throws IOException {
IndexRequest indexRequest = new IndexRequest(indexName).id(documentId).source(document);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}
public void delete(String indexName, String documentId) throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(indexName, documentId);
restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
}
public Map<String, Object> get(String indexName, String documentId) throws IOException {
GetRequest getRequest = new GetRequest(indexName, documentId);
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
return getResponse.getSourceAsMap();
}
public SearchHits search(String indexName, QueryBuilder queryBuilder) throws IOException {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
return searchResponse.getHits();
}
}
```
5. 使用Elasticsearch存储库
可以在Spring Boot应用程序中使用ElasticsearchRepository进行索引、删除、获取和搜索操作。
```java
@RestController
public class ElasticsearchController {
@Autowired
private ElasticsearchRepository elasticsearchRepository;
@PostMapping("/index")
public void index() throws IOException {
Map<String, Object> document = new HashMap<>();
document.put("name", "John Doe");
document.put("age", 30);
elasticsearchRepository.index("indexName", "documentId", document);
}
@DeleteMapping("/delete")
public void delete() throws IOException {
elasticsearchRepository.delete("indexName", "documentId");
}
@GetMapping("/get")
public Map<String, Object> get() throws IOException {
return elasticsearchRepository.get("indexName", "documentId");
}
@GetMapping("/search")
public List<Map<String, Object>> search() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "John");
SearchHits searchHits = elasticsearchRepository.search("indexName", queryBuilder);
List<Map<String, Object>> results = new ArrayList<>();
searchHits.forEach(hit -> {
results.add(hit.getSourceAsMap());
});
return results;
}
}
```
这就是Spring Boot与Elasticsearch的整合过程。
阅读全文