【问题标题】:Elasticsearch aggregation return always empty buckets [] (elasticsearch version 2.4.1)Elasticsearch 聚合总是返回空存储桶 [](elasticsearch 版本 2.4.1)
【发布时间】:2017-01-19 05:56:53
【问题描述】:

我有以下代码:

 final String index = ElasticSearchUtils.getIndexNameForExecution(queryId);
        SearchRequestBuilder query = client.prepareSearch(index);
        query.setTypes(indexType.toString());

        query.addAggregation(terms("errors").field("code").size(NUMBER_OF_HITS).order(Terms.Order.count(false)));

        int pageStart = getFrom(page) * size;
        SearchResponse response = query.setFrom(pageStart).setSize(getPageSize(size)).execute().actionGet();

        return response.toString();

部分回应是:

{
  "took" : 78,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "index_587e1e34e4b040c63c49137f",
      "_type" : "ERROR",
      "_id" : "AVmspgsKa7ZIkZu1p32G",
      "_score" : 1.0,
      "_source" : {
        "agent_id" : "{8668b249-9443-e611-87c6-005056aa41d1}",
        "_v" : "1",
        "host" : "RHEL65-X86-DEMO",
        "created_at" : "2017-01-17T13:37:58.496Z",
        "qid" : "587e1e34e4b040c63c49137f",
        "errors" : [ {
          "code" : 769,
          "module" : "FileHashing",
          "function" : "FindFiles"
        } ]
      }
    }, {
      "_index" : "index_587e1e34e4b040c63c49137f",
      "_type" : "ERROR",
      "_id" : "AVmspgsKa7ZIkZu1p32H",
      "_score" : 1.0,
      "_source" : {
        "agent_id" : "{7238f027-fbfc-47cf-85b0-c69838e26a2a}",
        "_v" : "1",
        "host" : "W8-X64-DEMO",
        "created_at" : "2017-01-17T13:37:58.501Z",
        "qid" : "587e1e34e4b040c63c49137f",
        "errors" : [ {
          "code" : 769,
          "module" : "FileHashing",
          "function" : "FindFiles"
        } ]
      }
    } ]
  },
  "aggregations" : {
    "errors" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ ]
    }
  }
}

And the result of search execution is:

如您所见,存储桶为空 [],聚合不起作用,但会抛出任何异常。这发生在 elasticsearch 2.4.1 上,同样的代码也适用于 elasticsearch 1.4.1

【问题讨论】:

    标签: java elasticsearch elasticsearch-aggregation


    【解决方案1】:

    你只需要修复这条线

    query.addAggregation(terms("errors").field("errors.code").size(NUMBER_OF_HITS).order(Terms.Order.count(false)));
                                                  ^
                                                  |
                                              add this
    

    【讨论】:

    • 酷,很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 2014-07-01
    相关资源
    最近更新 更多