# 关于Discoure热门排序

**URL:** <https://www.isharkfly.com/t/discoure/16154>\
**Category:** Discourse\
**Tags:** discourse\
**Created:** [August 16, 2024, 11:00am UTC](https://www.isharkfly.com/t/discoure/16154 "2024-08-16T11:00:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![VegaMonika](https://www.isharkfly.com/user_avatar/www.isharkfly.com/vegamonika/32/14424_2.png) [@VegaMonika](https://www.isharkfly.com/u/VegaMonika)\
**Post date:** [August 16, 2024, 11:00am UTC](https://www.isharkfly.com/t/discoure/16154/1 "2024-08-16T11:00:57Z")

</div>

我发现 Discoure 热门排序 不是按照  
 ![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/7/724d0eb2fce8aeb56e88ce05f2e57e3284590752.png)

显示数量感觉是乱排序的

 ![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/0/02729ff07c3fd6185031e4267e109ed38f190600.png)

我有的154的都在121的下面

不知道是按照什么来排序热门的

最新的排序

有的时候是按照最新发布的时间

有的时候回复帖子也算新发布的时间排序

我不得不去官网找到了一个

按照时间排序的组件

![image](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/b/b361f3124ca1a5fd99d13c40f3c7bc03cecd61b9.png)

> **[Created Topic Sort](https://meta.discourse.org/t/created-topic-sort/321521/2)**
>
> Wow, another perfect plugin that changes the forum! It would be great if it could integrate with the Discourse Nav Item Icon / Emoji plugin.

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [August 16, 2024, 11:09am UTC](https://www.isharkfly.com/t/discoure/16154/2 "2024-08-16T11:09:51Z")

</div>

> [@VegaMonika](#):
>
> 显示数量感觉是乱排序的

Discourse 的热门排序不是按照查看数量排序的。

Discourse 的热门排序有算法。

根据官方的说法：[Calculating "Top" topics in Discourse - Using Discourse - Discourse Meta](https://meta.discourse.org/t/calculating-top-topics-in-discourse/240928)

会根据几个因素去计算得出一个值，然后按照这个值去排序。

当然你可以调整权重，当权重调整后显示结果就不一样了。

 ![2024-08-16_07-07-20](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/2X/d/d9c0036c25893a4433ac175bd842d670fe00e66d.png)

上面还给了一个比较复杂的 SQL。

```auto
-- [params]
-- date :start_date = 26 apr 2020
-- date :end_date = 2 may 2020
-- double :log_views_multiplier = 2.0
-- double :first_post_likes_multiplier = 0.5
-- double :least_likes_per_post_multiplier = 3.0

WITH likes AS (
SELECT topic_id, SUM(like_count) AS count
FROM posts
WHERE created_at::date >= :start_date::date 
  AND created_at::date < :end_date::date
  AND deleted_at IS NULL
  AND NOT hidden
  AND post_type = 1
GROUP BY topic_id
),
op_likes AS (
SELECT topic_id, like_count AS count
FROM posts
WHERE created_at::date >= :start_date::date 
  AND created_at::date < :end_date::date
  AND post_number = 1
  AND deleted_at IS NULL
  AND NOT hidden
  AND post_type = 1
),
posts AS (
SELECT topic_id, GREATEST(COUNT(*), 1) AS count
FROM posts
WHERE created_at::date >= :start_date::date 
  AND created_at::date < :end_date::date
  AND deleted_at IS NULL
  AND NOT hidden
  AND post_type = 1
  AND user_id <> 0
GROUP BY topic_id
),
views AS (
SELECT topic_id, COUNT(*) AS count
FROM topic_views
WHERE viewed_at::date >= :start_date::date 
  AND viewed_at::date < :end_date::date
GROUP BY topic_id
),
category_definition_topic_ids AS (
  SELECT COALESCE(topic_id, 0) AS id FROM categories
),
top_topics AS(
SELECT 
topics.id AS topic_id,
topics.title,
topics.user_id, 
posts.count AS date_range_posts,
views.count AS date_range_views,
topics.views AS all_time_views,
topics.bumped_at,
(CASE
 WHEN topics.created_at::date < :start_date::date 
  AND topics.created_at::date >= :end_date::date
 THEN 0
 ELSE log(GREATEST(views.count, 1)) * :log_views_multiplier +
    op_likes.count * :first_post_likes_multiplier +
    CASE WHEN likes.count > 0 AND posts.count > 0
       THEN
        LEAST(likes.count / posts.count, :least_likes_per_post_multiplier)
       ELSE 0
    END +
    CASE WHEN topics.posts_count < 10 THEN
       0 - ((10 - topics.posts_count) / 20) * op_likes.count
    ELSE
       10
    END +
    log(GREATEST(posts.count, 1))
 END) AS score
FROM posts
INNER JOIN views ON posts.topic_id = views.topic_id
INNER JOIN likes ON posts.topic_id = likes.topic_id
INNER JOIN op_likes ON posts.topic_id = op_likes.topic_id
LEFT JOIN topics ON topics.id = posts.topic_id AND topics.deleted_at IS NULL
WHERE topics.deleted_at IS NULL
  AND topics.visible
  AND topics.archetype <> 'private_message'
  AND NOT topics.archived
  AND topics.id NOT IN (SELECT id FROM category_definition_topic_ids)
ORDER BY
  score DESC,
  topics.bumped_at DESC
)

SELECT * FROM top_topics WHERE score > 0

```

没有怎么仔细研究这个算法，应该还有点复杂。

对站点管理员来说，大致知道几个权重的配置应该就行了。
