Ehsan
Ehsan

Reputation: 1682

Custom sorting for lucene

I have document with fields like (title, content, datetime) I want to sort the results with the following formula

1) title boosts 2.5

2) content boost 1.5

3) IMPORTANT (boost those documents that is newer means datetime field is near today date) boost 3

how can I write a query considering the above criteria what should I do for #3

any help would be greatly appreciate.

Upvotes: 0

Views: 2349

Answers (3)

Madhav
Madhav

Reputation: 751

If you are looking for Custom Sorting based on your own definition then you can look at below example. But it will only help you define your sort on an individual field. You can later add multiple sorts to your query.

Not entirely sure if that helps

https://github.com/smadha/lucene-sorting-example/blob/master/CustomSorter.java

Upvotes: 0

rjarmstrong
rjarmstrong

Reputation: 1231

You can use a function query to boost the score for documents containing each of the text fields i.e. Title and Content (both ranked by date). Then after this multiplying the recency boost by your weightings given above.

http://wiki.apache.org/solr/SolrRelevancyFAQ#How_can_I_boost_the_score_of_newer_documents

{!boost b=product(recip(ms(NOW,datetime),3.16e-11,1,1),2.5)}Title:<query> 
{!boost b=product(recip(ms(NOW,Created),3.16e-11,1,1),1.5)}Content:<query>

You can't use a sort as the ordering of the secondary and tertiary sorts will be meaningless unless of course the precision of your dates is sufficiently low.

Upvotes: 0

bajafresh4life
bajafresh4life

Reputation: 12883

+title:foo^2.5 +content:bar^1.5 datetime:20100721^3

Obviously, fill in appropriate values for the datetime field. The key here is that the datetime term is not a required term; it only functions increase the score for documents that match the term. You can add another datetime term for yesterday's date, and another for the day before, and so on, while decreasing the boost as you get farther away from today's date.

Upvotes: 1

Related Questions