Java查询ES,普通查询和嵌套查询同时存在

1、近期开发涉及到了ES搜索引擎的使用,涉及到的数据结构比较复杂,父子结构中还有嵌套查询。使用的是ElasticSearchTemplate设置SearchQuery对ES进行条件查询。当查询条件中有普通查询和嵌套查询同时存在时,出现了问题:

1.1 所有条件都为NestQueryBuilder时,设置的所有条件都会去Nested的path去查询符合条件的数据,当嵌套内容中没有对应的字段时,查询出来的数据为0.
1.2 所有条件为普通的QueryBuilder时,嵌套查询的内容会不生效。
1.3 当ElasticSearchTemplate设置多个query时,query条件会被覆盖掉,只会保存最后一个query,导致查询的数据不准确。

2、目前的解决办法是将嵌套查询的QueryBuilder设置QueryBuilders.boolQuery().filter,这样同时设置query和filter。

3、期待遇到更好的解决办法。

4、补充

4.1 父子结构查询
  • JoinQueryBuilders.hasChildQuery
4.2 kibana普通查询和嵌套查询
GET /goods_info/subject/_search
{
  "query": {
    "has_child": { -- 父子结构查询
      "type": "list", -- 索引类型
      "query": {
        "bool": {
          "must": [
            {"match": {
              "brandId": "17"
            }},
            {
              "nested": {
              "path": "labels", -- 嵌套路径
                "query": {
                  "bool": {
                    "must": [
                      {"match": {
                        "labels.labelType": "2" -- 路径字段
                      }},
                      {
                        "match": {
                          "labels.labelValue": "2"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
发布了6 篇原创文章 · 获赞 1 · 访问量 4031
展开阅读全文

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

01-26

如题。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": { } } ``` 有大神来帮忙回答下吗。 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览