Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SOLR - Boost function (bf) to increase score of documents whose date is closest to NOW

Tags:

solr

lucene

I have a solr instance containing documents which have a 'startTime' field ranging from last month to a year from now. I'd like to add a boost query/function to boost the scores of documents whose startTime field is close to the current time.

So far I have seen a lot of examples which use rord to add boosts to documents whom are newer but I have never seen an example of something like this.

Can anyone tell me how to do it please?

Thanks

like image 813
Mechanic Avatar asked Sep 28 '09 13:09

Mechanic


2 Answers

If you're on Solr 1.4+, then you have access to the "ms" function in function queries, and the standard, textbook approach to boosting by recency is:

recip(ms(NOW,startTime),3.16e-11,1,1)

ms gives the number of milliseconds between its two arguments. The expression as a whole boosts scores by 1 for docs dated now, by 1/2 for docs dated 1 year ago, by 1/3 for docs dated 2 years ago, etc.. (See http://wiki.apache.org/solr/FunctionQuery#Date_Boosting, as Sean Timm pointed out.)

In your case you have docs dated in the future, and those will get assigned a negative score by the above function, so you probably would want to throw in an absolute value, like this:

recip(abs(ms(NOW,startTime)),3.16e-11,1,1)

abs(ms(NOW,startTime)) will give the # of milliseconds between startTime and now, guaranteed to be nonnegative.

That would be a good starting place. If you want, you can then tweak the 3.16e-11 if it's too agressive or not agressive enough.

Tangentially, the ms function will only work on fields based on the TrieDate class, not the classic Date and LegacyDate classes. If your schema.xml was based on the example one for Solr 1.4, then your date field is probably already in the correct format.

like image 106
Chris Avatar answered Nov 12 '22 08:11

Chris


You can do date math in Solr 1.4.

http://wiki.apache.org/solr/FunctionQuery#Date_Boosting

like image 23
Sean Timm Avatar answered Nov 12 '22 06:11

Sean Timm