Commit b845c3f0 authored by 刘基明's avatar 刘基明

排序落库

parent ca40b021
...@@ -49,18 +49,13 @@ public class ThemeAnalysDO { ...@@ -49,18 +49,13 @@ public class ThemeAnalysDO {
@ApiModelProperty(value = "用户质量") @ApiModelProperty(value = "用户质量")
private Double userWeight = 0.0; private Double userWeight = 0.0;
private Double score; public Double getRank() {
public Double calcScore() {
// 质量=帖子质量+用户质量 // 质量=帖子质量+用户质量
double w = (double) (viewCount * 0.1 + forwardCount * 3 + commentCount * 2 + likeCount * 1 + collectCount * 3) + userWeight; double w = (double) (viewCount * 0.1 + forwardCount * 3 + commentCount * 2 + likeCount * 1 + collectCount * 3) + userWeight;
double i = 1;//初试权重 double i = 1;//初试权重
double t = Double.valueOf(minuteTillNow) / 60; double t = Double.valueOf(minuteTillNow) / 60;
double g = 0.1;//时间系数 double g = 0.1;//时间系数
double rank = (w + i) / Math.pow(t + 1, g); return (w + i) / Math.pow(t + 1, g);
this.score = score;
return rank;
} }
} }
...@@ -40,21 +40,19 @@ public class TopicRankQo { ...@@ -40,21 +40,19 @@ public class TopicRankQo {
private Integer hoursTillNow; private Integer hoursTillNow;
private Double score;
/** /**
* 热度计算算法 * 热度计算算法
* *
* @return * @return
*/ */
public Double getRank() { public Double getScore() {
double g = 0.3;//时间系数 double g = 0.3;//时间系数
//顶置话题 //顶置话题
if (isTop > 0) { if (isTop > 0) {
return Double.MAX_VALUE; return Double.MAX_VALUE;
} }
Double socre = ((disscussCount * 3 + viewCount) / Math.pow(hoursTillNow + 1, g)) + themeWeight; Double socre = ((disscussCount * 3 + viewCount) / Math.pow(hoursTillNow + 1, g)) + themeWeight;
this.score = socre;
return socre; return socre;
} }
} }
...@@ -96,7 +96,7 @@ public class RankService { ...@@ -96,7 +96,7 @@ public class RankService {
} }
} }
//打分 //打分
Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::calcScore)); Map<ThemeAnalysDO, Double> map = themeAnalysDOS.stream().collect(Collectors.toMap(o -> o, ThemeAnalysDO::getRank));
//排序 //排序
hotestThemes = map.entrySet().stream() hotestThemes = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
...@@ -139,14 +139,14 @@ public class RankService { ...@@ -139,14 +139,14 @@ public class RankService {
topic.setDisscussCount(themeIds.size() + commentCount); topic.setDisscussCount(themeIds.size() + commentCount);
//帖子权重,求和 //帖子权重,求和
double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId())) double themeSum = getHotestThemes().stream().filter(o -> topic.getTopicId().equals(o.getTopicId()))
.mapToDouble(ThemeAnalysDO::calcScore) .mapToDouble(ThemeAnalysDO::getRank)
.sum(); .sum();
topic.setThemeWeight(themeSum); topic.setThemeWeight(themeSum);
//格式化浏览量、讨论量 //格式化浏览量、讨论量
topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount())); topic.setFormatViewCount(BizUtils.formatCountNumber(topic.getViewCount()));
topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount())); topic.setFormatDisscussCount(BizUtils.formatCountNumber(topic.getDisscussCount()));
} }
Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getRank)); Map<TopicRankQo, Double> map = topicRankQos.stream().collect(Collectors.toMap(o -> o, TopicRankQo::getScore));
List<TopicRankQo> rankList = map.entrySet().stream() List<TopicRankQo> rankList = map.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())) .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.map(Map.Entry::getKey) .map(Map.Entry::getKey)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment