所以我有一个类似于https://stackoverflow.com/a/18462040/1768337的查询。正如您所看到的,行已根据喜欢排序。假设我使用上面链接中的sql语句创建索引。有没有办法输出这个sql语句给出的确切内容?也就是说,维护sql语句中的排序集?我还尝试按如下方式对行进行排名,
source popular
{
type = mysql
sql_host = --------
sql_user = --------
sql_pass = --------
sql_db = --------
sql_port = 3306 # optional, default is 3306
sql_query = \
SET @rank=0; \
SELECT *, @rank := @rank + 1 AS rank \
FROM \
( \
SELECT p.id AS id, p.search AS search, COUNT(CASE WHEN li.date > DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN li.id END) AS daily_likes, COUNT(CASE WHEN li.date > DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN li.id END) AS weekly_likes, COUNT(li.id) AS total_likes \
FROM `photo` p \
JOIN `like` li \
ON p.id = li.photo_id \
WHERE \
li.date > DATE_SUB(CURDATE(), INTERVAL 30 DAY) \
GROUP BY \
p.id \
) AS dt \
ORDER BY \
case when daily_likes > 3 then daily_likes else 0 end desc, \
case when weekly_likes > 5 then weekly_likes else 0 end desc, \
total_likes DESC \
LIMIT 30
sql_attr_uint = daily_likes
sql_attr_uint = weekly_likes
sql_attr_uint = total_likes
sql_attr_uint = rank
sql_query_info = SELECT * FROM photo WHERE id=$id
}
我试过这个以后可以使用像$s->SetSortMode(SPH_SORT_EXTENDED, '@relevance DESC, rank ASC');
这样的东西。但是,似乎自定义变量在sphinx中不起作用。
所以我的问题是索引一个sql语句并使用sphinx(php api)输出它 - 甚至维护sql排序。
答案 0 :(得分:0)
好的,感谢barryhunter(救世主)帮助解决这个问题。正如他在评论中提到的SET
应该在sql_query_pre
。所以我的conf
文件看起来像:
sql_query_pre = SET @rank=0
sql_query = \
SELECT *, @rank := @rank + 1 AS rank \
FROM \
( \