Elasticsearch(简称ES)是一个开源的、高扩展的分布式全文检索引擎,可以近乎实时地存储、检索数据。它的设计目标是稳定、可靠、快速,并能够轻松地扩展到上百台服务器,处理PB级别的数据。
以下是关于Elasticsearch的详细介绍:
总的来说,Elasticsearch是一个强大、灵活且易于使用的分布式全文检索引擎,广泛应用于各种规模的企业和组织中。
在pom.xml
中添加以下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
在application.properties
中配置Elasticsearch连接信息:
spring.data.elasticsearch.cluster-name=your-cluster-name
spring.data.elasticsearch.cluster-nodes=your-host:your-port
创建一个实体类,例如Product
:
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 = "product", type = "product")
public class Product {
@Id
private String id;
@Field(type = FieldType.Keyword)
private String name;
@Field(type = FieldType.Keyword)
private String description;
// getters and setters...
}
创建一个继承ElasticsearchRepository
的接口:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
}
在Service或Controller中使用Repository:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import static org.springframework.data.elasticsearch.core.query.QueryBuilders.*;
import org.springframework.data.domain.*;
import org.springframework.data.elasticsearch.*;
import org.springframework.data.*;
import org.springframework.*;
import java.*;
import java.util.*;
import java.util.*;
public class ProductService {
private final ProductRepository productRepository;
@Autowired
public ProductService(ProductRepository productRepository) {
this.productRepository = productRepository;
}
public Page<Product> findAll(Pageable pageable) {
return productRepository.findAll(pageable);
}
public List<Product> findByName(String name) {
return productRepository.findByName(name);
}
public List<Product> findByDescription(String description) {
return productRepository.findByDescription(description);
}
// other methods...
}
如果你需要执行更复杂的查询,可以在Repository接口中定义自定义方法,并使用Elasticsearch的Query对象来构建查询条件。例如:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductRepository extends ElasticsearchRepository<Product, String> {
List<Product> findByCustomQuery(String customQuery);
}
在实现该自定义查询方法时,可以使用Elasticsearch的QueryBuilder来构建复杂的查询条件。例如:
public class ProductRepositoryImpl implements ProductRepository {
@Override
public List<Product> findByCustomQuery(String customQuery) {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
// 添加bool查询的must条件,相当于AND查询
boolQueryBuilder.must(QueryBuilders.matchQuery("name", customQuery));
boolQueryBuilder.must(QueryBuilders.matchQuery("description", customQuery));
// 添加bool查询的should条件,相当于OR查询
boolQueryBuilder.should(QueryBuilders.termQuery("category", "electronics"));
boolQueryBuilder.should(QueryBuilders.termQuery("category", "books"));
// 添加bool查询的filter条件,用于过滤不需要的数据,不参与排序和分页等操作,只用于过滤数据。
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(100).lte(500));
// 执行查询
return elasticsearchTemplate().search(boolQueryBuilder, Product.class)
.stream()
.map(SearchHit::getContent)
.collect(Collectors.toList());
}
}