开发者

Randomize search result in sphinx

Hi im using sphinx 201 beta (r2792)

i have rt index with rt_attr_uint = rake my goal is to sort results by this attribute whitch work as expected us开发者_如何学Going

$this->sphinx->SetSortMode(SPH_SORT_EXTENDED, 'rake DESC')

but i want to also randomize this result ... lets say i have this data

(id,  title,  rake)
  1, 'test',  3
  2, 'test2', 3
  3, 'ásdf',  1
  4, 'rreer', 0

so after i append random

$this->sphinx->SetSortMode(SPH_SORT_EXTENDED, 'rake DESC, @random')

i would expect randomize result sorted by attribute rake descending but im getting error:

sort-by attribute '@random' not found

whitch i dont understand because this random attribute is internal attribute am i doing something wrong? where could be a problem? thanks


What I did is create the random number on the SQL query

SELECT ...  RAND() as random_sort, ...

Added it as an attribute

sql_attr_float = random_sort

And then use it in the extended sort query

$this->sphinx->SetSortMode(SPH_SORT_EXTENDED, 'rake DESC, random_sort DESC');


From my quick tests, it looks like Sphinx only supports @random if it is the only attribute in the sort clause. So I'm afraid you're out of luck - either you just sort randomly, or you just sort by rake.


For real time randomness, index the ids and sort using expr mode with an expression like this (not sure this one is 100% random):

( ( ( id % #{pagesize} + #{rand(pagesize * 2)} ) % #{pagesize} ) / #{pagesize} )

Add an If statement to the expression above to use your rake attribute.

[http://sphinxsearch.com/docs/1.10/sorting-modes.html][1]

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜