作家排行榜topN

背景:对于一个小说网站,我们要将作家的受欢迎程度进行一个排序
1.对于mysql中:
表结构:
在这里插入图片描述
其中clickcount是该本书被点击的总次数
把作家的所有作品总点击数加起来求和再进行排序

select sum(a.clickcount) countTotal,a.author,group_concat(a.name) novels_name
from db_novel.novel a
where 1=1 group by author order by countTotal desc

group_concat:将字段进行拼接,并且以一行的形式返回
结果:
在这里插入图片描述

2.在es中实现:
我们的思路是一样的,我们就将sql语句转换为es的语法
把sql语句转换为es语句网址: http://www.ischoolbar.com/EsParser/
在这里插入图片描述
但是在一行不好看。
所以可以用到格式化es语句网址:http://www.bejson.com
在这里插入图片描述
进入kibana
1.创建作家排行的索引
PUT novel_author_countsort
{
“mappings” : {
“doc” : {
“properties” : {
“@timestamp” : {
“type” : “date”
},
“@version” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“author” : {
“type” : “keyword”
},
“category” : {
“type” : “keyword”
},
“clickcount” : {
“type” : “long”
},
“collect” : {
“type” : “long”
},
“count” : {
“type” : “long”
},
“countrecommend” : {
“type” : “long”
},
“detail” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“detaul” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“id” : {
“type” : “long”
},
“lastchapter” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“lastupdate” : {
“type” : “date”,
“format” : “yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis”
},
“monthclick” : {
“type” : “long”
},
“monthrecommend” : {
“type” : “long”
},
“name” : {
“type” : “keyword”
},
“new” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“novelinfo” : {
“type” : “keyword”
},
“picurl” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“status” : {
“type” : “text”,
“fields” : {
“keyword” : {
“type” : “keyword”,
“ignore_above” : 256
}
}
},
“weekclick” : {
“type” : “long”
},
“weekrecommend” : {
“type” : “long”
}
}
}
}
}
将author字段类型改为keyword
在这里插入图片描述
2.给索引加载数据
POST _reindex
{
“source”: {
“index”: “novel_new”
},
“dest”: {
“index”: “novel_author_countsort”
}
}
3.测试,将之前转行后的内容直接cv过来
GET novel_author_countsort/_search
{
“size” : 0,
“aggs”: {
“author”: {
“terms”: {
“field”: “author”,
“size”: 10,
“order”: {
“countTotal”: “DESC”
}
},
“aggs”: {
“countTotal”: {
“sum”: {
“field”: “clickcount”
}
},
“top”: {
“top_hits”: {
“size”: 1
}
}
}
}
}
}
4.查看结果
在这里插入图片描述
可以看到作者:危险的世界 countTotal:3339494 排名第一
在这里插入图片描述
与数据库中内容一致

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值