ElasticSearch简单的Java API BoolQueryBuilder使用

must的性能要低一些,为什么?因为他要进行打分评估,也就是说要进行_score,而filter则不会。 

 /**
     * 拼装查询条件
     * @param query
     * @return
     */
    public static BoolQueryBuilder buildEsParam(QueryGatewayTradeEsVo query) {

        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        
        //filter 效率比 must高的多 
        if (StringUtils.isNotBlank(query.getRouterDatabaseNo())) {
            queryBuilder.filter(QueryBuilders.termQuery("routerDatabaseNo", query.getRouterDatabaseNo()));
        }

        if (StringUtils.isNotBlank(query.getTradeNo())) {
            queryBuilder.filter(QueryBuilders.termQuery("tradeNo", query.getTradeNo()));
        }

        if (null != query.getSystemId()) {
            queryBuilder.filter(QueryBuilders.termQuery("systemId", query.getSystemId()));
        }

        if (null != query.getRefundType()) {
            queryBuilder.filter(QueryBuilders.termQuery("refundType", query.getRefundType()));
        }

        if (null != query.getRefundStatus()) {
            queryBuilder.filter(QueryBuilders.termQuery("refundStatus", query.getRefundStatus()));
        }

        //时间段 一定要有头有尾 不然会出现慢查询
        if (null != query.getCreateTime() && null != query.getUpdateTime()) {
            queryBuilder.filter(QueryBuilders.rangeQuery("createTime").from( query.getCreateTime()).to(query.getUpdateTime()));
        }

        return queryBuilder;
    }

 

你可能感兴趣的:(ElasticSearch)