SpringDataElasticsearch
提供了一个工具类 ElasticsearchTemplate,我们使用该类对象也可以对
ES
进行操作
1.操作索引
首先注入操作ES所需要的工具类(ElasticsearchTemplate)。
@Autowired
private ElasticsearchTemplate template;
将createIndex改为false
@Document(indexName = "product",createIndex = false)
@Data
@AllArgsConstructor
public class Product {
@Id //标记在成员变量上,标记一个字段为主键,该字段的值会同步到ES该文档的id值
//标记在成员变量上,标记为文档中的域,一般有如下属性-> type域的类型,index是否创建索引,store是否单独存储,analyzer分词器,searchAnalyzer搜索分词器
@Field(type = FieldType.Integer,store = true,index = true)
private Integer id;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String productName;
@Field(type = FieldType.Text,store = true,index = true,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
private String productDesc;
}
1.1新增索引(不推荐)
不推荐原因:生成的索引中mappings为空,并不按照指定
@Test
public void addIndex(){
//获取索引操作对象
IndexOperations indexOperations = template.indexOps(Product.class);
//创建索引
indexOperations.create();
}
1.2删除索引
@Test
public void deleteIndex(){
//获取索引操作对象
IndexOperations indexOperations = template.indexOps(Product.class);
//删除索引
indexOperations.delete();
}
2.操作文档
2.1新增/修改 文档
//新增/修改 文档
@Test
public void addDocument(){
Product product = new Product(5, "elasticsearch", "elasticsearch是一款非常好的分词器");
template.save(product);
}
2.2删除文档
@Test
public void deleteDocument(){
template.delete("5",Product.class);
}
2.3查询文档
//根据id查询
@Test
public void findDocument(){
Product product = template.get("1", Product.class);
System.out.println(product);
}
3.查询文档
template的search方法可以查询文档
SearchHits < T > search ( Query query , Class < T > clazz ) :查询文档, query 是查询条件对象, clazz 是结果类型。
3.1简单查询
@Test
public void findDocuments(){
//1.构造查询条件
NativeQuery query=new NativeQueryBuilder()
.withQuery(Queries.matchAllQueryAsQuery()).build();
//2.查询
SearchHits<Product> result = template.search(query, Product.class);
//3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
System.out.println(productSearchHit.getContent());
}
}
3.2匹配查询
@Test
public void findDocuments(){
//1.构造查询条件
NativeQuery query=new NativeQueryBuilder()
.withQuery(Queries.matchQueryAsQuery("productDesc","手机",null,null))
.build();
//2.查询
SearchHits<Product> result = template.search(query, Product.class);
//3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
System.out.println(productSearchHit.getContent());
}
}
4.复杂条件查询
@Test
public void findDocuments2(){
String productName="三体";
String productDesc="小说";
//1.构造查询条件
NativeQueryBuilder nativeQueryBuilder = new NativeQueryBuilder();
//判断是否有查询条件
if (productName== null && productDesc== null){
//查询所有
nativeQueryBuilder.withQuery(Queries.matchAllQueryAsQuery());
}else {
//构造bool查询
BoolQuery.Builder boolQuery = QueryBuilders.bool();
if (productName!=null){
//构造must条件
boolQuery.must(Queries.matchQueryAsQuery("productName",productName,null,null));
}
if (productDesc!=null){
boolQuery.must(Queries.matchQueryAsQuery("productDesc",productDesc,null,null));
}
//构造查询条件
nativeQueryBuilder.withQuery(boolQuery.build()._toQuery());
}
//构造查询条件
NativeQuery query = nativeQueryBuilder.build();
//2.查询
SearchHits<Product> result = template.search(query, Product.class);
//3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
System.out.println(productSearchHit.getContent());
}
}
5.分页查询
@Test
public void findDocuments3(){
//1.构造查询条件
//分页条件
Pageable pageable = PageRequest.of(0, 3, Sort.by(Sort.Direction.DESC, "id"));
NativeQuery query=new NativeQueryBuilder()
.withQuery(Queries.matchAllQueryAsQuery())
.withPageable(pageable)
.build();
//2.查询
SearchHits<Product> result = template.search(query, Product.class);
//3.处理查询结果
ArrayList content = new ArrayList();
for (SearchHit<Product> productSearchHit : result) {
content.add(productSearchHit.getContent());
}
/**
*封装page对象
* 参数1:具体数据 参数2:分页条件对象 参数3:总条数
*/
Page<PageImpl> page = new PageImpl(content, pageable, result.getTotalHits());
System.out.println(page.getTotalElements());
System.out.println(page.getTotalPages());
System.out.println(page.getContent());
}
6.结果排序
@Test
public void findDocumentSort(){
//1.构造查询条件
NativeQuery query=new NativeQueryBuilder()
.withQuery(Queries.matchAllQueryAsQuery())
//Sort.Direction.DESC 降序 Sort.Direction.ASC 升序
.withSort(Sort.by(Sort.Direction.DESC,"id"))
.build();
//2.查询
SearchHits<Product> result = template.search(query, Product.class);
//3.处理查询结果
for (SearchHit<Product> productSearchHit : result) {
System.out.println(productSearchHit.getContent());
}
}