数组项上的 Elasticsearch 聚合

Elasticsearch aggregation on array items

下面是我的 Elasticsearch 文档,我想在其中触发聚合查询。

{
  "id": 1,
  "attributes": [
    {
      "fieldId": 1,
      "value": "Male"
    },
    {
      "fieldId": 2,
      "value": "12/11/2015"
    }
  ]
}
{
  "id": 2,
  "attributes": [
    {
      "fieldId": 1,
      "value": "Male"
    },
    {
      "fieldId": 2,
      "value": "11/11/2015"
    }
  ]
}

结果必须如下。

[
  {
    "key": "Male",
    "doc_count": 1
  }
]
[
  {
    "key": "12/11/2015",
    "doc_count": 1
  },
  {
    "key": "11/11/2015",
    "doc_count": 1
  }
]

有没有办法在 Elasticsearch 中实现这一点?

这是可能的。看这个例子:

我们必须将属性映射为 nested 类型才能正确聚合。

PUT /test
{
  "mappings": {
    "sample": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "attributes": {
          "type": "nested",
          "properties": {
            "fieldId": {
              "type": "integer"
            },
            "value": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}

让我们添加您给定的测试数据:

PUT /test/sample/1
{"id":1,"attributes":[{"fieldId":1,"value":"Male"},{"fieldId":2,"value":"12/11/2015"}]}
PUT /test/sample/2
{"id":2,"attributes":[{"fieldId":1,"value":"Male"},{"fieldId":2,"value":"11/11/2015"}]}

最后 运行 这个查询:

GET /test/_search
{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "Nest": {
      "nested": {
        "path": "attributes"
      },
      "aggs": {
        "fieldIds": {
          "terms": {
            "field": "attributes.fieldId",
            "size": 0
          },
          "aggs": {
            "values": {
              "terms": {
                "field": "attributes.value",
                "size": 0
              }
            }
          }
        }
      }
    }
  }
}

它会做什么?

  1. 运行 nested 首先聚合,以便进入 nested 对象并正确聚合它们。
  2. 使用 terms 聚合为每个 fieldId 创建存储桶,在您的情况下,我们将获得其中两个:12
  3. 运行 terms 对上面的每个桶再次聚合以获得相应的值。

这就是输出。

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "Nest": {
      "doc_count": 4,
      "fieldIds": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": 1,
            "doc_count": 2,
            "values": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "Male",
                  "doc_count": 2
                }
              ]
            }
          },
          {
            "key": 2,
            "doc_count": 2,
            "values": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "11/11/2015",
                  "doc_count": 1
                },
                {
                  "key": "12/11/2015",
                  "doc_count": 1
                }
              ]
            }
          }
        ]
      }
    }
  }
}

这与您要求的不完全一样。但这是您在 Elasticsearch 中所能获得的最接近的结果。