基于评分和其他值的搜索结果排序
Search results ordering based on rating and other values
我正在努力为搜索结果页面构建一个复杂的排序算法。
我想按评分(评分计数、平均评分)订购商品,但我只希望评分占结果页面的 60-80%。一页有 12 个项目。它们应该随机分布在页面上。
我想应用简单排序作为次要条件,例如 created_at
字段。
有人知道怎么做吗?
我对您的要求的解读是:
- 共退回 12 件商品
- 4 个项目应该是最近创建的项目
- 剩下的 8 个应该是评分最高的项目
- 项目不应出现两次,因此如果项目是最近创建的并且评价很高,我们将需要一个额外的项目
为了实现这一点,我尝试了以下方法:
- 将有序行号分配给 created_at 和 avg_rating 列
- 计算前 4 个已创建项和前 8 个项的项数(我们将其称为 num_duplicates)
- 将要返回的高评价项目总数增加 num_duplicates
select *
from
(
select a.*,
sum(
/* We want the total number of items that meet both criteria */
/* For every one of these items, we want to include an extra row */
case when created_row_num <= 4 and rating_row_num <= 8
then 1
else 0
end ) over() as num_duplicates
from (
select ratings.*,
row_number() over( order by created_at desc ) as created_row_num,
row_number() over( order by avg_rating desc ) as rating_row_num
from ratings
) as a
) as b
where created_row_num <= 4
/* Get top 8 by rating, plus 1 for every record that has already been selected by creation date */
or rating_row_num <= 8 + num_duplicates
我最终使用了一种解决方案,其中包括未评级项目最终位于评级项目中间的机会。算法思路如下:
ORDER BY
CASE WHEN rating IS NOT NULL OR RANDOM() < 0.0x THEN 1 + RANDOM()ELSE RANDOM() END
DESC NULLS LAST
我正在努力为搜索结果页面构建一个复杂的排序算法。
我想按评分(评分计数、平均评分)订购商品,但我只希望评分占结果页面的 60-80%。一页有 12 个项目。它们应该随机分布在页面上。
我想应用简单排序作为次要条件,例如 created_at
字段。
有人知道怎么做吗?
我对您的要求的解读是:
- 共退回 12 件商品
- 4 个项目应该是最近创建的项目
- 剩下的 8 个应该是评分最高的项目
- 项目不应出现两次,因此如果项目是最近创建的并且评价很高,我们将需要一个额外的项目
为了实现这一点,我尝试了以下方法:
- 将有序行号分配给 created_at 和 avg_rating 列
- 计算前 4 个已创建项和前 8 个项的项数(我们将其称为 num_duplicates)
- 将要返回的高评价项目总数增加 num_duplicates
select *
from
(
select a.*,
sum(
/* We want the total number of items that meet both criteria */
/* For every one of these items, we want to include an extra row */
case when created_row_num <= 4 and rating_row_num <= 8
then 1
else 0
end ) over() as num_duplicates
from (
select ratings.*,
row_number() over( order by created_at desc ) as created_row_num,
row_number() over( order by avg_rating desc ) as rating_row_num
from ratings
) as a
) as b
where created_row_num <= 4
/* Get top 8 by rating, plus 1 for every record that has already been selected by creation date */
or rating_row_num <= 8 + num_duplicates
我最终使用了一种解决方案,其中包括未评级项目最终位于评级项目中间的机会。算法思路如下:
ORDER BY
CASE WHEN rating IS NOT NULL OR RANDOM() < 0.0x THEN 1 + RANDOM()ELSE RANDOM() END
DESC NULLS LAST