白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)
生活随笔
收集整理的這篇文章主要介紹了
白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 概述
- 官網
- 示例
概述
繼續跟中華石杉老師學習ES,第55篇
課程地址: https://www.roncoo.com/view/55
官網
Top Hits Aggregation : 戳這里
其他詳見官網
示例
需求: 對每個用戶發表的博客進行分組
模擬一批數據
PUT /blogs2/blogs2/2 {"title": "2跟石杉老師學ES","content": "2-second blog","userInfo": {"userId": 2,"username": "2小工匠"} }PUT /blogs2/blogs2/3 {"title": "3跟石杉老師學ES","content": "3-second blog","userInfo": {"userId": 3,"username": "3小工匠"} }PUT /blogs2/blogs2/4 {"title": "4跟石杉老師學ES","content": "4-second blog","userInfo": {"userId": 4,"username": "4小工匠"} }PUT /blogs2/blogs2/5 {"title": "5跟石杉老師學ES","content": "5-second blog","userInfo": {"userId": 2,"username": "2小工匠"} }PUT /blogs2/blogs2/6 {"title": "6跟石杉老師學ES","content": "6-second blog","userInfo": {"userId": 3,"username": "3小工匠"} }PUT /blogs2/blogs2/7 {"title": "7跟石杉老師學ES","content": "7-second blog","userInfo": {"userId": 4,"username": "4小工匠"} }DSL
#對每個用戶發表的博客進行分組,取前5篇的標題 GET /blogs2/blogs2/_search {"size": 0,"aggs": {"group_by_userName": {"terms": {"field": "userInfo.username.keyword"},"aggs": {"top_blog": {"top_hits": {"_source": {"includes": "title"},"size": 5}}}}} }返回:
{"took": 9,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 7,"max_score": 0,"hits": []},"aggregations": {"group_by_userName": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "2小工匠","doc_count": 2,"first_blog": {"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "blogs2","_type": "blogs2","_id": "5","_score": 1,"_source": {"title": "5跟石杉老師學ES"}},{"_index": "blogs2","_type": "blogs2","_id": "2","_score": 1,"_source": {"title": "2跟石杉老師學ES"}}]}}},{"key": "3小工匠","doc_count": 2,"first_blog": {"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "blogs2","_type": "blogs2","_id": "6","_score": 1,"_source": {"title": "6跟石杉老師學ES"}},{"_index": "blogs2","_type": "blogs2","_id": "3","_score": 1,"_source": {"title": "3跟石杉老師學ES"}}]}}},{"key": "4小工匠","doc_count": 2,"first_blog": {"hits": {"total": 2,"max_score": 1,"hits": [{"_index": "blogs2","_type": "blogs2","_id": "4","_score": 1,"_source": {"title": "4跟石杉老師學ES"}},{"_index": "blogs2","_type": "blogs2","_id": "7","_score": 1,"_source": {"title": "7跟石杉老師學ES"}}]}}},{"key": "小工匠","doc_count": 1,"first_blog": {"hits": {"total": 1,"max_score": 1,"hits": [{"_index": "blogs2","_type": "blogs2","_id": "1","_score": 1,"_source": {"title": "跟石杉老師學ES"}}]}}}]}} }總結
以上是生活随笔為你收集整理的白话Elasticsearch55-数据建模之对每个用户发表的博客进行分组 (Top Hits Aggregation)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 白话Elasticsearch54-数据
- 下一篇: 白话Elasticsearch56-数据