SphinxQL:如何根据自定义字段使用GROUP BY或RANK结果SUM()自定义字段

时间:2016-05-16 09:42:02

标签: php sum aggregate sphinx sphinxql

我有一个下一个结构的索引:

+----+-----------+---------+--------------+
| 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.5631.02等)来对结果进行排名。但我不确定是否可以使用

OPTION ranker=...

2 个答案:

答案 0 :(得分:1)

所以,我终于找到了原因:

在实际代码中,不在示例中,我将weight_field命名为weight。因此,sphinx将其识别为预定义的FUNCTION WEIGHT()并抛出错误,该错误表明它希望在()之后看到weight

修好它并重新编制索引后就可以了。

答案 1 :(得分:0)

我相信

SELECT SUM(WEIGHT()) AS sort,...

应该有效。 WEIGHT()的位置是当前排名者计算的值。

相关问题