lucene3.6.1 经典案例 入门教程

原文链接:http://liqita.iteye.com/blog/1676664

第一步:下载lucene的核心包

lucene-core-3.6.1-javadoc.jar (3.5 MB) 

lucene-core-3.6.1.jar (1.5 MB) 

拷贝到项目的lib 文件夹里

 

第二步:

在C盘下建立source文件夹   (C:\source)

source文件夹存放待索引的文件,例如,建立两个文件,名称为 test1.txt  test2.txt  。

test1.txt文件内容为:欢迎来到绝对秋香的博客。

test2.txt文件内容为:绝对秋香引领你走向潮流。

在C盘下再建立index文件夹,存放索引文件 (C:\index)

 

第三步,建立索引类 TextFileIndexer ,并运行主函数

Java代码   收藏代码
  1. package com.newtouchone.lucene;  
  2. import java.io.BufferedReader;  
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStreamReader;  
  7. import java.util.Date;  
  8.   
  9. import org.apache.lucene.analysis.Analyzer;  
  10. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  11. import org.apache.lucene.document.Document;  
  12. import org.apache.lucene.document.Field;  
  13. import org.apache.lucene.index.IndexWriter;  
  14. import org.apache.lucene.index.IndexWriterConfig;  
  15. import org.apache.lucene.index.IndexWriterConfig.OpenMode;  
  16. import org.apache.lucene.store.Directory;  
  17. import org.apache.lucene.store.FSDirectory;  
  18. import org.apache.lucene.util.Version;  
  19.     
  20. public class TextFileIndexer {    
  21.     public static void main(String[] args) throws Exception {    
  22.         /* 指明要索引文件夹的位置,这里是C盘的source文件夹下 */    
  23.         File fileDir = new File("C:\\source");    
  24.     
  25.         /* 这里放索引文件的位置 */    
  26.         File indexDir = new File("C:\\index");    
  27.         Directory dir = FSDirectory.open(indexDir);  
  28.         Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);  
  29.         IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,luceneAnalyzer);  
  30.         iwc.setOpenMode(OpenMode.CREATE);  
  31.         IndexWriter indexWriter = new IndexWriter(dir,iwc);    
  32.         File[] textFiles = fileDir.listFiles();    
  33.         long startTime = new Date().getTime();    
  34.             
  35.         //增加document到索引去    
  36.         for (int i = 0; i < textFiles.length; i++) {    
  37.             if (textFiles[i].isFile()    
  38.                     && textFiles[i].getName().endsWith(".txt")) {    
  39.                 System.out.println("File " + textFiles[i].getCanonicalPath()    
  40.                         + "正在被索引....");    
  41.                 String temp = FileReaderAll(textFiles[i].getCanonicalPath(),    
  42.                         "GBK");    
  43.                 System.out.println(temp);    
  44.                 Document document = new Document();    
  45.                 Field FieldPath = new Field("path", textFiles[i].getPath(),    
  46.                         Field.Store.YES, Field.Index.NO);    
  47.                 Field FieldBody = new Field("body", temp, Field.Store.YES,    
  48.                         Field.Index.ANALYZED,    
  49.                         Field.TermVector.WITH_POSITIONS_OFFSETS);    
  50.                 document.add(FieldPath);    
  51.                 document.add(FieldBody);    
  52.                 indexWriter.addDocument(document);    
  53.             }    
  54.         }    
  55.         indexWriter.close();    
  56.             
  57.         //测试一下索引的时间    
  58.         long endTime = new Date().getTime();    
  59.         System.out    
  60.                 .println("这花费了"    
  61.                         + (endTime - startTime)    
  62.                         + " 毫秒来把文档增加到索引里面去!"    
  63.                         + fileDir.getPath());    
  64.     }    
  65.     
  66.     public static String FileReaderAll(String FileName, String charset)    
  67.             throws IOException {    
  68.         BufferedReader reader = new BufferedReader(new InputStreamReader(    
  69.                 new FileInputStream(FileName), charset));    
  70.         String line = new String();    
  71.         String temp = new String();    
  72.             
  73.         while ((line = reader.readLine()) != null) {    
  74.             temp += line;    
  75.         }    
  76.         reader.close();    
  77.         return temp;    
  78.     }    
  79. }    

 输出结果为:

Java代码   收藏代码
  1. File C:\source\test1.txt正在被索引....  
  2. 欢迎来到绝对秋香的博客。  
  3. File C:\source\test2.txt正在被索引....  
  4. 绝对秋香引领你走向潮流。  
  5. 这花费了641 毫秒来把文档增加到索引里面去!C:\source  
 

第四步,建立测试类TestQuery,并运行主函数,输出测试结果

Java代码   收藏代码
  1. package com.newtouchone.lucene;  
  2. import java.io.File;  
  3. import java.io.IOException;    
  4.   
  5. import org.apache.lucene.analysis.Analyzer;    
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;    
  7. import org.apache.lucene.index.IndexReader;  
  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.FSDirectory;  
  15. import org.apache.lucene.util.Version;  
  16.     
  17. public class TestQuery {    
  18.     public static void main(String[] args) throws IOException, ParseException {    
  19.         String index = "C:\\index";         //搜索的索引路径  
  20.         IndexReader reader = IndexReader.open(FSDirectory.open(new File(index)));  
  21.         IndexSearcher searcher = new IndexSearcher(reader);    
  22.           
  23.         ScoreDoc[] hits = null;    
  24.         String queryString = "绝对秋香";   //搜索的关键词  
  25.         Query query = null;    
  26.           
  27.     
  28.         Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);    
  29.         try {    
  30.             QueryParser qp = new QueryParser(Version.LUCENE_36,"body", analyzer);    
  31.             query = qp.parse(queryString);    
  32.         } catch (ParseException e) {    
  33.         }    
  34.         if (searcher != null) {    
  35.             TopDocs results = searcher.search(query,10);    //返回最多为10条记录  
  36.             hits = results.scoreDocs;  
  37.             if (hits.length > 0) {    
  38.                 System.out.println("找到:" + hits.length + " 个结果!");    
  39.             }    
  40.             searcher.close();  
  41.         }   
  42.          
  43.     }    
  44.     
  45. }    

 测试输出结果为:

Java代码   收藏代码
  1. 找到:2 个结果!  

附件homework.rar为项目文件,解压部署则可运行该lucene案例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值