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