java 里面通过es查询不到数据。都要哭了。 5C

如题。java代码如下

 public List<AvgPowerVo> getAvgPower(AvgPowerParam param) {
        logger.debug("get avg power start");
        List<AvgPowerVo> list = new ArrayList<>();
        SearchResponse response = null;
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        TermsAggregationBuilder orderAgg = AggregationBuilders.terms("order ").field("pkgts_date.keyword").order(Terms.Order.term(true));
        if (StringUtils.isNotEmpty(param.getVin())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("vin", param.getVin()));
        }
        int week = param.getWeek();
        Date start = DateUtil.getLastWeekStartTime(week);
        Date end = DateUtil.getLastWeekEndTime(week);
        if (week == 0) {
            start = DateUtil.getLastWeekStartTime(12);
        }
        boolQueryBuilder.must(QueryBuilders.rangeQuery("pkgts_date").gte(start).lte(end));
        SearchRequestBuilder requestBuilder = esClient.getClient()
                .prepareSearch(esAvgPowerIndex)
                .setTypes(esAvgPowerType)
                .addAggregation(orderAgg)
                .setQuery(boolQueryBuilder);
        response = requestBuilder.execute().actionGet();
        if (response != null) {
            SearchHits searchHits = response.getHits();
            SearchHit[] hits = searchHits.getHits();
            for (SearchHit hit : hits) {
                Map<String, Object> rs = hit.getSource();
                AvgPowerVo avgPowerVo = new AvgPowerVo();
                String date = (String) rs.get("pkgts_date");
                if (StringUtils.isNotEmpty(date)) {
                    date = date.substring(0, 10);
                }
                avgPowerVo.setPkgtsDate(DateUtil.formatToDate(date));
                avgPowerVo.setAvgConsumOnce((Double) rs.get("avg_consum_once"));
                avgPowerVo.setAvgConsumAdd((Double) rs.get("avg_consum_add"));
                list.add(avgPowerVo);
            }
        }
        return list;
    }

然后这个是java里面执行的查询语句

 {
  "query" : {
    "bool" : {
      "must" : [
        {
          "match" : {
            "vin" : {
              "query" : "LMGFE1G88D1022052",
              "operator" : "OR",
              "prefix_length" : 0,
              "max_expansions" : 50,
              "fuzzy_transpositions" : true,
              "lenient" : false,
              "zero_terms_query" : "NONE",
              "boost" : 1.0
            }
          }
        },
        {
          "range" : {
            "pkgts_date" : {
              "from" : "2017-11-02T16:00:00.000Z",
              "to" : "2018-01-26T15:59:59.999Z",
              "include_lower" : true,
              "include_upper" : true,
              "boost" : 1.0
            }
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "aggregations" : {
    "order " : {
      "terms" : {
        "field" : "pkgts_date.keyword",
        "size" : 10,
        "min_doc_count" : 1,
        "shard_min_doc_count" : 0,
        "show_term_doc_count_error" : false,
        "order" : {
          "_term" : "asc"
        }
      }
    }
  }
}

然后我把这个拿到chrome客户端去查询。能够查询出来。。。

 {
"query": {
"bool": {
"must": [
{
"range": {
"pkgts_time": {
"gte": "2017-11-02T16:00:00.000Z",
"lt": "2018-01-26T15:59:59.999Z"
}
}
}
,
{
"term": {
"vin.keyword": "LMGFE1G88D1022052"
}
}
],
"must_not": [ ],
"should": [ ]
}
},
"from": 0,
"size": 50,
"sort": [ ],
"aggs": { }
}

有大神来帮忙回答下吗。

2个回答

建议 按照 插件 sql 和 head ,在sql插件中 编写sql 语句看是否能查询到,然后根据 druid 把sql 转化为 es 的api 查询 ,这样问题好定位一点

可是是分页的原因 ,默认分页的第一页的pageNum = 0

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐