Sphinx 排序:nil/NULL 值最后
在 Rails 项目中,我将 Sphinx 与 Thinking Sphinx 插件一起使用。 我用一个属性 :foo 为一个浮点数索引了一个表。
在对 :foo 列进行排序时,我期望的行为是 nil 值总是出现在列表的末尾,例如,
id; foo (order foo desc)
-------
1; 5
2; 3
3; -4
4: -5
5: nil
6: nil
id; foo (order foo asc)
-------
4: -5
3; -4
2; 3
1; 5
5: nil
6: nil
如果是普通的 sql,我会这样排序:
:order => "(foo IS NULL) ASC, foo DESC"
但这似乎不可能,因为我认为 NULL 值是转换为 0(这是真的吗?)。 使用 sphinx 排序表达式似乎无法正确对我的浮动进行排序。
有人解决了这个问题或者有关于如何做到这一点的想法吗?
On a Rails project, I'm using Sphinx together with Thinking Sphinx plugin. I index a table with an attribute :foo that is a float.
My desired behaviour when sorting for column :foo would be that nil values always appear at the end of the list, e.g.
id; foo (order foo desc)
-------
1; 5
2; 3
3; -4
4: -5
5: nil
6: nil
id; foo (order foo asc)
-------
4: -5
3; -4
2; 3
1; 5
5: nil
6: nil
If it was normal sql, I'd sort like:
:order => "(foo IS NULL) ASC, foo DESC"
But it seems not to be possible, as I think NULL values are translated to 0 (is that true?). Using sphinx ordering expressions seems not to sort my floats properly.
Did anybody solve this problem or has an idea on how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您提供的解决方案就是我建议的 - 是的,您是对的,Sphinx 将 NULL 视为 0。
The solution you've provided is what I would suggest - as yes, you're correct, Sphinx treats NULLs as 0's.
与此同时,我想出的一个解决方案是索引一个额外的属性,如下所示:
是什么让我这样排序 这
有点笨拙,特别是当我有很多属性想要这样排序时。
One solution I came up with in the meantime is to index an extra attribute, like this:
what lets me order like this
It's a bit clumsy, especially as I have many attributes that I want to have ordered like this.