Lucene全文检索样例(解决大文本建索引)

来源:互联网 发布:加密狗软件图片 编辑:程序博客网 时间:2024/06/07 02:59

建索引:

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

查询:

Java代码 复制代码
  1. package  com.pccw;      
  2.      
  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.queryParser.ParseException;      
  8.  import  org.apache.lucene.queryParser.QueryParser;      
  9.  import  org.apache.lucene.search.Hits;      
  10.  import  org.apache.lucene.search.IndexSearcher;      
  11.  import  org.apache.lucene.search.Query;      
  12.      
  13.  public   class  TestQuery   {      
  14.      public   static   void  main(String[] args)  throws  IOException, ParseException   {      
  15.         Hits hits  =   null ;      
  16.         String queryString  =   "中华" ;      
  17.         Query query  =   null ;      
  18.         IndexSearcher searcher  =   new  IndexSearcher( " c://index " );      
  19.      
  20.         Analyzer analyzer  =   new  StandardAnalyzer();      
  21.          try    {      
  22.             QueryParser qp  =   new  QueryParser( " body " , analyzer);      
  23.             query  =  qp.parse(queryString);      
  24.         }   catch  (ParseException e)   {      
  25.         }       
  26.          if  (searcher  !=   null )   {      
  27.             hits  =  searcher.search(query);      
  28.              if  (hits.length()  >   0 )   {      
  29.                 System.out.println( " 找到: "   +  hits.length()  +   "  个结果! " );      
  30.             }       
  31.         }       
  32.     }     
  33.      
  34. }