springboot整合es
时间: 2023-10-08 21:12:45 浏览: 97
springboot整合elasticsearch7实现es数据同步,查询、相关度排序、高亮显示、自动补全搜索等功能。
5星 · 资源好评率100%
Spring Boot可以通过Elasticsearch REST客户端轻松地与Elasticsearch进行集成。Elasticsearch REST客户端提供了一种简单的方法来与Elasticsearch进行交互。以下是Spring Boot如何与Elasticsearch集成的步骤:
1. 添加Elasticsearch依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.0</version>
</dependency>
```
2. 配置Elasticsearch客户端
在application.properties文件中添加以下配置:
```
spring.data.elasticsearch.cluster-nodes=localhost:9200
```
3. 创建Elasticsearch客户端
创建Elasticsearch客户端以与Elasticsearch进行交互。可以使用以下代码:
```
@Configuration
public class ElasticSearchConfig {
@Value("${spring.data.elasticsearch.cluster-nodes}")
private String esNodes;
@Bean(destroyMethod = "close")
public RestHighLevelClient elasticsearchClient() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("username", "password"));
RestClientBuilder builder = RestClient.builder(HttpHost.create(esNodes))
.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
```
在上面的代码中,我们创建了一个名为elasticsearchClient的Bean,该Bean使用RestHighLevelClient创建了一个Elasticsearch客户端。
4. 创建Elasticsearch存储库
创建一个Elasticsearch存储库以与Elasticsearch进行交互。可以使用以下代码:
```
@Repository
public class ProductRepository {
private final String INDEX = "product";
private final String TYPE = "default";
@Autowired
private RestHighLevelClient restHighLevelClient;
public boolean createProductIndex() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(INDEX);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();
}
public boolean addProduct(Product product) throws IOException {
IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, product.getId()).source(product.toMap());
IndexResponse response = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
return response.status() == RestStatus.CREATED;
}
public List<Product> searchProducts(String query) throws IOException {
SearchRequest searchRequest = new SearchRequest(INDEX);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryStringQuery(query));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
List<Product> products = new ArrayList<>();
for (SearchHit hit : hits) {
products.add(new Product(hit.getSourceAsMap()));
}
return products;
}
}
```
在上面的代码中,我们创建了一个名为ProductRepository的Bean,它使用RestHighLevelClient与Elasticsearch进行交互。
5. 测试
现在我们可以使用ProductRepository与Elasticsearch进行交互。可以使用以下代码:
```
@RestController
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping("/createIndex")
public boolean createIndex() throws IOException {
return productRepository.createProductIndex();
}
@PostMapping("/addProduct")
public boolean addProduct(@RequestBody Product product) throws IOException {
return productRepository.addProduct(product);
}
@GetMapping("/searchProducts")
public List<Product> searchProducts(@RequestParam String query) throws IOException {
return productRepository.searchProducts(query);
}
}
```
在上面的代码中,我们创建了一个名为ProductController的Bean,它使用ProductRepository与Elasticsearch进行交互。现在可以测试应用程序,以确保它可以与Elasticsearch进行交互。
阅读全文