Elasticsearch聚合返回总是空桶[](elasticsearch版本2.4.1)

时间:2017-01-18 20:23:01

标签: java elasticsearch elasticsearch-aggregation

我有以下代码:

 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一起使用

1 个答案:

答案 0 :(得分:0)

您只需要修复此行

SectionHeaderController@store