2

lucene5(4)修改指定索引的搜索权重

 2 years ago
source link: https://wakzz.cn/2017/10/01/lucene5/(4)%E4%BF%AE%E6%94%B9%E6%8C%87%E5%AE%9A%E7%B4%A2%E5%BC%95%E7%9A%84%E6%90%9C%E7%B4%A2%E6%9D%83%E9%87%8D/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

lucene5(4)修改指定索引的搜索权重

祈雨的博客
2017-10-01

lucene可以对指定索引的字段修改搜索权重,使权重大的索引优先显示

@Test
public void TestBoot() throws Exception {
IndexWriter writer = getIndexWriter(indexDir);
Document document = new Document();
// 添加一个文档信息,相当于一个数据库表字段
Field nameField = new TextField("name", "hello world", Field.Store.YES);
// 权值默认1,该值越大排名越前
nameField.setBoost(1.5F);
document.add(nameField);
document.add(new TextField("describes", "测试luence", Field.Store.YES));
writer.addDocument(document);
writer.close();
}

@Test
public void TestSearch() throws Exception {
IndexReader reader = getIndexReader();
IndexSearcher searcher = new IndexSearcher(reader);
// 指定Document的某个属性
QueryParser parser = new QueryParser("name", getAnalyzer());
// 指定索引内容,对应某个分词
Query query = parser.parse("hello");
TopDocs hits = searcher.search(query, 10);
for (ScoreDoc sd : hits.scoreDocs) {
Document doc = searcher.doc(sd.doc);
//显示搜索结果的评分(评分=权重*搜索匹配评分)
System.out.println("name:" + doc.get("name") + ",评分:"+sd.score);
}
reader.close();
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK