基于Elasticsearch中两个字段的聚合导致SearchParseExcepetion“无法找到聚合器类型”

时间:2014-11-28 01:20:25

标签: elasticsearch elasticsearch-aggregation

我正在使用Elasticsearch 1.4.0 并尝试使用聚合功能。我不断收到带有Cannot find aggregator type [fieldName] in [aggregationName]消息的SearchParseException。

在JSON格式中,我的数据如下所示。

{ "userCode": "abcd123", "response": 1 }
{ "userCode": "abcd123", "response": 1 }
{ "userCode": "abcd123", "response": 0 }
{ "userCode": "wxyz123", "response": 0 }
{ "userCode": "wxyz123", "response": 0 }
{ "userCode": "wxyz123", "response": 1 }

注意,有2个用户abcd123wxyz123,我只想计算每个响应1和0的次数。如果我将这些数据放入SQL表中,则在SQL中选择语法,我会做这样的事情(如果这个SQL示例有助于说明我想要做的事情)。

select userCode, response, count(*) as total
from response_table
group by userCode, response

我希望结果集如下所示。

abcd123, 0, 1 //user abcd123 responded 0 once
abcd123, 1, 2 //user abcd123 responded 1 twice
wxyz123, 0, 2 //user wxyz123 responded 0 twice
wxyz123, 1, 1 //user wxyz123 responded 1 once

对于Elasticsearch,我的聚合JSON如下所示。

{
 "aggs": {
  "users": {
   "terms": { "field": "userCode" },
   "aggs": {
    "responses" : {
     "terms": { "field": "response" }
    }
   }
  }
 }
}

但是,我得到了SearchParseException:Cannot find aggregator type [responses] in [aggs]。我究竟做错了什么?

如果有帮助,我的映射文件非常简单,如下所示。

{
  "data": {
    "properties": {
      "userCode": {
        "type": "string",
        "store": "yes",
        "index": "analyzed",
        "term_vector": "no"
      },
      "response": {
        "type": "integer",
        "store": "yes",
        "index": "analyzed",
        "term_vector": "yes"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:2)

以下聚合对我有用(它让我得到了我想要的结果),但是我仍然希望澄清为什么我之前的方法导致了SearchParseException。

{
 "aggs": {
  "users": {
   "terms": { "field" : "userCode" },
   "aggs": {
    "responses": {
     "histogram": { "field": "response", "interval": 1 }
    }
   }
  }
 }
}