我想制作一个lucene自定义评分函数,该函数获取存储在文档中的值并将其添加到最终得分中。
我已经想出了如何在评分函数中添加一个值,但我无法将文档的存储值放入方法中。
class CustomizedScoreProvider extends CustomScoreProvider {
public CustomizedScoreProvider(LeafReaderContext reader) {
super(reader);
// TODO Auto-generated constructor stub
}
public float customScore(int doc, float subQueryScore,float valSrcScores[]){
try {
subQueryScore+=4; \\ I only added this for testing ,
} catch(Exception e) { \\ I want to add a value that is stored in a field of a document
e.printStackTrace();
}
return subQueryScore;
}
}
class CustomizedScoreQuery extends CustomScoreQuery{
public CustomizedScoreQuery(Query subQuery,IndexReader ireader) {
super(subQuery);
// TODO Auto-generated constructor stub
}
public CustomizedScoreProvider getCustomScoreProvider (LeafReaderContext reader){
CustomizedScoreProvider i=new CustomizedScoreProvider(reader);
return (i);
}
}
答案 0 :(得分:2)
谢谢,但我已经解决了这个问题,我搜索了该文件的索引器,然后我提取了我想要使用的字段的值。
class CustomizedScoreProvider extends CustomScoreProvider {
private LeafReaderContext context;
public CustomizedScoreProvider(LeafReaderContext reader) {
super(reader);
this.context= reader;
// TODO Auto-generated constructor stub
}
public float customScore(int doc, float subQueryScore,float valSrcScores[]) throws IOException{
Document Social=context.reader().document(doc);
IndexableField i= Social.getField("soc");// the field I wanted to extract
float k= (float)i.numericValue();
subQueryScore+=k;
return subQueryScore;
}
}
答案 1 :(得分:-1)
如果我理解你要做什么,那么你需要打开一个文件(你的"文档")并解析一个浮动。
这里解释了如何打开文件并以几种不同的方式获取它的内容: How to open a txt file and read numbers in java
请注意,您需要Float.parseFloat(String s)
而不是Integer.parseInt(String s)
。