我用PrefixQuery計算分數時遇到問題。爲了改變每個文檔的分數,當將文檔添加到索引中時,我使用了setBoost來改變文檔的提升。然後我創建PrefixQuery進行搜索,但結果並未根據提升進行更改。看來setBoost完全不適用於PrefixQuery。請檢查下面我的代碼:Lucene:使用前綴查詢計算分數
@Test
public void testNormsDocBoost() throws Exception {
Directory dir = new RAMDirectory();
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_CURRENT), true,
IndexWriter.MaxFieldLength.LIMITED);
Document doc1 = new Document();
Field f1 = new Field("contents", "common1", Field.Store.YES, Field.Index.ANALYZED);
doc1.add(f1);
doc1.setBoost(100);
writer.addDocument(doc1);
Document doc2 = new Document();
Field f2 = new Field("contents", "common2", Field.Store.YES, Field.Index.ANALYZED);
doc2.add(f2);
doc2.setBoost(200);
writer.addDocument(doc2);
Document doc3 = new Document();
Field f3 = new Field("contents", "common3", Field.Store.YES, Field.Index.ANALYZED);
doc3.add(f3);
doc3.setBoost(300);
writer.addDocument(doc3);
writer.close();
IndexReader reader = IndexReader.open(dir);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(new PrefixQuery(new Term("contents", "common")), 10);
for (ScoreDoc doc : docs.scoreDocs) {
System.out.println("docid : " + doc.doc + " score : " + doc.score + " "
+ searcher.doc(doc.doc).get("contents"));
}
}
輸出是:
docid : 0 score : 1.0 common1
docid : 1 score : 1.0 common2
docid : 2 score : 1.0 common3
請注意,當在字段級別使用setBoost時,這似乎也適用。即PrefixQuery將會忽略字段提升,除非您按照此處所述更改rewrite方法。 – 2011-06-02 17:09:03
這幫了我,請標記爲答案。 – fommil 2013-07-24 12:52:24