zoukankan      html  css  js  c++  java
  • elasticsearch过滤聚合(Keyword和Nested聚合),先过滤,再根据过滤结果聚合,并排序

    elasticsearch分类聚合

    先模糊检索过滤后,再对结果聚合

    1. 对普通字段或数组类型聚合(默认按聚合数量排序)

    例子:对Keyword类型字段sponsor聚合,先进行模糊检索,再对检索的结果聚合

    {
        "size": 0,
        "query": {
            "bool": {
                "must": {
                    "wildcard": {
                        "sponsor": "*University*"
                    }
                }
            }
        },
        "aggs": {
            "sponsor": {
                "terms": {
                    "field": "sponsor",
                    "size":100
                }
            }
        }
    }
    
    2. 对nested类型字段聚合(默认按聚合数量排序)

    例子:对nested类型字段researchAreas对象的subjectName聚合,先进行模糊检索,再对检索的结果聚合

    {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    {
                        "nested": {
                            "path": "researchAreas",
                            "query": {
                                "bool": {
                                    "must": {
                                        "wildcard": {
                                            "researchAreas.subjectName": "*and*"
                                        }
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        },
        "aggs": {
            "researchAreas_aggs": {
                "nested": {
                    "path": "researchAreas"
                },
                "aggs": {
                    "filter_term": {
                        "filter": {
                            "wildcard": {
                                "researchAreas.subjectName": "*and*"
                            }
                        },
                        "aggs": {
                            "subjectName": {
                                "terms": {
                                    "field": "researchAreas.subjectName",
                                    "size": 100
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    3. 引入order对象,在例子1的基础上,添加自主排序

    例子:按聚合词项字符串的字母顺序排序

    {
        "size": 0,
        "query": {
            "bool": {
                "must": {
                    "wildcard": {
                        "sponsor": "*University*"
                    }
                }
            }
        },
        "aggs": {
            "sponsor": {
                "terms": {
                    "field": "sponsor",
                    "size": 100,
                    "order":{
                        "_term":"asc"
                    }
                }
            }
        }
    }
    
    更多聚合操作详情请见elasticSreach权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/aggregations.html
  • 相关阅读:
    kindeditor 创建多个 取值方式
    新浪微博分享平台地址
    thinkphp 直接写SQL
    nginx下禁止目录运行php
    phpcms_v9 同步登陆的BUG
    yii framework 创建项目
    phpcms_v9 关闭debug
    ucenter忘记创始人密码简单解决方法
    XSS 常见手段
    如何在 C 中使用 64 位整数?
  • 原文地址:https://www.cnblogs.com/zys-blog/p/13207133.html
Copyright © 2011-2022 走看看