一、Spring Boot简单整合Elasticsearch
整合 Spring Boot 和 Elasticsearch 主要是为了实现数据的增删查改功能,下面将给出一些基本步骤来完成这个任务。在此之前,需要确保你已经安装了 Elasticsearch 和 Spring Boot,并且已经引入了相关的依赖。
1、引入依赖 :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
2、添加配置
配置 Elasticsearch 在你的 application.properties 或 application.yml 文件中添加以下配置:
spring.data.elasticsearch.cluster-name=your-cluster-name
spring.data.elasticsearch.cluster-nodes=your-cluster-nodes
3、创建实体类
创建一个与 Elasticsearch 中的文档相对应的实体类。例如:
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Document(indexName = "your-index-name")
public class YourEntity {
@Id
private String id;
@Field(type = FieldType.Text)
private String field1;
// getters and setters...
}
4、创建 Repository 接口
创建一个继承 ElasticsearchRepository 的接口,用于对 Elasticsearch 进行操作:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface YourEntityRepository extends ElasticsearchRepository<YourEntity, String> {
}
实现增删查改功能 现在你可以在服务类中实现增删查改功能:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class YourEntityService {
@Autowired
private YourEntityRepository repository;
public YourEntity save(YourEntity entity) {
return repository.save(entity);
}
public void delete(String id) {
repository.deleteById(id);
}
public YourEntity findById(String id) {
return repository.findById(id).orElse(null);
}
public List<YourEntity> findAll() {
return repository.findAll();
}
}
Spring Boot 简单整合了 Elasticsearch,并实现了对数据的增删查改功能。需要处理异常、进行分页、排序等操作,可以参考 Spring Data Elasticsearch 的文档。
二、实现分页、模糊查询、多条件查询
实现模糊查询、多条件查询和分页查询
使用Repository
接口提供的方法实现模糊查询、多条件查询和分页查询操作。
引入以下依赖:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.io.IOException;
import java.util.*;
//...在原有代码的基础上增加以下内容...
1、模糊查询:
模糊查询使用QueryBuilders的matchQuery方法,它需要一个字段名和一个包含正则表达式的字符串。例如:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("field1", ".*pattern.*"));
SearchRequest searchRequest = new SearchRequest("your-index");
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
2、多条件查询:
可以使用bool查询,根据需求构造复杂的查询语句:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field1", "value1")).must(QueryBuilders.rangeQuery("field2").from(10).to(100)));
SearchRequest searchRequest = new SearchRequest("your-index");
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
3、分页查询:
你可以使用Pageable接口进行分页查询:
Pageable pageable = PageRequest.of(0, 10); // 0是页码, 10是每页的数量
Page<YourEntity> page = repository.findAll(pageable);
List<YourEntity> list = page.getContent(); // 获取分页后的数据列表