Search code examples
sphinxsphinxql

Sphinx: Show all results order by previous searches


I use SphinxQL for searching and filtering in product database and I store last x search phrases of each user. I wonder if is it possible to show all products (all rows) to every user but with relevance on previous search.

Let's say one user sought for mobile phones (iphone, galaxy s7...), ie. electronics category. I want to show him all products randomly, but products from category electronics more often and products with those searched keywords even more often.

Is it even possible with Sphinx?

Thanks and sorry for english.


Solution

  • An alternative, would be perhaps to create random numbers attached to each result. A high and a low number, with an overlapping range.

    sql_query = SELECT id, RAND()*100 AS rand_low, (RAND()*100)+50 AS rand_high, ...
    sql_attr_uint = rand_low
    sql_attr_uint = rand_high
    

    Can then arrange the ranking expression to pick either of these numbers depending on if matches or not, and sort by the result.

    SELECT id FROM index WHERE MATCH('_all_ MAYBE electronics MAYBE (galaxy s7)') 
    OPTION ranker=expr('IF(doc_word_count>1,rand_high,rand_low)');
    

    Will be mixed up. But results that match one of the words, have a greater chance of showing up first (because use the weighted random number) - its still only a chance, because a rand_high CAN still be smaller than rand_low. ... can change the size of the number 'overlap' to tweak the mix of matching/non matching results.

    (added as a new answer as its a quite differnt idea, although uses the same 'all' keyword)