要实现自定义评分,想把我们认为应该排在前面成为top,lucenen给我们留了一个扩展类就是CustomScoreQuery
首先。我们创建评分的时候要先定义一个我们自己要改变的评分域
FieldScoreQuery fieldScoreQuery=new FieldScoreQuery("score", Type.INT);//设置评分域为socreTopDocs docs=searcher.search(new CustomScoreQuery(query, fieldScoreQuery){
@Override
protected CustomScoreProvider getCustomScoreProvider(
IndexReader reader) throws IOException {
CustomScoreProvider customScoreProvider=new CustomScoreProvider(reader){
@Override
public float customScore(int doc, float arg1,
float[] arg2) throws IOException {
// TODO Auto-generated method stub
return super.customScore(arg0, arg1, arg2);
}
};
return customScoreProvider;
}
}, 500);@Override
protected CustomScoreProvider getCustomScoreProvider(IndexReader reader)
throws IOException {
CustomScoreProvider customScoreProvider=new CustomScoreProvider(reader){
String [] filenames=FieldCache.DEFAULT.getStrings(reader, "filename");
@Override
public float customScore(int doc, float sub, float vs)//默认打分。评分域打分
throws IOException {
float score=sub;
String name=filenames[doc];
if(name.endsWith(".ini")||name.endsWith(".txt")){
return score*2f;
}
return score;
}
};
return customScoreProvider;
}ok,自定义评分就是这么简单
转载请注明http://blog.csdn.net/a837199685/article/
原文地址:http://blog.csdn.net/a837199685/article/details/43451975