Apache Lucence 使用的一个简单例子

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. }  
package test;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
//需要的apache下的lucene包
/**
* 创建索引文件
*/
public class CreateIndex {
public static boolean createDocumentIndex() 
{
   boolean bool = false;
   try{
   //需要索引的目录
   File dirpath = new File("c:\\s");
   //将索引文件存放在index目录下
   File indexpath = new File("c:\\index");
   //创建分词器
   Analyzer analyzer = new StandardAnalyzer();
   IndexWriter index = new IndexWriter(indexpath,analyzer,true);
  
   File[] txtfiles = dirpath.listFiles();
  
   long starttime = new Date().getTime();
  
   for(int i=0;i<txtfiles.length;i++)
   {
    if(txtfiles[i].isFile())
    {
     System.out.println("文件"+txtfiles[i].getCanonicalPath()+"正在索引中...");
     Reader read = new FileReader(txtfiles[i]);
     Document doc = new Document();
     doc.add(new Field("content",read));
     doc.add(new Field("path",txtfiles[i].getAbsolutePath(),Field.Store.YES,Field.Index.NO));
     index.addDocument(doc);
    }
   }
   index.optimize();
   index.close();
        long endTime = new Date().getTime();   
        System.out   
                .println("这花费了" 
                        + (endTime - starttime)   
                        + " 毫秒来把文档增加到索引里面去!" 
                        + dirpath.getPath());   
   bool=true;
   }
   catch(Exception e)
   {
    bool = false;
   }
   return bool;
}
/**
* 测试
*/
public static void main(String[] a)
{
CreateIndex.createDocumentIndex();
}
}

测试结果:

文件C:\s\1.txt正在索引中...
文件C:\s\2.txt正在索引中...
文件C:\s\3.txt正在索引中...
这花费了141 毫秒来把文档增加到索引里面去!c:\index


package test;

import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
/**
* 从索引文件查询内容
* @author Jan
*
*/
public class QueryString 
{
public static void main(String[] args) throws IOException, ParseException {   
         Hits hits = null;   
         String queryString = "我";   
         Query query = null;   
         IndexSearcher searcher = new IndexSearcher("c:\\index");   
   
         Analyzer analyzer = new StandardAnalyzer();   
         try {   
             QueryParser qp = new QueryParser("content", analyzer);   
             query = qp.parse(queryString);   
         } catch (ParseException e) {   
         }   
         if (searcher != null) {   
             hits = searcher.search(query);   
             if (hits.length() > 0) {   
                 System.out.println("找到:" + hits.length() + " 个结果!");   
             }   
         }   
     }   
}


测试结果:

找到:2 个结果!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值