Lucene入门实例
2013-08-02 10:07
309 查看
package cn.lucene.helloworld; import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter.MaxFieldLength; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.Filter; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TopDocs; import org.junit.Test; import cn.lucene.utils.LuceneUtil; public class HelloWorld { String filePath = "F:\\workspace\\Lucene\\resource\\resource.txt"; String indexPath = "F:\\workspace\\Lucene\\indexPath"; Analyzer analyzer = new StandardAnalyzer(); @Test //建立索引 public void createIndex() throws Exception{ IndexWriter indexWriter = new IndexWriter(indexPath, analyzer,false, MaxFieldLength.LIMITED); indexWriter.addDocument(LuceneUtil.fileToDocument(new File(filePath))); indexWriter.close(); } @Test //根据关键字查询索引库 public void query() throws Exception{ String queryString = "document"; IndexSearcher indexSearcher = new IndexSearcher(indexPath); String[] fields = {"name","content"}; QueryParser parser = new MultiFieldQueryParser(fields, analyzer); Query query = parser.parse(queryString); Filter filter = null; TopDocs topDocs = indexSearcher.search(query, filter, 1000); for(ScoreDoc scoreDoc : topDocs.scoreDocs){ int index = scoreDoc.doc; Document document = indexSearcher.doc(index); LuceneUtil.printDocument(document); } } }
工具类:LuceneUtil.java
package cn.lucene.utils; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.Field.Index; import org.apache.lucene.document.Field.Store; public class LuceneUtil { public static Document fileToDocument(File file){ Document document = new Document(); document.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED)); document.add(new Field("content", getFileContent(file), Store.YES, Index.ANALYZED)); return document; } public static String getFileContent(File file) { StringBuffer sb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String str = null; while((str = br.readLine()) != null){ sb.append(str); } } catch (Exception e) { throw new RuntimeException(e); } return sb.toString(); } public static void printDocument(Document document){ System.out.println("-------------start-----------------"); System.out.println("name:" + document.get("name")); System.out.println("content:" + document.get("content")); System.out.println("-------------end-----------------"); } }
相关文章推荐
- lucene3.6入门实例
- lucene 入门实例
- Lucene入门实例
- JAVA:lucene 入门学习,简单实例模访google搜索
- Lucene入门以及简单实例
- Lucene3.0.0 入门实例
- lucene 搜索入门实例
- lucene入门实例
- lucene3.6.1 经典案例 入门实例
- Lucene入门项目实例---比较Lucene方式搜索与传统String方式搜索的效率
- lucene入门实例
- lucene4.0入门实例
- lucene入门 ==》 实例
- Lucene入门实例
- lucene介绍及入门实例
- Lucene入门实例。
- lucene&IKAnalyzer入门实例
- lucene3.6入门实例
- Lucene框架入门实例
- lucene 入门实例