ElasticsearchRestTemplate工具类对ES 进行操作

SpringDataElasticsearch提供了一个工具类ElasticsearchRestTemplate,我们注入该类对象也能对ES进行操作。

1. 操作索引

package com.neu.springdataes;

import com.neu.springdataes.model.Product;
import com.neu.springdataes.model.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;

/**
 * @Author yqq
 * @Date 2021/12/01 14:29
 * @Version 1.0
 */
@SpringBootTest
public class test1 {
    @Autowired
    private ElasticsearchRestTemplate template;
    @Test
    void t1(){
        IndexOperations ops = template.indexOps(Student.class);
        ops.create();
    }
    @Test
    void t2(){
        IndexOperations ops = template.indexOps(Student.class);
        ops.delete();
    }
 
}

2.增删改文档

template操作文档的常用方法:
save():新增/修改文档
delete():删除文档

@Test
    void t3(){
        Product product = new Product(6,"小米","红米");
        template.save(product);
    }
    @Test
    void t4(){
        Product product = template.get("6", Product.class);
        System.out.println(product);
    }
    @Test
    void t5(){
        template.delete("6",Product.class);
    }

3.查询文档

template的search方法可以查询文档:
SearchHits search(Query query, Class clazz):查询文档,query是查询条件对象, clazz是结果类型。

  1. 普通查询:
 @Test
    void t1(){
        //构建查询条件
//        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        TermQueryBuilder queryBuilder = QueryBuilders.termQuery("desc","手机");
        NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
        //查询
        SearchHits<Product> searchHits = template.search(query, Product.class);
        for (SearchHit<Product> hits:searchHits){
            Product product = hits.getContent();
            System.out.println(product);
        }
    }
  1. 复杂条件查询
@Test
    void t2(){
        String name = "12";
        String desc = "手机";
        //构建查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        if (name == null && desc == null){
            MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
            boolQueryBuilder.must(queryBuilder);
        }
        else {
            if (name != null){
                MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name",name);
                boolQueryBuilder.must(queryBuilder);
            }
            if (desc != null){
                MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("desc",desc);
                boolQueryBuilder.must(queryBuilder);
            }
        }
        NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();
        SearchHits<Product> searchHits = template.search(query, Product.class);
        //输出查询结果
        for (SearchHit<Product> hits:searchHits){
            Product product = hits.getContent();
            System.out.println(product);
        }
    }
  1. 分页查询
  @Test
    void t3(){
        //构建查询条件
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        //分页处理
        Pageable pageable = PageRequest.of(0,3);
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(pageable)
                .build();

        SearchHits<Product> searchHits = template.search(query, Product.class);
       //封装page对象
        List<Product> content = new ArrayList<>();
        for (SearchHit<Product> productSearchHit:searchHits)
            content.add(productSearchHit.getContent());
        Page<Product> page = new PageImpl<>(content,pageable,searchHits.getTotalHits());
        //输出分页对象
        System.out.println(page.getTotalPages());
        System.out.println(page.getContent());
        System.out.println(page.getTotalElements());
    }
  1. 结果排序
 @Test
    void t4(){
        //构建查询条件
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        //分页处理
        Pageable pageable = PageRequest.of(0,3);
        //排序条件
        FieldSortBuilder sortBuilder = SortBuilders.fieldSort("id").order(SortOrder.DESC);
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(pageable)
                .withSort(sortBuilder)
                .build();

        SearchHits<Product> searchHits = template.search(query, Product.class);
        //封装page对象
        List<Product> content = new ArrayList<>();
        for (SearchHit<Product> productSearchHit:searchHits)
            content.add(productSearchHit.getContent());
        Page<Product> page = new PageImpl<>(content,pageable,searchHits.getTotalHits());
        //输出分页对象
        System.out.println(page.getTotalPages());
        for (Product product:page.getContent())
            System.out.println(product);
        System.out.println(page.getTotalElements());
    }

ElasticsearchRestTemplate工具类对ES 进行操作

上一篇:Java项目:化妆品商城系统(java+Springboot+ssm+mysql+jsp+maven)


下一篇:记一下EF code Frist错误