【问题标题】:Aggregation based on two fields in Elasticsearch results in SearchParseExcepetion with "cannot find aggregator type"基于 Elasticsearch 中两个字段的聚合结果在 SearchParseExcepetion 中出现“找不到聚合器类型”
【发布时间】:2015-01-26 15:27:34
【问题描述】:

我正在使用 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 select语法中,我会做这样的事情(如果这个 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.4.0 中。

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

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

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

【讨论】:

  • 我误读了文档并认为嵌套的aggs 是根aggs 的子节点,而实际上它是组的一部分(这也是您最初的错误)。感谢您发布此内容,我快疯了,确定我做的一切都是正确的。
猜你喜欢
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多