lucene3.0

来源:互联网 发布:阿里云上传速度慢 编辑:程序博客网 时间:2024/05/16 19:54

做了两个简单的例子
创建索引

Java代码 复制代码 收藏代码
  1. package com.langhua;   
  2.   
  3. import java.io.File;   
  4. import java.io.FileReader;   
  5. import java.io.IOException;   
  6. import java.util.Date;   
  7.   
  8. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  9. import org.apache.lucene.document.DateTools;   
  10. import org.apache.lucene.document.Document;   
  11. import org.apache.lucene.document.Field;   
  12. import org.apache.lucene.index.IndexWriter;   
  13. import org.apache.lucene.store.Directory;   
  14. import org.apache.lucene.store.SimpleFSDirectory;   
  15. import org.apache.lucene.util.Version;   
  16. /**  
  17.  * 创建索引 Lucene 3.0+  
  18.  * @author Administrator  
  19.  *  
  20.  */  
  21. public class Indexer {   
  22.   
  23.     /**  
  24.      * @param args  
  25.      * @throws IOException   
  26.      */  
  27.     public static void main(String[] args) throws IOException {   
  28.         //保存索引文件的地方   
  29.         String indexDir = "F://indexDir";   
  30.         //将要搜索TXT文件的地方   
  31.         String dateDir = "F://dateDir";   
  32.         IndexWriter indexWriter = null;   
  33.         //创建Directory对象   
  34.         Directory dir = new SimpleFSDirectory(new File(indexDir));   
  35.         //创建IndexWriter对象,第一个参数是Directory,第二个是分词器,第三个表示是否是创建,如果为false为在此基础上面修改,第四表示表示分词的最大值,比如说new MaxFieldLength(2),就表示两个字一分,一般用IndexWriter.MaxFieldLength.LIMITED    
  36.         indexWriter = new IndexWriter(dir,new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);   
  37.         File[] files = new File(dateDir).listFiles();   
  38.         for (int i = 0; i < files.length; i++) {   
  39.             Document doc = new Document();   
  40.             //创建Field对象,并放入doc对象中   
  41.             doc.add(new Field("contents"new FileReader(files[i])));    
  42.             doc.add(new Field("filename", files[i].getName(),    
  43.                                 Field.Store.YES, Field.Index.NOT_ANALYZED));   
  44.             doc.add(new Field("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NOT_ANALYZED));   
  45.             //写入IndexWriter   
  46.             indexWriter.addDocument(doc);   
  47.         }   
  48.         //查看IndexWriter里面有多少个索引   
  49.         System.out.println("numDocs"+indexWriter.numDocs());   
  50.         indexWriter.close();   
  51.            
  52.     }   
  53.   
  54. }  



搜索索引 Lucene 3.0+

Java代码 复制代码 收藏代码
  1. package com.langhua;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5.   
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  7. import org.apache.lucene.document.Document;   
  8. import org.apache.lucene.queryParser.ParseException;   
  9. import org.apache.lucene.queryParser.QueryParser;   
  10. import org.apache.lucene.search.IndexSearcher;   
  11. import org.apache.lucene.search.Query;   
  12. import org.apache.lucene.search.ScoreDoc;   
  13. import org.apache.lucene.search.TopDocs;   
  14. import org.apache.lucene.store.Directory;   
  15. import org.apache.lucene.store.SimpleFSDirectory;   
  16. import org.apache.lucene.util.Version;   
  17. /**  
  18.  * 搜索索引 Lucene 3.0+  
  19.  * @author Administrator  
  20.  *  
  21.  */  
  22. public class Searcher {   
  23.   
  24.     public static void main(String[] args) throws IOException, ParseException {   
  25.         //保存索引文件的地方   
  26.         String indexDir = "F://indexDir";   
  27.         Directory dir = new SimpleFSDirectory(new File(indexDir));   
  28.         //创建 IndexSearcher对象,相比IndexWriter对象,这个参数就要提供一个索引的目录就行了   
  29.         IndexSearcher indexSearch = new IndexSearcher(dir);   
  30.         //创建QueryParser对象,第一个参数表示Lucene的版本,第二个表示搜索Field的字段,第三个表示搜索使用分词器   
  31.         QueryParser queryParser = new QueryParser(Version.LUCENE_30,   
  32.                 "contents"new StandardAnalyzer(Version.LUCENE_30));   
  33.         //生成Query对象   
  34.         Query query = queryParser.parse("langhua9527");   
  35.         //搜索结果 TopDocs里面有scoreDocs[]数组,里面保存着索引值   
  36.         TopDocs hits = indexSearch.search(query, 10);   
  37.         //hits.totalHits表示一共搜到多少个   
  38.         System.out.println("找到了"+hits.totalHits+"个");   
  39.         //循环hits.scoreDocs数据,并使用indexSearch.doc方法把Document还原,再拿出对应的字段的值   
  40.         for (int i = 0; i < hits.scoreDocs.length; i++) {   
  41.             ScoreDoc sdoc = hits.scoreDocs[i];   
  42.             Document doc = indexSearch.doc(sdoc.doc);   
  43.             System.out.println(doc.get("filename"));               
  44.         }          
  45.         indexSearch.close();   
  46.     }   
  47. }  
原创粉丝点击