Elasticsearch - return 聚合以匹配特定值?

Elasticsearch - return aggregation to match a specific value?

使用 Elasticsearch 2,是否可以 return 文档类别与特定字段值匹配的聚合?例如,我想获取 categories.type = "application".

的所有类别

我的映射如下所示:

"mappings": {
    "products": {
        "_all": {
            "enabled": true
        }, 
        "properties": { 
            "title": {
                "type": "string"
            },
            "categories": {
                "type":"nested",
                "properties": {
                    "type": {
                        "type": "string", 
                        "index": "not_analyzed"
                    },
                    "name": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
}

我的查询看起来像这样,其中 return 是所有类别类型,但我只想过滤 categories.type = "application".

的类别
{  
    "query":{  
        "multi_match": {  
            "query": "Sound",
            "fields": [  
                "title"
            ]
        }
    },
    "aggs":{
        "Applications": {
            "nested": {
                "path": "categories"
            },
            "aggs": {
                "meta": {
                    "terms": {
                        "field": "categories.type"
                    },
                    "aggs": {
                        "name": {
                            "terms": {
                                "field": "categories.name"
                            }
                        }
                    }
                }
            }
        }
    }
}

您只需要将 "include": ".*" 替换为 "include": "application"

{  
    "query":{  
        "multi_match": {  
            "query": "Sound",
            "fields": [  
                "title"
            ]
        }
    },
    "aggs":{
        "Applications": {
            "nested": {
                "path": "categories"
            },
            "aggs": {
                "meta": {
                    "terms": {
                        "field": "categories.type"
                        , "include": "application"
                    },
                    "aggs": {
                        "name": {
                            "terms": {
                                "field": "categories.name"
                            }
                        }
                    }
                }
            }
        }
    }
}

如果我没看错你可以用aggregation filter:

{   
    size : 50,
    "query":{  
        "multi_match": {  
            "query": "Sound",
            "fields": [  
                "title"
            ]
        }
    },
    "aggs":{
        "Applications": {
            "nested": {
                "path": "categories"
            },
            "aggs": {
                "meta": {
                    "filter" : {
                        "term" : {
                            "categories.type" : "application"
                        }
                    },
                    "aggs": {
                        "name": {
                            "terms": {
                                "field": "categories.name"
                            }
                        }
                    }
                }
            }
        }
    }
}

希望对您有所帮助。