对数据库中的数据进行索引

  • 创建一个名为test的数据库,按照下面建表语句创建表:
CREATE TABLE `authors` (
   `Au_id` varchar(11) NOT NULL,
   `Au_name` varchar(45) DEFAULT NULL,
   `Phone` char(12) DEFAULT NULL,
   PRIMARY KEY (`Au_id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8

在表中插入如下数据:

  • 数据库连接类DBHelpler.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DBHelper {
    public static final String url = "jdbc:mysql://localhost:3306/test";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "abc123";

    public Connection conn = null;
    public PreparedStatement pst = null;

    public DBHelper(String sql) {
        try {
            Class.forName(name);//指定连接类型
            conn = DriverManager.getConnection(url, user, password);//获取连接
            pst = conn.prepareStatement(sql);//准备执行语句
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void close() {
        try {
            this.conn.close();
            this.pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
  • TestLucene.java
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class TestLucene {

    static String sql = null;
    static DBHelper db1 = null;
    static ResultSet ret = null;
    String indexPath = "D:\\lucene\\index";

    /**
     * 获取数据库中的数据,返回ResultSet
     * 
     * @param sql 需要执行的sql语句
     */

    public ResultSet getResult(String sql) {
        db1 = new DBHelper(sql);// 创建DBHelper对象
        try {
            ret = db1.pst.executeQuery(sql);
            return ret;
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    public Analyzer getAnalyzer() {
        return new SmartChineseAnalyzer();
    }

    /**
     * 为数据库的数据建立索引
     * 
     * @param rs
     *            从数据库中返回的数据
     * @throws IOException
     * @throws SQLException
     */
    public void Index(ResultSet rs) throws IOException, SQLException {
        Directory directory = FSDirectory.open(Paths.get(indexPath));
        Analyzer analyzer = getAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        iwc.setOpenMode(OpenMode.CREATE);
        IndexWriter writer = new IndexWriter(directory, iwc);

        while (rs.next()) {
            Document document = new Document();
            Field field = new StringField("id", rs.getString("au_id"), Field.Store.YES);
            document.add(field);
            field = new StringField("name", rs.getString("au_name"), Field.Store.YES);
            document.add(field);
            field = new StringField("phone", rs.getString("Phone"), Field.Store.YES);
            document.add(field);
            writer.addDocument(document);
            System.out.println(
                    "adding: " + rs.getString("au_id") + "\t" + rs.getString("au_name") + "\t" + rs.getString("Phone"));
        }
        writer.close();
    }
    /**
     * 查询
     * 
     * @throws IOException
     * @throws ParseException
     */
    public void seacher(String queryString) throws IOException, ParseException {
        String defualtField = "id";
        TopDocs topDocs = null;

        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(indexPath)));
        IndexSearcher searcher = new IndexSearcher(reader);

        Analyzer analyzer = getAnalyzer();

        QueryParser queryParser = new QueryParser(defualtField, analyzer);
        Query query = queryParser.parse(queryString);

        topDocs = searcher.search(query, 10);

        ScoreDoc[] hits = topDocs.scoreDocs;
        for (ScoreDoc hit : hits) {
            Document hitDoc = searcher.doc(hit.doc);
            System.out.println(hitDoc.get(defualtField) 
                        + " " + hitDoc.get("name"));
        }
    }

}
  • 测试类Test.java:
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.queryparser.classic.ParseException;

public class Test {

    public static void main(String[] args) throws IOException, SQLException, ParseException {
        TestLucene test=new TestLucene();
        String sql="select * from Authors";
        ResultSet rs=test.getResult(sql);
        test.Index(rs);
        test.seacher("2");
    }
}

运行结果:

  • 查看索引
      使用查看lucene索引的工具——luke[2],下载的时候要下载跟当前lucene对应版本的luke,否则会无法读取。Luke的界面如下:
对数据库中的数据进行索引_第1张图片

参考资料:

[1]http://www.cnblogs.com/sleeper-qp/archive/2012/12/20/2827238.html
[2] https://github.com/DmitryKey/luke/releases
[3] 罗刚. 解密搜索引擎技术实战--LUCENE & JAVA精华版(第3版)[M]. 电子工业出版社, 2016.

你可能感兴趣的:(对数据库中的数据进行索引)