es查询代码模板

Posted 皓洲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es查询代码模板相关的知识,希望对你有一定的参考价值。

	@Autowired
    private RestHighLevelClient restHighLevelClient;
    @Autowired
    private CosCommonConfig cosCommonConfig;
    
public Integer getReadCountById(XfaceNoticeMessageListInquiryResponseSubDTO subDTO) 
        // 构造查询器
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.filter(QueryBuilders.termsQuery("push_id", subDTO.getNoticeId()));

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.fetchSource(new String[]"push_id", "read_num", "etl_time", null);//指定获取字段
        //设置超时时间
        sourceBuilder.timeout(new TimeValue(30, TimeUnit.SECONDS));
        //设置分页参数
        sourceBuilder.from(0); // 第几页
        sourceBuilder.size(1000); // 每页多少条数据

        SearchRequest searchRequest = new SearchRequest(cosCommonConfig.getTftPushReadIndex())//"sit_tft_push_read_stat_index"
                .source(sourceBuilder)
                .searchType(SearchType.QUERY_THEN_FETCH);
        //阅读量默认为0
        Integer readCount = 0;
        try 
            logger.info("查询es数据库-开始,索引为:,查询的pushID为:", cosCommonConfig.getTftPushReadIndex(), subDTO.getNoticeId());
            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
            // 封装查询结果
            SearchHits termhts = searchResponse.getHits();
            //如果查询结果不为空,则取阅读量
            if (null != termhts && termhts.getHits().length > 0) 
                SearchHit lastHitItem = termhts.getHits()[termhts.getHits().length - 1];
                if (null != lastHitItem.getSourceAsMap()) 
                    readCount = (Integer.parseInt(lastHitItem.getSourceAsMap().get("read_num").toString()));
                
            
         catch (Exception e) 
            logger.error(" getReadCountById 查询ES阅读量异常 - Exception异常:", e.getMessage());
        
       return readCount;
    

以上是关于es查询代码模板的主要内容,如果未能解决你的问题,请参考以下文章

MySQL慢查询日志ES索引模板

Es查询结果集默认是10000,更新设置

elasticsearch查询模板

ES 实战索引模板

es索引管理

kibana操作ES索引没有出现时间范围