62.对每个用户发表的博客进行分组

本节的需求:对已建好冗余数模型的数据进行分组。

本节的语法没有学懂

   

一、构造更多测试数据

   

PUT /website/users/3

{

"name": "黄药师",

"email": "huangyaoshi@sina.com",

"birthday": "1970-10-24"

}

   

PUT /website/blogs/3

{

"title": "我是黄药师",

"content": "我是黄药师啊,各位同学们!!!",

"userInfo": {

"userId": 3,

"username": "黄药师"

}

}

   

PUT /website/users/2

{

"name": "花无缺",

"email": "huawuque@sina.com",

"birthday": "1980-02-02"

}

   

PUT /website/blogs/2

{

"title": "花无缺的身世揭秘",

"content": "大家好,我是花无缺,所以我的身世是。。。",

"userInfo": {

"userId": 2,

"username": "花无缺"

}

}

   

二、对每个用户发表的博客进行分组

   

比如说,小鱼儿发表的那些博客,花无缺发表了哪些博客,黄药师发表了哪些博客

   

GET /website/blogs/_search

{

"size": 0,

"aggs": {

"group_by_username": {

"terms": {

"field": "userInfo.username.keyword"

},

"aggs": {

"top_blogs": {

"top_hits": {

"_source": {

"includes": "title"

},

"size": 5

}

}

}

}

}

}

原文地址:https://www.cnblogs.com/liuqianli/p/8542123.html