Lucene检索索引


package com;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

public class Search {
public static void main(String[] args) throws Exception {
String fieldName = "discript";
String keyword = "人民";
/*Query IkQuery = searchIKQuery(fieldName, keyword);
executeQuery(fieldName, keyword,IkQuery);*/
BooleanQuery booleanQuery = searchBooleanQuery(keyword);
executeQuery(fieldName, booleanQuery);
}

public static BooleanQuery searchBooleanQuery( String keyword) throws Exception {
BooleanQuery bq = new BooleanQuery();

Analyzer analyzer = new IKAnalyzer();
String[] fn = {"name"};
// 增加权重
Map<String, Float> filedmap = new HashMap();
filedmap.put("name", 50f);

MultiFieldQueryParser multField = new MultiFieldQueryParser(Version.LUCENE_30, fn, analyzer, filedmap);
Query muquery = multField.parse(keyword);

Query ikquery = IKQueryParser.parseMultiField(fn,keyword);

Term term1 = new Term("discript",keyword);
TermQuery tq = new TermQuery(term1);
bq.add(tq,BooleanClause.Occur.MUST);
bq.add(muquery, BooleanClause.Occur.SHOULD);
//bq.add(ikquery, BooleanClause.Occur.MUST);
System.out.println(bq);
return bq;
}

public static Query searchIKQuery(String fieldName, String keyword) throws IOException{
// 使用IKQueryParser查询分析器构造Query对象
Query query = IKQueryParser.parse(fieldName, keyword);

return query;
}
public static IndexSearcher createSeacher() throws Exception {
IndexSearcher isearcher = null;
Directory directory = null;
File file = new File("C:/commFile/lucence");
directory = FSDirectory.open(file);
// 实例化搜索器
isearcher = new IndexSearcher(directory);

// 在索引器中使用IKSimilarity相似度评估器
isearcher.setSimilarity(new IKSimilarity());
if (directory != null) {
directory.close();
}
return isearcher;
}

public static void executeQuery(String fieldName,Query query)throws Exception {
// 实例化IKAnalyzer分词器
Analyzer analyzer = new IKAnalyzer();
IndexSearcher isearcher = createSeacher();
try {
Sort sort = new Sort();
// 按字段的排序(一个或多个)
SortField[] f = { new SortField("name", SortField.STRING, false) };
sort.setSort(f);
// 搜索相似度最高的10条记录
//TopDocs topDocs = isearcher.search(query, null, 10, sort);
TopDocs topDocs = isearcher.search(query, 10);

// 关键字高亮显示
Formatter formatter = new SimpleHTMLFormatter("<font color=\"red\">", "</font>"); // 前缀和后缀
Scorer scorer = new QueryScorer(query);
Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.setTextFragmenter(new SimpleFragmenter(200)); // 字长度
System.out.println("命中文章的篇数:" + topDocs.totalHits);
// 输出结果
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (int i = 0; i < topDocs.totalHits; i++) {
int docId =scoreDocs[i].doc;
Document targetDoc = isearcher.doc(docId);
System.out.println("内容:" + targetDoc.get(fieldName)+"===="+targetDoc.get("name"));//输出相应字段的内容
String hupName = highlighter.getBestFragment(analyzer,fieldName,targetDoc.get(fieldName));
//System.out.println(hupName);
}

} catch (Exception e) {
e.printStackTrace();
} finally {
if (analyzer != null) {
analyzer.close();
}
if (isearcher != null) {
isearcher.close();
}

}

}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值