记spring-data-elasticsearch API文档学习

记录spring-data-elasticsearch的API文档学习

一 开发环境

springboot 2.0.1 + docker +elasticsearch5.6.3

docker搭建ES服务器和kibana监控平台

二 创建springboot工程

依赖如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    
    com.cy
    spring-data-elasticsearch-kafka
    0.0.1-SNAPSHOT
    spring-data-elasticsearch-kafka
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.yml
spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0.0.1:9300

需要放开9200和9300两个端口
9300端口是使用tcp客户端连接使用的端口;
9200端口是通过http协议连接es使用的端口;
cluster-nodes是 {your.server.ip}:9300
cluster-name是访问localhost:9200里面的cluster_name 保持名字一样

三 准备实体类

@Document(indexName = "item",type = "docs", shards = 1, replicas = 0)
public class Item {
    @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(index = false, type = FieldType.Keyword)
    private String images; // 图片地址
}

index.name -> 创建的索引名字
type -> 对应在索引库中的类型
shards -> 节点上索引的分片数 (primary shards)
replicas -> 每个分片的副本 (replicas shards)
FieldType.Keyword -> 不分词
index = false 不作为索引条件 当对images进行索引 会抛出异常

四 持久层

@Repository
public interface ESRepository extends ElasticsearchRepository {
}

五 业务逻辑层 — 索引创建与删除

    /**
     * 创建索引和创建字段映射
     */
    public void create_index() {
        elasticsearchTemplate.createIndex(Item.class);
        log.info("创建索引成功");
        elasticsearchTemplate.putMapping(Item.class);
        log.info("字段映射成功");
    }

    /**
     * 删除索引
     */
    public void delete_index() {
        //Clazz String
        elasticsearchTemplate.deleteIndex(Item.class);
//        elasticsearchTemplate.deleteIndex("item");
        log.info("删除索引成功");
    }

六 业务逻辑层— 数据的增删改

 /**
     * 新增
     */
    public void add() {
        Item item = esRepository.save(
                new Item(4L, "Iphone X", "手机", "小米",
                        6398.00, "http://image.baidu.com/13123.jpg")
        );
        log.info("添加数据:" + item.toString());
        //可迭代
        List list = new ArrayList<>();
        list.add(new Item(2L, "坚果手机R1", " 手机", "锤子", 3699.00, "http://image.baidu.com/13123.jpg"));
        list.add(new Item(3L, "华为META10", " 手机", "华为", 4499.00, "http://image.baidu.com/13123.jpg"));
        esRepository.saveAll(list);
        log.info("批量添加数据");
    }


    /**
     * 修改
     */
    public void update() {
        Item item = esRepository.save(
                new Item(1L, "Iphone X", "手机", "苹果",
                        6399.00, "http://image.baidu.com/13123.jpg")
        );
    }

    /**
     * 删除
     */
    public void delete() {
        esRepository.delete(
                new Item(1L, "Iphone X", "手机", "小米",
                        6399.00, "http://image.baidu.com/13123.jpg")
        );

//        esRepository.deleteById(1L);

    }

六 业务逻辑层— 数据的基本查询

/**
     * 分页查询
     *
     * @param page
     * @param size
     */
    public void search_paging(int page, int size) {
        Page items = esRepository.findAll(PageRequest.of(page, size));
        items.forEach(x -> log.info(x.toString()));
    }

    /**
     * 排序查询
     */
    public void search_sort() {
        Iterable items = esRepository.findAll(Sort.by("price").descending());
        items.forEach(x -> log.info(x.toString()));
    }

    /**
     * term query 
     */
    public void search_as_your_want() {
        //根据term query 再来进行分页查询 此方法默认查询第一页的前十条
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery("brand", "小米")).build();
        Page pages = esRepository.search(searchQuery);
        pages.stream().sorted(Comparator.comparing(Item::getPrice)).forEach(x -> log.info(x.toString()));
    }

    /**
     * match query  ES已经进行排序 其本质为term query
     */
    public void search_as_your_want2(){
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchQuery("title", "Iphone X")).build();

        Page pages = esRepository.search(query);
        pages.stream().sorted(Comparator.comparingDouble(Item::getPrice)).forEach(x->log.info(x.toString()));

    }

    /**
     * range query
     */
    public void search_as_your_want3(){
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.rangeQuery("price").gte(0).lte(10000)).build();

        Page pages = esRepository.search(query);
        pages.stream().sorted(Comparator.comparingDouble(Item::getPrice)).forEach(x->log.info(x.toString()));

    }

    /**
     * bool query
     */
    public void search_as_your_want4(){
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        BoolQueryBuilder boolQuery = boolQueryBuilder.filter(QueryBuilders.termQuery("brand", "小米"))
                .should(QueryBuilders.rangeQuery("price").gte(3000).lte(7000));

        Iterable items = esRepository.search(boolQuery);
        StreamSupport.stream(items.spliterator(),false)
                .sorted(Comparator.comparingDouble(Item::getPrice))
                .forEach(x->log.info(x.toString()));

    }

    /**
     *  sort query
     */
    public void search_as_your_want5(){

        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchQuery("brand", "小米"))
                .withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC)).build();
        Page items = esRepository.search(searchQuery);
        items.forEach(x->log.info(x.toString()));

    }

    /**
     * fuzzy query
     */
    public void search_as_your_want6(){
        SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(QueryBuilders.fuzzyQuery("brand", "小米")).build();
        Page items = esRepository.search(searchQuery);
        items.forEach(x->log.info(x.toString()));
    }

此只记录核心代码块

随后将会用springBoot整合kafka和elasticSearch,实现批量拉取日志以及批量更新到ES,以及完成有ES为搜索中间件到博客搜索系统.

你可能感兴趣的:(spring-data-es)