solr修改評分機制實現排序

破棉襖發表於2015-05-08

solr要修改打分機制怎麼改?
大多數回答:Similarity是個不錯的方案.
但是具體怎麼弄很少有明確的描述,官方wiki也只是說可以自定義,具體如何做,沒有例子.

首先,solr4.0本身提供了多種評分方法:

org.apache.solr.search.similarities.BM25SimilarityFactory
org.apache.solr.search.similarities.DefaultSimilarityFactory
org.apache.solr.search.similarities.DFRSimilarityFactory
org.apache.solr.search.similarities.IBSimilarityFactory
org.apache.solr.search.similarities.LMDirichletSimilarityFactory
org.apache.solr.search.similarities.LMJelinekMercerSimilarityFactory
org.apache.solr.search.similarities.SchemaSimilarityFactory

每一個有什麼不同不在今天討論範圍內,說下怎麼配置.

schema.xml






bm25_test欄位就會按照BM25SimilarityFactory機制評分.那麼如何進一步自定義呢,不廢話看程式碼:

package org.nlp.lucene.search.similarities;

 

import org.apache.lucene.search.similarities.Similarity;

import org.apache.solr.schema.SimilarityFactory;

 

public class NlpSimilarityFactory extends SimilarityFactory {

public Similarity getSimilarity() {

return new NlpSimilarity();

}

}

package org.nlp.lucene.search.similarities;

import org.apache.lucene.search.similarities.DefaultSimilarity;

public class NlpSimilarity extends DefaultSimilarity {
@Override  //idf值全部為1.0f
public float idf(long docFreq, long numDocs) {
return 1.0F;
}

@Override //tf值全部為1.0f
public float tf(float freq) {
return 1.0F;
}

@Override
public String toString() {
return “nlpSimilarity”;
}

}

SimilarityFactory照葫蘆畫瓢就是,NlpSimilarity可以重寫哪些方法看一下DefaultSimilarity就好,當然BM25SimilarityFactory這些都是可以搞的,自己看著辦吧,哥只能幫你到這裡了.

什麼,怎麼確定評分的修改是否生效?

查詢的時候加上引數debug=true就好.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29754888/viewspace-1629741/,如需轉載,請註明出處,否則將追究法律責任。

相關文章