Springboot如何整合elasticsearch
时间: 2023-03-10 17:51:25 浏览: 113
Springboot可以通过官方提供的Elasticsearch Starter来整合elasticsearch,它可以自动配置Elasticsearch客户端和提供Elasticsearch操作的工具。
相关问题
Springboot整合ElasticSearch
好的,下面是Spring Boot整合Elasticsearch的基本步骤:
1. 添加Elasticsearch依赖
在pom.xml文件中添加Elasticsearch的依赖:
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
```
2. 配置Elasticsearch连接信息
在application.properties或application.yml文件中添加Elasticsearch的连接信息:
```yaml
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=my-application
```
3. 创建Elasticsearch实体类
创建一个实体类,用于映射Elasticsearch中的文档:
```java
@Document(indexName = "blog", type = "article")
public class Article {
@Id
private String id;
private String title;
private String content;
// 省略getter和setter方法
}
```
4. 创建Elasticsearch的Repository
创建一个继承自ElasticsearchRepository的接口,用于操作Elasticsearch中的文档:
```java
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
List<Article> findByTitle(String title);
}
```
5. 使用ElasticsearchRepository进行操作
在Spring Boot的Controller中,注入ArticleRepository,并使用它进行Elasticsearch的操作:
```java
@RestController
public class ArticleController {
@Autowired
private ArticleRepository articleRepository;
@GetMapping("/articles")
public List<Article> search(@RequestParam("title") String title) {
return articleRepository.findByTitle(title);
}
}
```
以上就是基本的Spring Boot整合Elasticsearch的步骤。当然,还有很多高级的用法,比如分页、聚合等,需要根据具体的业务需求进行实现。
springboot整合elasticSearch
Spring Boot可以很方便地与Elasticsearch进行集成。下面是一个简单的示例:
1. 添加Elasticsearch客户端依赖
在pom.xml中添加以下依赖:
```xml
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.0</version>
</dependency>
```
2. 配置Elasticsearch客户端
在application.properties中添加以下配置:
```properties
spring.elasticsearch.rest.uris=http://localhost:9200
```
3. 创建Elasticsearch客户端Bean
```java
@Configuration
public class ElasticsearchConfig {
@Value("${spring.elasticsearch.rest.uris}")
private String elasticsearchUrls;
@Bean
public RestHighLevelClient restHighLevelClient() {
String[] urls = elasticsearchUrls.split(",");
HttpHost[] httpHosts = new HttpHost[urls.length];
for (int i = 0; i < urls.length; i++) {
String url = urls[i];
httpHosts[i] = new HttpHost(url.split(":")[0], Integer.parseInt(url.split(":")[1]), "http");
}
RestClientBuilder builder = RestClient.builder(httpHosts);
return new RestHighLevelClient(builder);
}
}
```
4. 创建Elasticsearch操作类
```java
@Component
public class ElasticsearchTemplate {
@Autowired
private RestHighLevelClient restHighLevelClient;
public void index(String index, String id, String json) throws IOException {
IndexRequest request = new IndexRequest(index);
request.id(id);
request.source(json, XContentType.JSON);
restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
public SearchHits search(String index, String keyword) throws IOException {
SearchRequest request = new SearchRequest(index);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.query(QueryBuilders.matchQuery("name", keyword));
request.source(builder);
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
return response.getHits();
}
}
```
5. 使用Elasticsearch操作类
```java
@RestController
public class ElasticsearchController {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@PostMapping("/index")
public void index(String index, String id, String json) throws IOException {
elasticsearchTemplate.index(index, id, json);
}
@GetMapping("/search")
public SearchHits search(String index, String keyword) throws IOException {
return elasticsearchTemplate.search(index, keyword);
}
}
```
这就是一个简单的Spring Boot整合Elasticsearch的示例。你可以根据自己的需求进行修改和扩展。
阅读全文