Spring Data Elasticsearch

1 依赖




    4.0.0

    spring-data-elasticsearch

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.16.RELEASE
         
    
    
        1.8
    
    
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
         
            com.alibaba
            fastjson
            1.2.76
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
       
    


2 配置文件

application.yml

server:
  port: 20001
spring:
  application:
    name: elastic-search
  data:
    elasticsearch:
      cluster-name: my-application
      # 注意这里使用的是TransportClient 连接的是ES的TCP端口,而非ES的http端口
      cluster-nodes: 127.0.0.1:9300

3 JavaBean

/**
 * 索引库对应实体类
 * 创建索引库信息使用注解:@Document
 *      indexName:索引库名
 *      type:类型
 *      shards:分片 默认5
 *      replicas:副本 默认1
 * 指定mapping需要在field上添加@Id和@Field注解
 *      type:类型
 *      analyzer:分词器
 *      index:是否创建索引
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "goods", type = "_doc", shards = 3, replicas = 1)
public class Goods implements Serializable {
    @Id
    private Long id;
    @Field(type = FieldType.Text,analyzer = "ik_max_word")
    private String title; //标题
    @Field(type = FieldType.Keyword)
    private String category;// 分类
    @Field(type = FieldType.Keyword)
    private String brand; // 品牌
    @Field(type = FieldType.Double)
    private Double price; // 价格
    @Field(type = FieldType.Keyword,index = false)
    private String images; // 图片地址
}

4 使用ElasticsearchTemplate操作索引库

elasticsearch的操作可以通过ElasticsearchTemplate来进行

@RunWith(SpringRunner.class)
@SpringBootTest
public class ElasticSearchTest {

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private GoodsRepository goodsRepository;

    /**
     * 创建索引库
     * 实体类的field上没加Id和Field注解时不会创建Mapping
     */
    @Test
    public void testCreateIndex(){
        boolean result = template.createIndex(Goods.class);
        System.out.print("创建索引库结果为" + result);
    }

    /**
     * 新增映射
     */
    @Test
    public void testCreateMapping(){
        boolean result = template.putMapping(Goods.class);
        System.out.println("创建mapping结果为"+ result);
    }
}

5 ElasticsearchRepository

ElasticsearchRepository封装了基本的CRUD方法,可以通过继承ElasticsearchRepository来使用

// ElasticsearchRepository T 为要操作的类型  ID为主键类型
@Repository
public interface GoodsRepository extends ElasticsearchRepository {
}

5.1 基本CRUD操作

/**
     * 向索引库新增文档
     */
    @Test
    public void testAddDoc(){
        Goods goods = new Goods(7L, "小米电视4A", " 电视",
                "小米", 5699.00, "/13123.jpg");
        goodsRepository.save(goods);
    }

    @Test
    public void testBatchAddDoc(){
        List list = new ArrayList<>();
        list.add(new Goods(2L, "坚果手机R1", "手机", "锤子", 3699.00, "/13123.jpg"));
        list.add(new Goods(3L, "华为META10", "手机", "华为", 4499.00, "/13123.jpg"));
        list.add(new Goods(4L, "小米Mix2S", "手机", "小米", 4299.00, "/13123.jpg"));
        list.add(new Goods(5L, "荣耀V10", "手机", "华为", 2799.00, "/13123.jpg"));
        list.add(new Goods(6L, "小米手机7", "手机", "小米", 3299.00, "/13123.jpg"));
        goodsRepository.saveAll(list);
        System.out.println("批量新增文档成功");
    }

    /**
     * 根据id查询文档
     */
    @Test
    public void testQueryById(){
        Optional goodsOptional = goodsRepository.findById(1L);
        System.out.println(goodsOptional.orElse(null));
    }

    /**
     * 更新文档
     */
    @Test
    public void testUpdateDoc(){
        Goods goods = new Goods(1L, "小米手机10Pro", " 手机",
                "小米", 9999.00, "http://image.leyou.com/13123.jpg");
        goodsRepository.save(goods);
        System.out.println("更新文档成功");
    }

    /**
     * 查询所有文档
     */
    @Test
    public void queryAllDoc(){
        Iterable allGoods = goodsRepository.findAll();
        allGoods.forEach(goods -> System.out.println(goods));
    }

    /**
     * 根据文档id删除
     */
    @Test
    public void testDeleteDocById(){
        goodsRepository.deleteById(6L);
        System.out.println("根据id删除成功");
    }

5.2 自定义查询

Keyword Sample Elasticsearch Query String
And findByNameAndPrice {"bool" : {"must" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}}
Or findByNameOrPrice {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"price" : "?"}} ]}}
Is findByName {"bool" : {"must" : {"field" : {"name" : "?"}}}}
Not findByNameNot {"bool" : {"must_not" : {"field" : {"name" : "?"}}}}
Between findByPriceBetween {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}
LessThanEqual findByPriceLessThan {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}
GreaterThanEqual findByPriceGreaterThan {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}}
Before findByPriceBefore {"bool" : {"must" : {"range" : {"price" : {"from" : null,"to" : ?,"include_lower" : true,"include_upper" : true}}}}}
After findByPriceAfter {"bool" : {"must" : {"range" : {"price" : {"from" : ?,"to" : null,"include_lower" : true,"include_upper" : true}}}}}
Like findByNameLike {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}}
StartingWith findByNameStartingWith {"bool" : {"must" : {"field" : {"name" : {"query" : "?*","analyze_wildcard" : true}}}}}
EndingWith findByNameEndingWith {"bool" : {"must" : {"field" : {"name" : {"query" : "*?","analyze_wildcard" : true}}}}}
Contains/Containing findByNameContaining {"bool" : {"must" : {"field" : {"name" : {"query" : "**?**","analyze_wildcard" : true}}}}}
In findByNameIn(Collectionnames) {"bool" : {"must" : {"bool" : {"should" : [ {"field" : {"name" : "?"}}, {"field" : {"name" : "?"}} ]}}}}
NotIn findByNameNotIn(Collectionnames) {"bool" : {"must_not" : {"bool" : {"should" : {"field" : {"name" : "?"}}}}}}
Near findByStoreNear Not Supported Yet !
True findByAvailableTrue {"bool" : {"must" : {"field" : {"available" : true}}}}
False findByAvailableFalse {"bool" : {"must" : {"field" : {"available" : false}}}}
OrderBy findByAvailableTrueOrderByNameDesc {"sort" : [{ "name" : {"order" : "desc"} }],"bool" : {"must" : {"field" : {"available" : true}}}}

repository修改

@Repository
public interface GoodsRepository extends ElasticsearchRepository {

    List findByCategory(String category);

    List findByBrand(String brand);

    List findByPriceBetween(Double from,Double to);

    List findByTitleAndCategory(String title, String category);
}

使用自定义查询

/**
     * 自定义查询
     */
    @Test
    public void testConditionSearch(){
        //List goodsList = goodsRepository.findByBrand("锤子");
        //List goodsList = goodsRepository.findByPriceBetween(1000D, 4000D);
        List goodsList = goodsRepository.findByTitleAndCategory("小米", "手机");
        goodsList.forEach(goods -> System.out.println(goods));
    }

/**
     * 分页搜索
     */
    @Test
    public void testPageSearch(){
        //构建分页排序对象
        int page = 0;
        int size = 5;
        Sort sort = Sort.by(Sort.Direction.DESC, "price");
        PageRequest pageRequest = PageRequest.of(0, 3, sort);
        //构建查询对象
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        //查询
        Page goodsPage = goodsRepository.search(matchAllQueryBuilder, pageRequest);
        List goodsList = goodsPage.getContent();
        goodsList.forEach(goods -> System.out.println(goods));
    }

6 原生查询

一些较复杂的查询使用repository查询较困难,此时可使用原生查询

6.1 高亮查询

高亮查询的原理,就是将关键字使用css标签将查询结果中的关键词包起来.具体操作时需要自定义结果处理器

/**
 * goods的自定义结果处理器:用于接收处理高亮搜索结果
 */
public class GoodsSearchResultMapper implements SearchResultMapper {
    @Override
    public  AggregatedPage mapResults(SearchResponse searchResponse, Class aClass, Pageable pageable) {
        //获取返回数据所需总命中数
        long totalHits = searchResponse.getHits().totalHits;
        //获取返回数据所需最大评分
        float maxScore = searchResponse.getHits().getMaxScore();
        //获取返回数据所需聚合结果
        Aggregations aggregations = searchResponse.getAggregations();
        //获取返回数据必要参数
        String scrollId = searchResponse.getScrollId();

        //返回结果
        List content = new ArrayList<>();
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            //获取源数据
            String sourceAsString = hit.getSourceAsString();
            //源数据转对象
            T goods = JSON.parseObject(sourceAsString, aClass);
            //获取高亮属性
            Map highlightFields = hit.getHighlightFields();
            HighlightField titleHighlightField = highlightFields.get("title");
            Text[] fragments = titleHighlightField.getFragments();
            if (fragments != null && fragments.length > 0){
                StringBuilder highlightFieldValue = new StringBuilder();
                for (Text fragment : fragments) {
                    highlightFieldValue.append(fragment.toString());
                }
                //高亮字段覆盖源字段值
                Goods item = (Goods) goods;
                item.setTitle(highlightFieldValue.toString());
            }
            content.add(goods);
        }
        return new AggregatedPageImpl<>(content,pageable,totalHits,aggregations,scrollId,maxScore);

    }
}

使用原生高亮查询

    /**
     * 原生查询:高亮
     */
    @Test
    public void testHighlight(){
        //构建查询对象
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("title","电视");
        //构建高亮字段对象
        HighlightBuilder.Field highlightField = new HighlightBuilder.Field("title")
                .preTags("")
                .postTags("");
        //使用原生查询:传入条件查询对象和高亮对象
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withHighlightFields(highlightField)
                .build();
        //自定义结果处理对象
        GoodsSearchResultMapper goodsSearchResultMapper = new GoodsSearchResultMapper();
        AggregatedPage goodsAggregatedPage = template.queryForPage(nativeSearchQuery, Goods.class, goodsSearchResultMapper);
        List goodsList = goodsAggregatedPage.getContent();
        goodsList.forEach(goods -> System.out.println(goods));
    }

6.2 聚合查询

/**
     * 原生查询:聚合
     */
    @Test
    public void testAggregation(){
        //构建查询条件对象
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        //构建聚合分组--桶:按照brand字段聚合为桶
        AbstractAggregationBuilder agg = AggregationBuilders.terms("brand_agg") //指定分组名称
                .field("brand");//指定分组字段
        //添加子聚合--度量:每个桶内求价格平均值
        agg.subAggregation(AggregationBuilders.avg("price_avg").field("price"));
        //使用原生查询
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .addAggregation(agg).build();
        AggregatedPage goodsAggregatedPage = template.queryForPage(nativeSearchQuery, Goods.class);
        Terms brandAgg = (Terms) goodsAggregatedPage.getAggregation("brand_agg");
        List buckets = brandAgg.getBuckets();
        for (Terms.Bucket bucket : buckets) {
            String brandName = bucket.getKeyAsString();
            long docCount = bucket.getDocCount();
            Avg priceAvg = bucket.getAggregations().get("price_avg");
            double priceAvgValue = priceAvg.getValue();
            System.out.println("品牌为[" + brandName + "]的商品数量为" + docCount + ",平均价格为" + priceAvgValue);
        }
    }

你可能感兴趣的:(Spring Data Elasticsearch)