背景:对于一个小说网站,我们要将作家的受欢迎程度进行一个排序
1.对于mysql中:
表结构:
其中clickcount是该本书被点击的总次数
把作家的所有作品总点击数加起来求和再进行排序
select sum(a.clickcount) countTotal,a.author,group_concat(a.name) novels_name
from vel a
where 1=1 group by author order by countTotal desc
group_concat:将字段进行拼接,并且以一行的形式返回
结果:
2.在es中实现:
我们的思路是一样的,我们就将sql语句转换为es的语法
把sql语句转换为es语句网址: /
但是在一行不好看。
所以可以用到格式化es语句网址:
进入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 排名第一
与数据库中内容一致
本文发布于:2024-01-28 21:51:27,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170644989010525.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |