Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the best lucene setup for ranking exact matches as the highest

Which analyzers should be used for indexing and for searching when I want an exact match to rank higher then a "partial" match? Possibly set up custom scoring in a Similarity class?

For example, when my index consist of car parts, car, and car shop (indexed with StandardAnalyzer on lucene 3.5), a query for "car" results in:

  • car parts
  • car
  • car shop

(basically returned in the order in which they were added, since they all get the same score).

What I would like to see is car ranked first, then the other results (doesn't really matter which order, I assume the analyzer can influence that).

like image 331
NoMoreMrCodeGuy Avatar asked Jan 09 '12 09:01

NoMoreMrCodeGuy


1 Answers

All three matches are exact (term car being matched, not 'ca' or 'ar') :)

If there's no more content in these fields ("car parts", "car" and "car shop"), then you could use lengthNorm() or computeNorm() (depending on Lucene version), to give shorter fields more weight so that car gets higher score for being shorter. In Lucene 3.3.0, DefaultSimilarity.computeNorm() looks like this:

return state.getBoost() * ((float) (1.0 / Math.sqrt(numTerms)));

where numTerms is the total number of terms in the field. So it's surprising "car" and "car shop" documents have the same score, because for "car" the norm is 1 and for "car shop" it should be 0.7 (assuming boost of 1).

like image 103
milan Avatar answered Oct 06 '22 23:10

milan