厚德博学 敬业乐群
@sky
全部博文(252)
2015年(2)
2014年(1)
2013年(1)
2012年(16)
2011年(42)
2010年(67)
2009年(87)
2008年(36)
25742040
shijiulo
niuxlinu
ebayboy
hayand66
大鬼不动
acer1025
醉鬼的故
小雅贝贝
XINGCHEN
wzy_yzw
十的9次
zds05
bjywxc
zlhc1
smile124
cynthia
格伯纳
分类:
2009-08-05 14:49:31
import org.apache.lucene.document.Document; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.document.Field; import org.apache.lucene.search.Query; import org.apache.lucene.search.Hits; import java.io.FileReader; import java.io.BufferedReader; public class TestSearch { public static void main(String[] args) throws Exception { IndexWriter writer = new IndexWriter("/root/index", new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); Document doc = new Document(); FileReader reader = new FileReader("/root/store/email.txt"); Field field = new Field("content", reader); Field field2 = new Field("filename", "/root/store/email.txt", Field.Store.YES, Field.Index.TOKENIZED); doc.add(field); doc.add(field2); writer.addDocument(doc); writer.close(); IndexSearcher searcher = new IndexSearcher("/root/index"); QueryParser parser = new QueryParser("content", new StandardAnalyzer()); Query query = parser.parse("haoyuan"); Hits hits = searcher.search(query); for (int i = 0; i < hits.length(); i++) { Document ret = hits.doc(i); String str = ret.get("filename"); FileReader freader = new FileReader(str); BufferedReader in = new BufferedReader(freader); String line; while ((line = in.readLine()) != null) System.out.println(line); in.close(); } } }
上一篇:java 索引分词
下一篇:lucene 排序查找结果
登录 注册