lucene检索实例

来源:互联网 发布:大型java web项目源码 编辑:程序博客网 时间:2024/06/08 14:13
public class MySearcher {public static final String STORE_PATH = "E:/lucene_index";public static void searcher(String keyword) throws ParseException,IOException {long startTime = System.currentTimeMillis();Directory dir = FSDirectory.open(new File(STORE_PATH));IndexReader reader = IndexReader.open(dir);IndexSearcher search = new IndexSearcher(reader);Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);QueryParser parse = new QueryParser(Version.LUCENE_35, "content", analyzer);Query query = parse.parse(keyword);System.out.println("解析后的语法:"+query.toString());TopDocs it = search.search(query, 100);System.out.println("search : " + query.toString());ScoreDoc[] docs = it.scoreDocs;for (int i = 0; i < docs.length; i++) {System.out.println(i + "\t" + search.doc(docs[i].doc).get("title")+"\t"+docs.toString());}long endTime = System.currentTimeMillis();System.out.println("total time: " + (endTime - startTime) + " ms");}public static void main(String[] args) throws IOException, ParseException {searcher("lucene");}}

0 0
原创粉丝点击