【问题标题】:Elasticsearch - return aggregation to match a specific value?Elasticsearch - 返回聚合以匹配特定值?
【发布时间】:2016-05-11 03:44:11
【问题描述】:

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

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

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    如果我理解正确,您可以使用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"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    希望有帮助。

    【讨论】:

    • 有没有办法将布尔逻辑添加到聚合中,例如 categories.type = "application" 或 "sensors"?
    • 是的。您可以使用布尔过滤器。将过滤器部分更改为:"filter":{"bool":{"should":[{"term":{"categories.type":"application"}},{"term":{"categories.type": “传感器”}}]}}
    • 你知道如何为这个聚合指定一个大小参数吗?它默认为 10,但我想获得其中的 50 个。
    • 只需添加大小:50,在顶部
    • 当我尝试在聚合中的任何位置添加“size”时出现错误:3(或其他)。它到底应该去哪里?
    【解决方案2】:

    你只需要将 "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"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 我不想只匹配具有应用程序类别类型的文档。我想从匹配的文档中聚合与特定类别类型匹配的类别。
    • 我明白了。我已经改变了答案。您只需将 "include": ".*" 替换为 "include": "application"
    猜你喜欢
    • 2021-07-28
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多