0

我有以下代码:

 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

4

1 回答 1

0

你只需要修复这条线

query.addAggregation(terms("errors").field("errors.code").size(NUMBER_OF_HITS).order(Terms.Order.count(false)));
                                              ^
                                              |
                                          add this
于 2017-01-18T21:56:53.517 回答