IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("F:\\temp")));// 打开索引
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_40);
String[] fields = { "questionname","id" };
Occur[] occurs = new Occur[] { Occur.SHOULD,Occur.SHOULD };
Query query = MultiFieldQueryParser.parse(Version.LUCENE_40, "测试 的", fields,
occurs, analyzer);
TopDocs result = searcher.search(query, searcher.getIndexReader()
.maxDoc());
ScoreDoc[] hits = result.scoreDocs;
List<Document> list = new ArrayList<Document>();
for (int i = 0; i <hits.length; i++) {
Document doc = searcher.doc(hits[i].doc);
list.add(doc);
}
System.out.println("搜索list的长度\t→→→→\t"+list.size());
for (Document document : list) {
System.out.println(document.getField("questionname"));
}
analyzer.close();
注:红色字体是输入的检索条件,多个用空格隔开,找到的结果先匹配同时符合多个的结果,结果只是拿过来的document一个list集合,具体结果再解析就行了。
结果如图:
posted on 2013-08-12 17:26
魏文甫 阅读(1167)
评论(1) 编辑 收藏