tags:

views:

1352

answers:

3

Hi all,

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

A: 

Use a FunctionQuery. See the FAQ

Mauricio Scheffer
As far as I can tell, you can't do datefield arithmetic with functionquery functions. The ord function works out the ordinal of the result in the db but in our case the date a record was added may not have any relevance to the actual startTime of the result.
Mechanic
True, you can't do date math, but you don't need to. The rord function applied to your startTime field will apply a boost based on the order of this *date field*, not based on the order of insertion.
Mauricio Scheffer
Yes ord and rord will allow you to order by date, however, assume I have a list of records with startTimes ranging from 1990-2020 and I want to boost records with startTimes close to the current year (2009). This isn't possible unless I can do date math as far as I can tell.
Mechanic
Sorry, you're right, I didn't think about documents with future dates!
Mauricio Scheffer
A: 

You can do date math in Solr 1.4.

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

Sean Timm
+3  A: 

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.

Chris