我有一个包含 id、数据和数据类型的 Sphinx-Index-Table。
| sphinxid | objectid | data | type |
|----------------|----------------|----------------|-------------|
| 1 | 10 | Lorem ipsum | title |
|----------------|----------------|----------------|-------------|
| 2 | 10 | dolor si ipsum | description |
|----------------|----------------|----------------|-------------|
| 3 | 20 | dolor sit amet | date |
现在我尝试运行类似于此工作 mysql 查询的搜索查询:
SELECT objectid, GROUP_CONCAT(type) AS types
FROM SphinxIndex
WHERE data LIKE '%ipsum%'
GROUP BY objectid
这是我使用 sphinxapi.php 的 php 代码
$cl = new SphinxClient();
$cl->SetServer("localhost", 9312);
$cl->SetMatchMode(SPH_MATCH_EXTENDED);
$cl->SetLimits(0, 1000);
$cl->SetRankingMode(SPH_RANK_SPH04);
// THROWS ERR: can not aggregate non-scalar attribute 'datatype'
$cl->SetSelect("objectid, GROUP_CONCAT(type) AS types");
//----------------------------------------------
$cl->SetGroupBy('objectid', SPH_GROUPBY_ATTR, '@count DESC');
$cl->SetSortMode(SPH_SORT_EXTENDED, "@count DESC");
$searchresults = $cl->Query('*' . 'ipsum' . '*');
我的预期结果是这样的,
| objectid | types |
|----------------|--------------------|
| 10 | title, description |
但不幸的是它抛出了一个错误:
THROWS ERR: can not aggregate non-scalar attribute 'type'
我读到多值归因可能是解决该问题的一个可能的解决方案,但我并没有真正理解它是如何工作的。
最佳答案
Sphinx 不允许您聚合非标量的字段(例如整数)。我假设 type
字段用于某种引用值。您可以使用另一个表,例如 type_dictionary
:
1 title
2 desctiption
3 date
并在主表中使用其索引:
sphinxid ... type
1 1
2 3
3 2
在这种情况下,GROUP_CONCAT
可以工作。
关于php - 如何使用 Group By 和 Group_Concat 进行 Sphinx 搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45328404/