lucene搜索
1、创建Directory;
2、创建IndexReader
3、根据IndexReader创建IndexSearcher
4、创建搜索的Query
5、根据Searcher搜索并且返回TopDocs
6、根据TopDocs获取ScoreDoc对象
7、根据Searcher和ScoreDoc对象获取具体的Document对象
8、根据Document对象获取需要的值
Directory directory=FSDirectory.open(new File("D:/eclipse-jee-mars-1-win32-x86_64/lucenefile/index01")); try { IndexReader reader=IndexReader.open(directory); IndexSearcher indexSearcher=new IndexSearcher(reader); QueryParser parser=new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35)); Query query=parser.parse("java"); TopDocs topDocs=indexSearcher.search(query, 10); ScoreDoc[] scoreDocs=topDocs.scoreDocs; for(ScoreDoc scoreDoc:scoreDocs){ Document document=indexSearcher.doc(scoreDoc.doc); System.out.println(document.get("filename")+" "+document.get("path")+"]"); } reader.close();
}
posted on 2015-12-01 15:02 waveletbobo 阅读(131) 评论(1) 编辑 收藏 举报