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

荒凉一梦 提交于 2020-01-01 10:05:07

问题


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).


回答1:


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).




回答2:


Quick hack: after getting the ScoreDoc[] from IndexSearcher.search, re-sort it with score as the first criterion and length (ascending) as the second.



来源:https://stackoverflow.com/questions/8786343/what-is-the-best-lucene-setup-for-ranking-exact-matches-as-the-highest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!