无法在Elasticsearch中执行聚合

时间:2017-11-27 22:49:12

标签: elasticsearch elasticsearch-aggregation

我是Elasticsearch的新手,并且一直在使用它的高级休息客户端来执行日期范围的聚合。当我运行代码时,查询会成功执行,但返回的对象的null为聚合值:

这就是我的尝试:

POST /index_name/1234/_search?size=0
{
    "aggs": {
        "range": {
            "date_range": {
                "field": "date_created",
                "format": "MM",
                "ranges": [
                    { "from": "now-0M/M" } 
                ]
            }
        }
    }
}

searchResponse对象将聚合作为空值。

但是当我在Kibana中运行聚合查询时,这就是我得到的

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "range": {
      "buckets": [
        {
          "key": "11-*",
          "from": 1509494400000,
          "from_as_string": "11",
          "doc_count": 2
        }
      ]
    }
  }
}

输出:

"hits": [
      {
        "_index": "index_name",
        "_type": "1234",
        "_id": "AV__ZmN6KNpjod2Y_7PE",
        "_score": 1,
        "_source": {
          "category": 1234,
          "i_id": 87665,
          "v_Id": 12,
          "date_created": "2017-11-27T14:33:38.954Z"
        }
      },
      {
        "_index": "index_name",
        "_type": "1234",
        "_id": "AV_-XzlpzSX1UeitwVjd",
        "_score": 1,
        "_source": {
          "category": 1234,
          "i_id": 8766,
          "v_Id": 2,
          "date_created": "2017-11-27T09:46:00.095Z"
        }
      }
    ]

有人可以帮我这个吗?我有点困难了。

根据请求添加样本数据:

{{1}}

谢谢!

1 个答案:

答案 0 :(得分:0)

最后,我找到了问题的答案。创建聚合对象后,我们必须手动将其设置为SearchRequest。

Stream -> Stream Object

添加了修复代码的行:

.each(console.log)