SphinxQL:如何通过自定义字段对 GROUP BY 或 RANK 结果进行 SUM() 自定义字段
SphinxQL: How to SUM() custom field with GROUP BY or RANK results by custom field
我有一个具有下一个结构的索引:
+----+-----------+---------+--------------+
| id | entity_id | content | weight_field |
+----+-----------+---------+--------------+
| 1 | 1 | car | 1.2 |
+----+-----------+---------+--------------+
| 2 | 1 | desert | 1.45 |
+----+-----------+---------+--------------+
| 3 | 1 | water | 1.55 |
+----+-----------+---------+--------------+
| 4 | 2 | water | 1.1 |
+----+-----------+---------+--------------+
| 5 | 2 | desert | 1.9 |
+----+-----------+---------+--------------+
有人可以告诉我是否可以 SUM()
字段值分组吗?
我试过这个
SELECT SUM(weight_field) AS sort, entity_id FROM test_index WHERE MATCH ('@content car|desert|water') GROUP BY entity_id ORDER BY sort DESC
但出现错误:
syntax error, unexpected '(', expecting $end near '()'
我希望得到下一个结果:
+------+-----------+
| sort | entity_id |
+------+-----------+
| 4.2 | 1 |
+------+-----------+
| 3.0 | 2 |
+------+-----------+
第二种方式适合我:
使用自定义 weight_field
(包含 1.563
、1.02
等浮点值)对结果进行排名。但我不确定是否可以使用
OPTION ranker=...
我相信
SELECT SUM(WEIGHT()) AS sort,...
应该可以。其中 WEIGHT()
是当前排序器计算的值。
所以,我终于找到了原因:
在实际代码中,不是在示例中,我将 weight_field
命名为 weight
。因此 sphinx 将其识别为预定义的 FUNCTION WEIGHT()
并抛出错误,表明它希望在 weight
.
之后看到 ()
修复它并重新编制索引后它就可以工作了。
我有一个具有下一个结构的索引:
+----+-----------+---------+--------------+
| id | entity_id | content | weight_field |
+----+-----------+---------+--------------+
| 1 | 1 | car | 1.2 |
+----+-----------+---------+--------------+
| 2 | 1 | desert | 1.45 |
+----+-----------+---------+--------------+
| 3 | 1 | water | 1.55 |
+----+-----------+---------+--------------+
| 4 | 2 | water | 1.1 |
+----+-----------+---------+--------------+
| 5 | 2 | desert | 1.9 |
+----+-----------+---------+--------------+
有人可以告诉我是否可以 SUM()
字段值分组吗?
我试过这个
SELECT SUM(weight_field) AS sort, entity_id FROM test_index WHERE MATCH ('@content car|desert|water') GROUP BY entity_id ORDER BY sort DESC
但出现错误:
syntax error, unexpected '(', expecting $end near '()'
我希望得到下一个结果:
+------+-----------+
| sort | entity_id |
+------+-----------+
| 4.2 | 1 |
+------+-----------+
| 3.0 | 2 |
+------+-----------+
第二种方式适合我:
使用自定义 weight_field
(包含 1.563
、1.02
等浮点值)对结果进行排名。但我不确定是否可以使用
OPTION ranker=...
我相信
SELECT SUM(WEIGHT()) AS sort,...
应该可以。其中 WEIGHT()
是当前排序器计算的值。
所以,我终于找到了原因:
在实际代码中,不是在示例中,我将 weight_field
命名为 weight
。因此 sphinx 将其识别为预定义的 FUNCTION WEIGHT()
并抛出错误,表明它希望在 weight
.
()
修复它并重新编制索引后它就可以工作了。