2017-07-26 44 views
0

我有一個獅身人面像索引表與ID,數據和數據類型。如何使用Group By和Group_Concat進行Sphinxsearch?

| 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

$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' . '*'); 

我預期的結果是什麼我的PHP代碼像這樣,

| objectid |  types   | 
|----------------|--------------------| 
|  10  | title, description | 

但不幸的是它拋出一個錯誤:

THROWS ERR: can not aggregate non-scalar attribute 'type' 

,我讀了多值屬性可能是問題的一個可能的解決方案,但我沒有真正明白這是怎麼工作的。

+0

什麼是'datatype'?我沒有看到你的表中有任何數據類型。 – akond

+0

它的一個錯字 - 在我真正的索引表中該列被命名爲數據類型。編輯它 – steedz

回答

0

獅身人面像不允許您聚合非標量字段(例如整數)。我假設type字段是用於某種參考價值的。你可以使用另外一個表,說type_dictionary

1 title 
2 desctiption 
3 date 

,並在主表中使用其索引:

sphinxid ... type 
1   1 
2   3 
3   2 

在這種情況下GROUP_CONCAT會工作。

+0

非常感謝,這似乎工作 – steedz