2016-05-16 58 views
2

我想创建一个lucene自定义得分函数,它将存储在文档中的值添加到最终得分中。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); 
} 
} 

回答

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

如果我明白你正在尝试做的,那么你需要打开一个文件(你的“文件”),并解析一个浮点数。

这里解释如何打开一个文件,并得到它的内容以几种不同的方式: How to open a txt file and read numbers in java

请注意,你需要的不是Float.parseFloat(String s)Integer.parseInt(String s)