【发布时间】:2016-05-15 17:22:09
【问题描述】:
我正在开发一个索引和搜索索引中的推文的系统,每条推文都有一个定义其社会重要性(社会价值)的字段,我想将此值添加到相似度分数中,以便我可以对文档进行排名通过结合他们的社会价值和他们对查询的分数。
例如,我的评分函数会是这样的
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();
}
}
}
【问题讨论】:
-
我已经解决了问题,我使用了 CustomScoreProvider 类