默认情况下,Lucene 仅根据 TEXT-RELEVANCE 进行排序。有很多因素会影响相关性得分。
tf-idf 值和长度标准化可能会影响您的分数,导致“a b”/“b c”文档显示在排名靠前的结果中,而不是包含“a b c”的文档。
您可以克服上述问题的方法是根据匹配查询词的数量来提高相关性得分。您可以按照以下步骤操作。
1) 编写一个从DefaultSimilarity 扩展的自定义相似性类。如果您想知道什么是相似度,它是 Lucene 使用的类,其中包含对分数有贡献的所有评分因素公式。
教程:Lucene Scoring
2) 覆盖DefaultSimilarity.coord()
Lucene 文档中的 coord() 解释。
coord(q,d) is a score factor based on how many of the query terms are found in the specified document. Typically, a document that contains more of the query's terms will receive a higher score than another document with fewer query terms. This is a search time factor computed in coord(q,d) by the Similarity in effect at search time.
3) coord 的默认实现是overlap/maxoverlap。您可以尝试不同的公式,以便包含更多查询词的文档显示在顶部结果中。以下公式可能是很好的起点。
1) coord return value = Math.sqrt(overlap/maxoverlap)
2) coord return value = overlap;
4) 您不必重写其他方法,因为 DefaultSimilarity 具有所有评分因素的默认实现。只需触摸您要试验的那个,在您的情况下是 coord() 。如果你从Similarity 扩展,你必须提供所有的实现。
5) 可以使用 IndexSearcher.setSimilarity() 将相似度传递给 IndexSearcher