Elasticsearch - 返回聚合以匹配特定值?

标签 elasticsearch aggregation

使用 Elasticsearch 2,是否可以返回文档类别与特定字段值匹配的聚合?例如,我想获取 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"
                    }
                }
            }
        }
    }
}

我的查询看起来像这样,它返回所有类别类型,但我只想过滤 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"
                            }
                        }
                    }
                }
            }
        }
    }
}

最佳答案

您可以使用 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"
                            }
                        }
                    }
                }
            }
        }
    }
}

希望对您有所帮助。

关于Elasticsearch - 返回聚合以匹配特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37079445/

相关文章:

mongodb - $查找数组mongo聚合中的每个元素

elasticsearch - 在 Elasticsearch 中匹配精确短语

python - celery 任务分组/聚合

python-3.x - ElasticSearch中的地理距离排序?

elasticsearch - Elasticsearch是否仅对_search端点或全部支持GET over POST?

aggregate-functions - 如何聚合/汇总百分位测量

r - 从一列中提取另一列中的每个组的列表

sql - 用于数据分析的 NoSQL 或 RDBMS

elasticsearch - 使用轻松的脚本循环点击

elasticsearch - 如何在Elasticsearch的一个字段中配置多个分析器?