I am using Sphinx Search on a website to keep track of various table entries. These entries contain a date component and are less relevant after their data has been passed.
How can I have Sphinx assign more weight to results that match the search criteria (e.g. keyword search) and then assign less weight to those results that have a date in the past but still meet the search criteria? The illustration below should help to convey my needs:
Table containing data to be searched:
--------- ------------------ ---------
entry_id | title | date |
--------- ------------------ ---------
1 Tennis Racquet 12-10-2010
2 Basketball 03-24-2011
3 Tennis 03-03-2012
4 Skydiving 09-16-2012
5 Fishing 11-27-2012
6 Tennis Court 02-09-2013
Search Term:
tennis
Search Results:
- Entry 3 – Tennis
- Entry 6 – Tennis Court
- Entry 1 – Tennis Racquet – This is returned last because the date of this entry is before today’s date (2-9-2012), but it still matches the search term
I have found a way to do this using a variation of @mobius’
SetSortModeexample.Instead of using
SPH_SORT_EXTENDED, I usedSPH_SORT_EXPRalong with the expression below:This allows me to weigh upcoming results higher, then future results slightly lower, and finally old results last.
The large number in the numerator is to correspond with the number of decimal places in the UNIX timestamp.