其他分享
首页 > 其他分享> > elasticsearchRestTemplate search 精准查询和模糊查询即不区分大小写

elasticsearchRestTemplate search 精准查询和模糊查询即不区分大小写

作者:互联网

背景

springboot2.3.2.release
spring-boot-starter-data-elasticsearch2.3.2.release
estemplate.search
根据es进行批量查询时,身份证区分大小写了

解决方案

区分大写查询 使用了.keyWord

 //TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("idCardNum.keyword", keySet);
  

不区分大小查询

      TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("idCardNum", keySet);

查询案例

//参数类型:Set<String> keySet, String searchName

TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(unikeyName, keySet);
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(termsQueryBuilder)
                .build();
        // 允许返回条数设置  默认返回10000条
        searchQuery.setTrackTotalHits(true);
        searchQuery.setMaxResults(1000000);
        // 开启重试机制
        int i=0;
        boolean timeOut = true;
        SearchHits<EsComplatOutSideUserMapping> search = null;
        while(timeOut && i<3){
            try{
                search = elasticsearchRestTemplate.search(searchQuery, EsComplatOutSideUserMapping.class);
                timeOut = false;
            }catch (Exception e){
                log.error("es批量查询异常,异常次数:{}|异常信息:{}", i, e.getMessage());
                if(e.getMessage().indexOf("30,000 milliseconds timeout on connection ") !=-1){
                    i++;
                }else if(e.getMessage().indexOf("30,000 milliseconds timeout on connection ") == -1){
                    timeOut = false;
                    throw e;
                }
                if(i==3 && timeOut){
                    throw e;
                }
            }
        }
        Set<String> haveSet = new HashSet<>();
        List<SearchHit<EsComplatOutSideUserMapping>> searchHits = search.getSearchHits();
        log.info("es查询{},查询结果总数:{},返回list长度:{}", unikeyName, search.getTotalHits(),searchHits.size());
        if(searchHits.size() != search.getTotalHits()){
            log.error("es查询{}结果值与预期不符,total:{},list:{}", unikeyName, search.getTotalHits(), searchHits.size());
        }

标签:search,searchQuery,elasticsearchRestTemplate,查询,keySet,timeOut,es
来源: https://blog.csdn.net/Dolphin_bobo/article/details/120294031