`
baobeituping
  • 浏览: 1042163 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Lucene3 建立索引

阅读更多

Lucene3在建立索引的API上有比较大的变化。直接上程序:

 

package lucene3;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.indexdb.Config;
import com.indexdb.DataBase;

public class DBIndex {

 /**
  * @param args
  */
 public static Connection DBConn = null;
 public static Statement Stmt = null;
 public static ResultSet Rs = null;
 public static String strArgs[] = new String[10];
 public static DataBase db = null;
 public static Config config = new Config();
 public static boolean dbflag=false; 
 public static  File INDEX_DIR = new File("d://lucene3");  
   
 public static boolean initDBConn()
 {
  
  try {
   System.out.println("开始数据库连接...");
   Class.forName("com.mysql.jdbc.Driver");
   DBConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8","root","root");
   System.out.println("数据库连接成功...");
   dbflag=true;
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  return dbflag;
 }
 public static void main(String[] args) {
  if(initDBConn())
  {
   String sql = "select ID,Name from kindergarden_user";
    Date start = new Date();  
       try 
       {  
           IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR),   
                   new StandardAnalyzer(Version.LUCENE_CURRENT),   
                   true, //true为覆盖原index;false为追加  
                   IndexWriter.MaxFieldLength.LIMITED);  
           System.out.println("Indexing to directory '" + INDEX_DIR + "'...");
           Stmt = DBConn.createStatement();
           Rs = Stmt.executeQuery(sql);
           while(Rs.next())
           {
            Document doc = new Document();
            Field f1 = new Field("ID",Rs.getString("ID"),Field.Store.YES,Field.Index.ANALYZED);
            Field f2 = new Field("Name",Rs.getString("Name"),Field.Store.YES,Field.Index.ANALYZED);
            doc.add(f1);
            doc.add(f2);
            writer.addDocument(doc);
           }
           System.out.println("Optimizing...");  
           writer.optimize();          //优化索引  
           writer.close();  
          
    
           Date end = new Date();  
           System.out.println(end.getTime() - start.getTime() + " total milliseconds");  
    
       }  
       catch (Exception e)  
       {  
           System.out.println(e.getMessage());  
           
       }  

  }

 }

}

 

 

创建索引的主要步骤:

1、先指定要搜索的文件(从数据库中搜索做为测试数据)和存放索引文件的目录(INDEX_DIR )

2、创建IndexWriter-需要提供索引目录、使用的Analyzer等。Analyzer用于解析文本内容,拆分成单词,这里我使用的是lucene自带的分词器。

3、递归遍历所有数据,生成Document,每一个文件对于一个Document。

4、将Document逐个加入索引

5、关闭IndexWriter,保存索引信息

 

说明:

1、索引文件可存放在目录或内存中,分别使用FSDirectory和RAMDirectory

2、每个Document类似与索引中的一行记录,具体的字段由Field标识,我这里加入了2个Field,分别是ID和NAME

3、Field中的枚举字段

     Field.Store.YES表示此字段内容需要在索引中保存

     Field.Index.NOT_ANALYZED表示此字段内容不需要做分词

      

 

几个基本对象间的关系如下图:

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics