/*
* 通过改变boost的值来改变文档的得分
* 在这个例子中Doc3的得分是最少的,
* 加入我想把doc3的得分变高,把他放到网页的第一条,
* 那么就是用doc3.setBoost(2f);方法就可以实现
* */
package score;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TermQuery;
public class ChangeBoost{
public ChangeBoost(String INDEX_STORE_PATH)
{
try{
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
//创建3个文档
Document doc1 = new Document();
Document doc2 = new Document();
Document doc3 = new Document();
Field f1 = new Field("bookname", "ab bc", Field.Store.YES, Index.TOKENIZED);
Field f2 = new Field("bookname", "ab bc cd", Field.Store.YES, Index.TOKENIZED);
Field f3 = new Field("bookname", "ab bc de ff gg ss", Field.Store.YES, Index.TOKENIZED);
doc1.add(f1);
doc2.add(f2);
doc3.add(f3);
writer.addDocument(doc1);
writer.addDocument(doc2);
doc3.setBoost(2f);
writer.addDocument(doc3);
writer.close();
IndexSearcher searcher = new IndexSearcher(INDEX_STORE_PATH);
TermQuery q = new TermQuery(new Term("bookname", "bc"));
Hits hits = searcher.search(q);
for(int i = 0; i < hits.length(); i++){
Document doc = hits.doc(i);
//与之匹配的文档
System.out.println(doc.get("bookname") + "\t\t");
//文档所得分数
System.out.println(hits.score(i));
//这里通过解释的方法,来了解这个分数是怎么回事
System.out.println(searcher.explain(q, hits.id(i)).toString());
}
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ChangeBoost cb = new ChangeBoost("E:\\Lucene项目\\索引文件");
}
}
改变前的截图:
加上处理后截图: