elasticSearch 与springboot整合使用

导入所需要的包
可以参考博客:https://segmentfault.com/a/1190000018625101#item-2

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

yml配置

spring:
  data:
    elasticsearch:
      cluster-nodes: localhost:9300
      cluster-name: docker-cluster//此处名字应该与elasticSearch端的cluster-name的名字相同

简单版单条件查询

 @Autowired
    private GoodsRepository goodsRepository;
public PageUtil<Goods> search(String keyName, Integer pageStart, Integer pageSize) {
        Pageable page = PageRequest.of(pageStart, pageSize);
     
        Page<Goods> goods = goodsRepository.queryByGoodsName(keyName, page);

        //return new PageUtil<Goods>(pageStart,pageSize,goodsPage.getTotalElements(),goodsPage.getContent());
        return new PageUtil<Goods>(pageStart,pageSize,goods.getTotalElements(),goods.getContent());
    }

*继承Elasticrepository

@Repository
public interface GoodsRepository extends ElasticsearchRepository<Goods,String> {

    Page<Goods> queryByGoodsName(String keyWord, Pageable pageable);
}

分页,多条件查询

/**
 * 高级搜索,根据字段进行搜索
 * @param name 名称
 * @param color 颜色
 * @param sellingPoint 卖点
 * @param price 价格
 * @param start 开始时间(格式:yyyy-MM-dd HH:mm:ss)
 * @param end 结束时间(格式:yyyy-MM-dd HH:mm:ss)
 * @param page 当前页,从0开始
 * @param size 每页大小
 * @return {@link Result}
 */
@GetMapping("/_search")
public Mono<Result> search(String name, String color, String sellingPoint, String price, String start, String end, int page, int size) {

    // 校验参数
    if (StringUtils.isEmpty(page) || page < 0)
        page = 0; // if page is null, page = 0

    if (StringUtils.isEmpty(size) || size < 0)
        size = 10; // if size is null, size default 10
    
    // 构造分页对象
    Pageable pageable = PageRequest.of(page, size);

    // BoolQueryBuilder (Elasticsearch Query)
    BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
    if (!StringUtils.isEmpty(name)) {
        boolQueryBuilder.must(QueryBuilders.matchQuery("name", name));
    }

    if (!StringUtils.isEmpty(color)) {
        boolQueryBuilder.must(QueryBuilders.matchQuery("colors", color));
    }

    if (!StringUtils.isEmpty(color)) {
        boolQueryBuilder.must(QueryBuilders.matchQuery("sellingPoints", sellingPoint));
    }

    if (!StringUtils.isEmpty(price)) {
        boolQueryBuilder.must(QueryBuilders.matchQuery("price", price));
    }

    if (!StringUtils.isEmpty(start)) {
        Date startTime = null;
        try {
            startTime = DateTimeUtil.stringToDate(start, DateTimeFormat.yyyy_MM_dd_HH_mm_ss);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").gt(startTime.getTime()));
    }

    if (!StringUtils.isEmpty(end)) {
        Date endTime = null;
        try {
            endTime = DateTimeUtil.stringToDate(end, DateTimeFormat.yyyy_MM_dd_HH_mm_ss);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        boolQueryBuilder.must(QueryBuilders.rangeQuery("createTime").lt(endTime.getTime()));
    }

    // BoolQueryBuilder (Spring Query)
    SearchQuery searchQuery = new NativeSearchQueryBuilder()
            .withPageable(pageable)
            .withQuery(boolQueryBuilder)
            .build()
            ;

    // page search
    Page<PhoneModel> phoneModelPage = elasticsearchTemplate.queryForPage(searchQuery, PhoneModel.class);
    
    // return
    return Mono.just(Result.success(phoneModelPage));
}
发布了3 篇原创文章 · 获赞 0 · 访问量 4
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览