Search code examples
phpsphinx

How to sort results in sphinx/php?


I have priority column in my index. I want to get autosuggestions by priority DESC but its not working

      $cl->SetMatchMode(SPH_MATCH_EXTENDED2);
      $cl->SetSortMode(SPH_SORT_EXTENDED, 'priority DESC');
      $cl->SetLimits(0, 10);
      $cl->SetGroupBy('alias_seq', SPH_GROUPBY_ATTR);
      $partialQueryStr = " @keyword ^$query*";
      $cl->AddQuery($partialQueryStr, 'autosuggest');
      $result = $cl->RunQueries();

Sphinx index

source autosuggest {
//..other fields/attrs
sql_attr_uint = priority
}

Solution

  • Notice you also have GROUP BY. In a GROUP BY query, the SetSortMode sets the within GROUP order (ie which row from the group is returned) - NOT the final resultset.

    ... you need the third param ($groupsort) to SetGroupBy http://sphinxsearch.com/docs/current.html#api-func-setgroupby

    Note that it's $groupsort that affects the order of matches in the final result set. Sorting mode (see Section 9.3.3, “SetSortMode”) affect the ordering of matches within group, ie. what match will be selected as the best one from the group. So you can for instance order the groups by matches count and select the most relevant match within each group at the same time.

    eg:

    $cl->SetGroupBy('alias_seq', SPH_GROUPBY_ATTR, 'priority DESC');  
    

    (note it only accepts a extended sort param, so your existing sort can be duplicated)