栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

elasticsearch查询的简单使用

elasticsearch查询的简单使用

  1. 引入ES依赖:

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

  1. 创建一个类存放es的索引位置:
    例子:
@document(indexName = "goods_by_580", type = "GoodsInfo580Es")
@Data
public class GoodsInfo580document extends GoodsInfo580Es {
}

indexName:索引名字(相当于mysql的数据库)

type:文档类型(相当于mysql数据库中的表)

@Data
public class GoodsInfo580Es {

    @Id
    private String id;
    
    private String crmGoodsId;
    
    private String prescriptionType;
    
    private String name;
    
    private String goodsCode;
    
    private String barCode;
    
    private String productSpecification;
    
    private String unit;
    
    private String dosageForm;
    
    private String manufactor;
    
    private String licenseNumber;

创建es Repository接口:

@Repository
public interface GoodsInfo580Repository extends ElasticsearchRepository {
}

业务查询处理:

public class HealthGoodsInfoService4EsImpl implements HealthGoodsInfoService4Es {

    private final GoodsInfo580Repository goodsInfo580Repository;
    @Override
    public List goodsList(HealthGoodsInfo4EsQO qo) {
        //获取构造好的查询条件
        NativeSearchQuery build = getSearchQuery(qo).build();
        Page search = goodsInfo580Repository.search(build);
        if(search.getContent().size() == 0){
            log.info("ES没有580商品数据。。");
            return null;
        }
        List goodsInfo4Es580DTOS = CollectionUtil.copy(search.getContent(), HealthGoodsInfo4Es580DTO.class);
        return goodsInfo4Es580DTOS;
    }

    
    private NativeSearchQueryBuilder getSearchQuery(HealthGoodsInfo4EsQO qo) {
        BoolQueryBuilder bool = new BoolQueryBuilder();
        Pageable pageable = PageRequest.of(0,1);
        if (qo == null) {
            return new NativeSearchQueryBuilder();
        }
        if (Func.isNotEmpty(qo.getBarCode())){
            // 精确查询
            QueryBuilder queryBuilders = QueryBuilders.matchPhrasePrefixQuery("barCode",qo.getBarCode());
            bool.must(queryBuilders);
        }
        if (Func.isNotEmpty(qo.getGoodsCode())){
            // 精确查询
            QueryBuilder queryBuilders = QueryBuilders.matchPhrasePrefixQuery("goodsCode",qo.getGoodsCode());
            bool.must(queryBuilders);
        }
        if (Func.isNotEmpty(qo.getGoodsName())){
            // 模糊查询
            MatchPhraseQueryBuilder matchPhraseQuery = QueryBuilders.matchPhraseQuery("name",qo.getGoodsName());
            // 查询结果分页
            pageable = PageRequest.of(0, 70);
            bool.must(matchPhraseQuery);
        }

        return new NativeSearchQueryBuilder().withPageable(pageable).withQuery(bool);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/304493.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号