2016-05-15 44 views
1

我正在研究索引中索引和搜索推文的系統,每個推文都有一個定義其社會重要性(社會價值)的字段,我想將此值添加到相似性分數中,這樣我就可以通過將他們的社交價值和他們的分數結合到查詢中來排列文檔。Lucene評分函數

例如,我的打分函數會像

Final Score = QueryScore + Social score (which is a float that I already calculated) 

所以,我怎麼能做到這一點?

我使用Lucene-5.5.0

package Lucene; 


import java.nio.file.Path; 
import java.nio.file.Paths; 
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.index.DirectoryReader; 
import org.apache.lucene.search.IndexSearcher; 
import org.apache.lucene.search.Query; 
import org.apache.lucene.queryparser.classic.QueryParser; 
import org.apache.lucene.search.ScoreDoc; 
import org.apache.lucene.store.Directory; 
import org.apache.lucene.store.FSDirectory; 

public class SearchFiles { 

    @SuppressWarnings("deprecation") 
    public static void main(String[] args){ 
     try{ 

      Path path = Paths.get("C:\\Users\\JUGURTHA\\Desktop\\boulot\\index"); 
      Directory dir = FSDirectory.open(path); 
      DirectoryReader ireader = DirectoryReader.open(dir); 
      IndexSearcher isearcher = new IndexSearcher(ireader); 
      StandardAnalyzer analyzer = new StandardAnalyzer(); 
      //get each token    
      QueryParser parser = new QueryParser("text", analyzer); 
      Query query = parser.parse("Love"); 
      ScoreDoc[] hits = isearcher.search(query, null, 20).scoreDocs; 
      for (int i = 0; i < hits.length; i++){ 
       Document hitDoc = isearcher.doc(hits[i].doc); 
       System.out.println("Tweet " + i + " : " + hitDoc.get("text")); 
       System.out.println("created_at: " + hitDoc.get("date")); 
       System.out.println("id: " + hitDoc.get("id")); 
       System.out.println(); 
       System.out.println(); 

      } 


     } catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 
+0

我已經結果解決了這個問題,我使用了類CustomScoreProvider –

回答

0

我想通了如何完成工作,我用的是類CustomScoreProvider,並得到了我想要

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) { 
     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); 
} 
}