的nested aggregation和filter aggregation組合似乎做什麼你想要,如果我理解正確。儘管如此,你必須將你的映射設置爲nested type。
作爲一個玩具的例子,我建立了一個簡單的指標如下:
PUT /test_index
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"properties": {
"properties": {
"type": "nested",
"properties": {
"attribute": {
"type": "string"
},
"value": {
"type": "string"
}
}
}
}
}
}
}
(注意,這是一個有點混亂,因爲「性」既是一個關鍵字和屬性的定義,在這種情況下。 )
現在我可以索引幾個文件:
POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"properties":[{"attribute":"lorem","value":"Donec a diam lectus."},{"attribute":"ipsum","value":"Sed sit amet ipsum mauris."}]}
{"index":{"_id":2}}
{"properties":[{"attribute":"dolor","value":"Donec et mollis dolor."},{"attribute":"sit","value":"Donec sed odio eros."}]}
{"index":{"_id":3}}
{"properties":[{"attribute":"amet","value":"Vivamus fermentum semper porta."}]}
然後我可以通過"properties.attribute"
過濾上"properties.value"
匯聚如下:
POST /test_index/_search?search_type=count
{
"aggs": {
"nested_properties": {
"nested": {
"path": "properties"
},
"aggs": {
"filtered_by_attribute": {
"filter": {
"terms": {
"properties.attribute": [
"lorem",
"amet"
]
}
},
"aggs": {
"value_terms": {
"terms": {
"field": "properties.value"
}
}
}
}
}
}
}
}
在這種情況下返回:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"nested_properties": {
"doc_count": 5,
"filtered_by_attribute": {
"doc_count": 2,
"value_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "a",
"doc_count": 1
},
{
"key": "diam",
"doc_count": 1
},
{
"key": "donec",
"doc_count": 1
},
{
"key": "fermentum",
"doc_count": 1
},
{
"key": "lectus",
"doc_count": 1
},
{
"key": "porta",
"doc_count": 1
},
{
"key": "semper",
"doc_count": 1
},
{
"key": "vivamus",
"doc_count": 1
}
]
}
}
}
}
}
這裏是我以前一起代碼:
http://sense.qbox.io/gist/1e0c58aae54090fadfde8856f4f6793b68de0167
非常感謝您的完整的答案,不知何故因此未通知所以我現在只看到它。在這之間,我使用瞭解決方法將屬性作爲獨立類型存儲在索引中,並存儲了每個屬性文檔對原始文檔的引用。但從我看到你的解決方案更清潔。 – TeTeT