现在的位置: 首页 > 综合 > 正文

Apache Lucence 使用的一个简单例子

2013年06月09日 ⁄ 综合 ⁄ 共 5759字 ⁄ 字号 评论关闭

Apache Lucence 使用的一个简单例子 

 
//在C盘下创建一个s的文件夹,然后在s文件夹中新建三个文件:1.txt,3.txt,2.txt,,1.txt中输入"想 飞的我",2.txt中输入"想飞",3.txt中输入"我".接下来我们来看下面的测试:

Java代码
 
package test;   
  1.   
  2. import java.io.File;   
  3. import java.io.FileReader;   
  4. import java.io.IOException;   
  5. import java.io.Reader;   
  6. import java.util.Date;   
  7.   
  8. import org.apache.lucene.analysis.Analyzer;   
  9. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  10. import org.apache.lucene.document.Document;   
  11. import org.apache.lucene.document.Field;   
  12. import org.apache.lucene.index.CorruptIndexException;   
  13. import org.apache.lucene.index.IndexWriter;   
  14. import org.apache.lucene.store.LockObtainFailedException;   
  15. //需要的apache下的lucene包   
  16. /**  
  17. * 创建索引文件  
  18. */  
  19. public class CreateIndex {   
  20. public static boolean createDocumentIndex()    
  21. {   
  22.    boolean bool = false;   
  23.    try{   
  24.    //需要索引的目录   
  25.    File dirpath = new File("c:\\s");   
  26.    //将索引文件存放在index目录下   
  27.    File indexpath = new File("c:\\index");   
  28.    //创建分词器   
  29.    Analyzer analyzer = new StandardAnalyzer();   
  30.    IndexWriter index = new IndexWriter(indexpath,analyzer,true);   
  31.      
  32.    File[] txtfiles = dirpath.listFiles();   
  33.      
  34.    long starttime = new Date().getTime();   
  35.      
  36.    for(int i=0;i<txtfiles.length;i++)   
  37.    {   
  38.     if(txtfiles[i].isFile())   
  39.     {   
  40.      System.out.println("文件"+txtfiles[i].getCanonicalPath()+"正在索引中...");   
  41.      Reader read = new FileReader(txtfiles[i]);   
  42.      Document doc = new Document();   
  43.      doc.add(new Field("content",read));   
  44.      doc.add(new Field("path",txtfiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.NO));   
  45.      index.addDocument(doc);   
  46.     }   
  47.    }   
  48.    index.optimize();   
  49.    index.close();   
  50.         long endTime = new Date().getTime();      
  51.         System.out      
  52.                 .println("这花费了"    
  53.                         + (endTime - starttime)      
  54.                         + " 毫秒来把文档增加到索引里面去!"    
  55.                         + dirpath.getPath());      
  56.    bool=true;   
  57.    }   
  58.    catch(Exception e)   
  59.    {   
  60.     bool = false;   
  61.    }   
  62.    return bool;   
  63. }   
  64. /**  
  65. * 测试  
  66. */  
  67. public static void main(String[] a)   
  68. {   
  69. CreateIndex.createDocumentIndex();   
  70. }   
  71. }   
  72.   
  73. 测试结果:   
  74.   
  75. 文件C:\s\1.txt正在索引中...   
  76. 文件C:\s\2.txt正在索引中...   
  77. 文件C:\s\3.txt正在索引中...   
  78. 这花费了141 毫秒来把文档增加到索引里面去!c:\index   
  79.   
  80.   
  81. package test;   
  82.   
  83. import java.io.IOException;   
  84.   
  85. import org.apache.lucene.analysis.Analyzer;   
  86. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  87. import org.apache.lucene.queryParser.ParseException;   
  88. import org.apache.lucene.queryParser.QueryParser;   
  89. import org.apache.lucene.search.Hits;   
  90. import org.apache.lucene.search.IndexSearcher;   
  91. import org.apache.lucene.search.Query;   
  92. /**  
  93. * 从索引文件查询内容  
  94. * @author Jan  
  95. *  
  96. */  
  97. public class QueryString    
  98. {   
  99. public static void main(String[] args) throws IOException, ParseException {   
      
  100.          Hits hits = null;      
  101.          String queryString = "我";      
  102.          Query query = null;      
  103.          IndexSearcher searcher = new IndexSearcher("c:\\index");      
  104.       
  105.          Analyzer analyzer = new StandardAnalyzer();      
  106.          try {      
  107.              QueryParser qp = new QueryParser("content", analyzer);      
  108.              query = qp.parse(queryString);      
  109.          } catch (ParseException e) {      
  110.          }      
  111.          if (searcher != null) {      
  112.              hits = searcher.search(query);      
  113.              if (hits.length() > 0) {      
  114.                  System.out.println("找到:" + hits.length() + " 个结果!");      
  115.              }      
  116.          }      
  117.      }      
  118. }  

测试结果:

找到:2 个结果!

抱歉!评论已关闭.