1、构造更多测试数据
PUT /website/users/3 { "name": "黄药师", "email": "[email protected]", "birthday": "1970-10-24" }
PUT /website/blogs/3 { "title": "我是黄药师", "content": "我是黄药师啊,各位同学们!!!", "userInfo": { "userId": 3, "userName": "黄药师" } }
PUT /website/users/2 { "name": "花无缺", "email": "[email protected]", "birthday": "1980-02-02" }
PUT /website/blogs/4 { "title": "花无缺的身世揭秘", "content": "大家好,我是花无缺,所以我的身世是。。。", "userInfo": { "userId": 2, "userName": "花无缺" } } PUT /website/blogs/5 { "title": "小鱼儿的武功秘籍", "content": "我是小鱼儿啊,小鱼儿的武功秘籍!!!", "userInfo": { "userId": 1, "userName": "小鱼儿" } } |
2、对每个用户发表的博客进行分组
比如说,小鱼儿发表的那些博客,花无缺发表了哪些博客,黄药师发表了哪些博客
GET /website/blogs/_search { "size": 0, "aggs": { "group_by_username": { "terms": { "field": "userInfo.userName.keyword" }, "aggs": { "top_blogs": { "top_hits": { "_source": { "include": "title" }, "size": 5 } } } } } } 结果: { "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 4, "max_score": 0, "hits": [] }, "aggregations": { "group_by_username": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "小鱼儿", "doc_count": 2, "top_blobs": { "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "website", "_type": "blogs", "_id": "5", "_score": 1, "_source": { "title": "小鱼儿的武功秘籍" } }, { "_index": "website", "_type": "blogs", "_id": "1", "_score": 1, "_source": { "title": "小鱼儿的第一篇博客" } } ] } } }, { "key": "花无缺", "doc_count": 1, "top_blobs": { "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "website", "_type": "blogs", "_id": "4", "_score": 1, "_source": { "title": "花无缺的身世揭秘" } } ] } } }, { "key": "黄药师", "doc_count": 1, "top_blobs": { "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "website", "_type": "blogs", "_id": "3", "_score": 1, "_source": { "title": "我是黄药师" } } ] } } } ] } } } |
意思是:
对博客进行分组,然后每组返回五个博客的title