1

样本文件Elasticsearch数组值计数聚集

{ 
    "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”股份公司使用下面的查询进行gregation。但我不知道如何获取每个属性值。

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

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

+0

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html – blackmamba

+0

@blackmamba我已经看了在它。它不清楚如何完成我的要求。当我给路径作为“属性”时,我得到异常。当我给“attributes.name”,它得到计数为0 – Sriram

+0

你已经映射属性和它的父亲作为嵌套的权利? – blackmamba

回答

1

正如你所说属性字段是嵌套的。 试试这个,这将工作

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

嘿,它工作? – blackmamba

+0

刚刚遇到http://stackoverflow.com/a/31052532/1179958这个答案,这正是我所寻找的,当我在这里告诉你,你已经回答了它:) – Sriram