本文主要是介绍通过改变boost值来改变文档的得分源码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在进行相关度排序的时候,如果想加某个文档的相关度,使其在搜索解雇中排名更加靠前的位置上,则使用boost。
代码:
package change;
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.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 static void main(String[] args) {
// TODO 自动生成的方法存根
try {
IndexWriter writer = new IndexWriter("E:\\Lucene项目\\index", new StandardAnalyzer(), true);
writer.setUseCompoundFile(false);
Document doc1= new Document();
Document doc2= new Document();
Document doc3= new Document();
Field f1 = new Field("bookname", "bc", Field.Store.YES, Field.Index.TOKENIZED);
Field f2 = new Field("bookname", "ab bc cd", Field.Store.YES, Field.Index.TOKENIZED);
Field f3 = new Field("bookname", "ab bc cd de ef", Field.Store.YES, Field.Index.TOKENIZED);
doc1.add(f1);
doc2.add(f2);
doc3.add(f3);
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
IndexSearcher searcher = new IndexSearcher("E:\\Lucene项目");
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.print(doc.get("bookname") + "\t\t");
System.out.println(hits.score(i));
System.out.println(searcher.explain(q, hits.id(i)).toString());
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
这篇关于通过改变boost值来改变文档的得分源码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!