使用Lucene为数据库建立索引

一,lucene中比较基础的建立索引步骤(资料:http://www.ourys.com/post/lucene3-0_database_index.html)

1.指定目录索引

2.创建Directory对象(参数是存放索引的File类型,根据File的存放地点选择创建类)

3.创建indexWriter对象,参数(Directory对象,分词器,是否创建,分词的最大值)

4.获取源文件的File数组

5.通过循环将每个文件写入索引。

     创建Document对象,并创建Field对象(列名称(文件名、内容等)),将Field加入到Dcument中,通过IndexWriter.addDocument(Document)写入索引中。

6.关闭indexWriter。

二,连接数据库的基本过程(资料:http://blog.csdn.net/xiehuan_uestc/article/details/7969321)

基本过程如下:

l 建立数据源

l 导入用到的包

l 加载驱动程序

l 创建与数据库的连接

l 创建语句对象

l 编写SQL语句

l 执行SQL语句

l 处理数据

l 关闭相关对象

l 处理异常

 

三,具体步骤:

  说明: 数据库:sql server 2005

    IDE:myeclipse 10.0

    lucene版本:3.61

   1,sql中新建数据库test,新建表Article(....)以及加入例子

        myecl中新建webproject,将sqljdbc.jar,lucene-core-3.6.1-javadoc.jar以及lucene-core-3.6.1.jar放在web-inf/lib中

   2,新建DataBaseIndexer.java以及DBConn.java

    

复制代码
package com.DBIndex;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;



/**********************
*
* 建立数据库索引 lucene3.6+
* 
*/

public class DataBaseIndexer{
public static void main(String[] args) throws IOException,SQLException{
String indexDir = "d:\\lucene\\index";
DBConn conn = new DBConn();
conn.OpenConnection();

ResultSet rs = conn.ExecuteQuery("select * from Article");
// 为表字段建立索引 
Directory dir = new SimpleFSDirectory(new File(indexDir)); 

Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36,luceneAnalyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(dir,iwc);  


while (rs.next()) { 
System.out.println(rs.getString("Article_Title")); 
Document doc = new Document(); 
doc.add(new Field("Article_Title", rs.getString("Article_Title"),Field.Store.YES, Field.Index.ANALYZED)); 
doc.add(new Field("Article_Content", rs.getString("Article_Content"),Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("indexDate",DateTools.dateToString(new Date(), DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NOT_ANALYZED)); 
indexWriter.addDocument(doc); 
}
System.out.println("numDocs"+indexWriter.numDocs()); 
indexWriter.close(); 
}
}
复制代码
复制代码
package com.DBIndex;
import java.text.*;
import java.util.*;
import java.sql.*;
//import com.jstrd.htgl.webservice.XmlUtility; /*XML可能用到*/

/** *//**
 *
 */

public class DBConn {

  public String ClassString=null;
  public String ConnectionString=null;
  public String UserName=null;
  public String PassWord=null;

  public Connection Conn;
  public Statement Stmt;


  public DBConn() {
    //From System.xml
   // ClassString=XmlUtility.getConfigValue("DB_ClassString");//"oracle.jdbc.driver.OracleDriver";
   // ConnectionString=XmlUtility.getConfigValue("DB_ConnectionString");//"jdbc:oracle:thin:@192.168.103.171:1521:jstrd";
    //UserName=XmlUtility.getConfigValue("DB_UserName");//"htgl";
    //PassWord=XmlUtility.getConfigValue("DB_PassWord");//"1";

    //For ODBC
    //ClassString="sun.jdbc.odbc.JdbcOdbcDriver";
    //ConnectionString=("jdbc:odbc:DBDemo");
    //UserName="dbdemo";
    //PassWord="dbdemo";


    //For Access Driver
    //ClassString="sun.jdbc.odbc.JdbcOdbcDriver";
    //ConnectionString=("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\dbdemo.mdb;ImplicitCommitSync=Yes;MaxBufferSize=512;MaxScanRows=128;PageTimeout=5;SafeTransactions=0;Threads=3;UserCommitSync=Yes;").replace('\\','/');

    //For SQLServer Driver
      //      MSSQL 2005:Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      //      MSSQL 2000:Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
    ClassString="com.microsoft.sqlserver.jdbc.SQLServerDriver";
    ConnectionString="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test;User=sa;Password=1234";
    //ClassString="net.sourceforge.jtds.jdbc.Driver";
   // ConnectionString="jdbc:jtds:sqlserver://127.0.0.1:1433;DatabaseName=name;User=sa;Password=pwd";

    //For Oracle Driver
    //ClassString="oracle.jdbc.driver.OracleDriver";
    //ConnectionString="jdbc:oracle:thin:@192.168.103.171:1521:jstrd";
    //UserName="htgl";
    //PassWord="1";

    //For MySQL Driver
    //ClassString="org.gjt.mm.mysql.Driver";
    //ConnectionString="jdbc:mysql://localhost:3306/blog?user=root&password=pwd&useUnicode=true&characterEncoding=8859_1";
    //ClassString="com.mysql.jdbc.Driver";
    //ConnectionString="jdbc:mysql://localhost:3306/blog?user=root&password=pwd&useUnicode=true&characterEncoding=gb2312";

    //For Sybase Driver
    //ClassString="com.sybase.jdbc.SybDriver";
    //ConnectionString="jdbc:sybase:Tds:localhost:5007/tsdata"; //tsdata为你的数据库名
    //Properties sysProps = System.getProperties();
    //SysProps.put("user","userid");
    //SysProps.put("password","user_password");
    //If using Sybase then DriverManager.getConnection(ConnectionString,sysProps);
  }

  //打开连接
  public boolean OpenConnection()
  {
   boolean mResult=true;
   try
   {
     Class.forName(ClassString);
     if ((UserName==null) && (PassWord==null))
     {
       Conn= DriverManager.getConnection(ConnectionString);
     }
     else
     {
       Conn= DriverManager.getConnection(ConnectionString,UserName,PassWord);
     }

     Stmt=Conn.createStatement();
     mResult=true;
   }
   catch(Exception e)
   {
     System.out.println(e.toString());
     mResult=false;
   }
   return (mResult);
  }

  //关闭数据库连接
  public void CloseConnection()
  {
   try
   {
     Stmt.close();
     Conn.close();
   }
   catch(Exception e)
   {
     System.out.println(e.toString());
   }
  }

  //获取当前时间(JAVA)
  public String GetDateTime()
  {
   Calendar cal  = Calendar.getInstance();
   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String mDateTime=formatter.format(cal.getTime());
   return (mDateTime);
  }

  //获取当前时间(T-SQL)
  public  java.sql.Date  GetDate()
  {   
    Calendar cal  = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String mDateTime=formatter.format(cal.getTime());
    return (java.sql.Date.valueOf(mDateTime));
  }

   //生成新的ID
   public int GetMaxID(String vTableName,String vFieldName)
  {
   int mResult=0;
   boolean mConn=true;
   String mSql=new String();
   mSql = "select max("+vFieldName+")+1 as MaxID from " + vTableName;
   try
   {
       if (Conn!=null){
           mConn=Conn.isClosed();
       }
       if (mConn){
         OpenConnection();
       }

       ResultSet result=ExecuteQuery(mSql);
       if (result.next())
       {
         mResult=result.getInt("MaxID");
       }
       result.close();

       if (mConn)
       {
         CloseConnection();
       }

     }
     catch(Exception e)
     {
       System.out.println(e.toString());
   }
   return (mResult);
 }

  //数据检索
  public ResultSet ExecuteQuery(String SqlString)
  {
    ResultSet result=null;
    try
    {
      result=Stmt.executeQuery(SqlString);
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
    return (result);
  }

  //数据更新(增、删、改)
  public int ExecuteUpdate(String SqlString)
  {
    int result=0;
    try
    {
      result=Stmt.executeUpdate(SqlString);
    }
    catch(Exception e)
    {
      System.out.println(e.toString());
    }
    return (result);
  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值