zoukankan      html  css  js  c++  java
  • Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询

    最近项目中要用到模糊查询,开始研究lucene,期间走了好多弯路,总算实现了一个简单的demo。

    使用的lucene jar包是3.6版本.

    一:建立数据库表,并加上测试数据。数据库表:UserInfo

     

    二:新建java project,并引入lucene jar包。http://lucene.apache.org/

     

    三:为数据库表建立索引及利用索引查数据:

     

    import java.io.File;


    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;


    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.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.QueryParser;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.util.Version;
    import org.apache.lucene.store.SimpleFSDirectory;
    import com.test.dbc.DBConnection;


    public class MakeTableIndex {
    public static void main(String[] args) throws IOException, SQLException {
    String indexDir = "d:\\lucene\\index";
    Connection conn;
    DBConnection conn1 = new DBConnection();
    conn = conn1.getConnection();
    PreparedStatement pstmt = conn
    .prepareStatement("SELECT * FROM UserInfo");


    ResultSet rs = pstmt.executeQuery();
    // 为表字段建立索引
    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("username***" + rs.getString(2));
    Document doc = new Document();
    doc.add(new Field("ID", rs.getString(1), Field.Store.YES,
    Field.Index.ANALYZED));
    doc.add(new Field("UserName", rs.getString(2), Field.Store.YES,
    Field.Index.ANALYZED));
    doc.add(new Field("Hobby", rs.getString(5), Field.Store.YES,
    Field.Index.ANALYZED));
    indexWriter.addDocument(doc);
    }
    System.out.println("numDocs" + indexWriter.numDocs());
    indexWriter.close();
    try {
    search();
    } catch (Exception e) {
    // TODO: handle exception
    System.out.println(e);
    }

    }


    // ------------------Search
    public static void search() throws Exception {
    String  dirPathString="d:\\lucene\\index";
    System.out.println(dirPathString);
    Directory dir = new SimpleFSDirectory(new File(dirPathString));//查询分析器  路径
    IndexReader reader = IndexReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    QueryParser parser = new QueryParser(Version.LUCENE_35, "UserName", new StandardAnalyzer(Version.LUCENE_36));
    Query q = parser.parse("张丽");
    TopDocs tds = searcher.search(q, 5);
    ScoreDoc[] sds = tds.scoreDocs;
    for (ScoreDoc sd : sds) {
    System.out.println(sd.score);
    int docName = sd.doc;
    Document doc = searcher.doc(docName);
    String UserName = doc.get("UserName");
    String Hobby = doc.get("Hobby");
    System.out.println("UserName:"+UserName+"---Hobby:"+Hobby);
    }
    }
    }

    package com.test.dbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    public class DBConnection {
        public static final String DBDRIVER= "com.microsoft.sqlserver.jdbc.SQLServerDriver"; ;
        public static final String DBURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Wang;" ;
        public static final String DBUSER = "sa" ;
        public static final String DBPASS = "sa" ;
        private Connection conn = null ;
        public DBConnection(){        //在构造方法中进行数据库连接
            try{
                Class.forName(DBDRIVER) ;     //加载驱动程序
                conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
            }catch(Exception e){
                e.printStackTrace() ;
            }
        }
        public Connection getConnection(){     //取得数据库连接
            System.out.println("数据库链接");
            return this.conn ;
        }
        public void close(){
            if(this.conn!=null){                  //数据库关闭操作,避免空指针异常。
                try{
                    this.conn.close() ;
                }catch(Exception e){}
            }
        }
    }

    有几点问题需要注意:

    1.建立索引的分词器和查询用的分词器必须一致

    2.建立索引的字段名和查询的字段名需保持一致,才能找到结果

    个人认为,lucene查询相比在数据库里查询表只是多了建立索引这一步,达到的目的都是从数据库了检索出我们需要的数据。

    以上仅代表个人观点,欢迎大家拍砖

  • 相关阅读:
    CSS 选择器
    HTML lable和fieldset
    html image和表格
    HTML a标签
    html 提交后台的标签
    HTML INPUT系列使用
    HTML内标签、换行
    HTML 头部详解
    单例模式
    const 指针的三种使用方式
  • 原文地址:https://www.cnblogs.com/bwlang/p/3203213.html
Copyright © 2011-2022 走看看