【问题标题】:ElasticSearch - Aggregations with document detailsElasticSearch - 具有文档详细信息的聚合
【发布时间】:2015-03-03 20:03:44
【问题描述】:

我需要汇总以下文档:

{
    "title": "American Psycho",
    "releaseDate": "7/06/2000",
    "imdbRate": "7.6",
    "casting": [
        {
            "name": "Christian Bale",
            "category": "Actor"
        },
        {
            "name": "Justin Theroux",
            "category": "Actor"
        }
    ]
}

{
    "title": "The Dark Knight",
    "releaseDate": "13/08/2008",
    "imdbRate": "9.0",
    "casting": [
        {
            "name": "Christian Bale",
            "category": "Actor"
        },
        {
            "name": "Morgan Freeman",
            "category": "Actor"
        }
    ]
}

按演员,并希望得到以下结构:

 [
    {"name": "Christian Bale"},
    {"movies": [
        {
            "title": "American Psycho",
            "releaseDate": "7/06/2000",
            "imdbRate": "7.6"
        },
        {
            "title": "The Dark Knight",
            "releaseDate": "13/08/2008",
            "imdbRate": "9.0"
        }, ...
]

如果使用基于 cast.name 字段的标准术语聚合,我如何检索相关文档的 releaseDate 和 imdbRate? 对于每个演员,我还需要按 releaseDate asc 对电影进行排序。

我可以使用一个请求来执行此操作吗?

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    由于您的文档中有一组casting 对象,因此您需要在映射中使用嵌套类型。要获得所需的聚合,您需要 Terms AggregationsNested AggregationsReverse Nested Aggregations 的组合。下面是一个例子。

    使用映射创建和索引:

    POST /test
    {
        "mappings": {
            "movie": {
                "properties": {
                    "title": {
                        "type": "string",
                        "fields": {
                            "raw": {
                                "type": "string",
                                "index": "not_analyzed"
                            }
                        }
                    },
                    "releaseDate": {
                        "type": "string",
                         "index": "not_analyzed"
                    },
                    "casting": {
                        "type": "nested",
                        "properties": {
                            "name": {
                                "type": "string",
                                "fields":{
                                "raw": {
                                    "type": "string",
                                    "index": "not_analyzed"
                                }
                                }
                            },
                            "category": {
                                "type": "string",
                                "fields":{
                                    "raw": {
                                        "type": "string",
                                        "index": "not_analyzed"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    索引文档:

    POST /test/movie/1
    {
        "title": "American Psycho",
        "releaseDate": "7/06/2000",
        "imdbRate": "7.6",
        "casting": [
            {
                "name": "Christian Bale",
                "category": "Actor"
            },
            {
                "name": "Justin Theroux",
                "category": "Actor"
            }
        ]
    }
    
    POST /test/movie/2
    {
        "title": "The Dark Knight",
        "releaseDate": "13/08/2008",
        "imdbRate": "9.0",
        "casting": [
            {
                "name": "Christian Bale",
                "category": "Actor"
            },
            {
                "name": "Morgan Freeman",
                "category": "Actor"
            }
        ]
    }
    

    最后搜索:

    POST /test/movie/_search?search_type=count
    {
        "aggs": {
            "nested_path": {
                "nested": {
                    "path": "casting"
                },
                "aggs": {
                    "actor_name": {
                        "terms": {
                            "field": "casting.name.raw"
                        },
                        "aggs": {
                            "movies": {
                                "reverse_nested": {},
                                "aggs": {
                                    "movie_title": {
                                        "terms": {
                                            "field": "title.raw"
                                        },
                                        "aggs": {
                                            "release_date": {
                                                "terms": {
                                                    "field": "releaseDate"
                                                }
                                            },
                                            "imdbRate_date": {
                                                "terms": {
                                                    "field": "imdbRate"
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    克里斯蒂安·贝尔的回应是:

    {
        "key": "Christian Bale",
        "doc_count": 2,
        "movies": {
            "doc_count": 2,
            "movie_title": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "American Psycho",
                        "doc_count": 1,
                        "release_date": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "7/06/2000",
                                    "doc_count": 1
                                }
                            ]
                        },
                        "imdbRate_date": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "7.6",
                                    "doc_count": 1
                                }
                            ]
                        }
                    },
                    {
                        "key": "The Dark Knight",
                        "doc_count": 1,
                        "release_date": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "13/08/2008",
                                    "doc_count": 1
                                }
                            ]
                        },
                        "imdbRate_date": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "9.0",
                                    "doc_count": 1
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
    

    【讨论】:

    • 好的,所以没有办法避免子聚合来检索 releaseDate/imdbRate,非常感谢!
    猜你喜欢
    • 2010-12-06
    • 1970-01-01
    • 2013-03-16
    • 2021-07-26
    • 2014-03-01
    • 2019-05-30
    • 2015-01-28
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多