Elasticsearch 数组值计数聚合

标签 elasticsearch elasticsearch-2.0 elasticsearch-aggregation

示例文档:

{
    "id": "62655",
    "attributes": [
        {
            "name": "genre",
            "value": "comedy"
        },
        {
            "name": "year",
            "value": "2016"
        }
    ]
}

{
    "id": "62656",
    "attributes": [
        {
            "name": "genre",
            "value": "horror"
        },
        {
            "name": "year",
            "value": "2016"
        }
    ]
}

{
    "id": "62657",
    "attributes": [
        {
            "name": "language",
            "value": "english"
        },
        {
            "name": "year",
            "value": "2015"
        }
    ]
}

预期输出:

{
    "hits" : {
        "total": 3,
        "hits": []
    },
    "aggregations": {
        "attribCount": {
            "language": 1,
            "genre": 2,
            "year": 3
        },
        "attribVals": {
            "language": {
                "english": 1
            },
            "genre": {
                "comedy": 1,
                "horror": 1
            },
            "year": {
                "2016": 2,
                "2015": 1
            }
        }
    }
}

我的查询:

我可以使用以下查询获取“attribCount”聚合。但是我不知道如何获取每个属性值计数。

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            }
        }
    },
    "aggs": {
        "attribCount": {
            "terms": {
                "field": "attributes.name",
                "size": 0
            }
        }
    },
    "size": 0
}

当我使用 attributes.value 进行聚合时,它给出了总计数。但我需要它列在预期输出中给定的名称值下。

最佳答案

正如您所说,属性字段是嵌套的。 试试这个,这会起作用

{
  "size": 0,
  "aggs": {
    "count": {
      "nested": {
        "path": "attributes"
      },
      "aggs": {
        "attribCount": {
          "terms": {
            "field": "attributes.name"
          }
        },
        "attribVal": {
          "terms": {
            "field": "attributes.name"
          },
          "aggs": {
            "attribval2": {
              "terms": {
                "field": "attributes.value"
              }
            }
          }
        }
      }
    }
  }
}

关于Elasticsearch 数组值计数聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39590769/

相关文章:

java - ES 1.7.3 Elasticsearch Java 客户端 : significant terms aggregation unknown values

elasticsearch - 无法在同一台计算机上运行两个单独版本的Elasticsearch

elasticsearch - ElasticSearch 2.X查找数组中存在数值失败

elasticsearch - 按日期和类别 Elasticsearch 查询分组

elasticsearch - 如何根据日期字段从 Elasticsearch 获取最新数据

elasticsearch - 查询ElasticSearch中格式错误的空子句错误

azure - 如何连接到Azure上安装的多个节点的elasticsearch集群?如何获取elasticsearch端点?

docker - Ansible文件以部署特定版本的 Elasticsearch

elasticsearch 2.0 父子孙子

elasticsearch - 弹性,我可以将我自己的全局序号与无痛术语聚合脚本一起使用吗?