2016-05-16 6 views
2

ドキュメントに格納された値をとって最終スコアに追加するルーメンカスタムスコアリング関数を作成したいと思います。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

あなたがしようとしていることを理解していれば、ファイル( "ドキュメント")を開いてフロートを解析する必要があります。

ここでは、ファイルを開いて、それはいくつかの異なる方法での内容です取得する方法を説明しますあなたがFloat.parseFloat(String s)代わりのInteger.parseInt(String s)が必要になります How to open a txt file and read numbers in java

注意。