如何使用 Group By 和 Group_Concat 进行 Sphinxsearch?

How to Sphinxsearch with Group By and Group_Concat?

我有一个带有 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

这是我的 php 代码,使用 sphinxapi.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 字段具有某种参考价值。你可以使用另一个 table,比如 type_dictionary:

1 title
2 desctiption
3 date

并在您的主 table:

中使用它的索引
sphinxid ... type
1            1
2            3
3            2

在这种情况下 GROUP_CONCAT 可以。