• 用lucene替代mysql读库的尝试


    采用lucene对mysql中的表建索引,并替代全文检索操作。

    备注:代码临时梳理很粗糙,后续修改。

    import java.io.File;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Date;
    
    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.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.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    
    public class App {
        
        private static Directory dir = null;
    
        /**
         * @param args
         */
        public static void main(String[] args) {
        
             try {
                initIndex();
                searchBylucene();
                searchByMysql();
            } catch (IOException | ParseException | SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
        
        //创建索引
        private static void initIndex() throws IOException{
            
            dir = FSDirectory.open(new File("E:\index"));
            
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig iwc = new IndexWriterConfig(
                    Version.LUCENE_4_10_2, analyzer);
            iwc.setOpenMode(OpenMode.CREATE);
            IndexWriter writer = new IndexWriter(dir, iwc);
            
            Connection conn = null;
            
            conn = DbUtil.getcon();
            
            Statement stmt;
            
            try {
                stmt = conn.createStatement();
                String sql = "select * from t_content";
                ResultSet rs = stmt.executeQuery(sql);
                while (rs.next()) {
                
                    Document doc = new Document();
                    doc.add(new StringField("id", rs.getString(1), Field.Store.YES));
                    doc.add(new StringField("content", 
                            rs.getString(2), 
                            Field.Store.YES));
                        
                    writer.addDocument(doc);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                DbUtil.Close(conn);
            }finally {
                DbUtil.Close(conn);
                writer.close();
            }
        }
        
        
        private static void searchByMysql() throws SQLException{
            Connection conn = null;
            
            Date date1 = new Date();
            conn = DbUtil.getcon();
            Statement stmt = conn.createStatement();
            
            String sql = "select * from t_content where content like '%内马尔%'";
            ResultSet rs = stmt.executeQuery(sql);
            
            Date date2 = new Date();
            
            System.out.println("Mysql:" + String.valueOf(date2.getTime() - date1.getTime()));
            
            int i= 0;
            
            while (rs.next()) {
                //System.out.println(rs.getString(2));
                i++;
            }
            
            System.out.println(i);
            
        }
        
        private static void searchBylucene() throws IOException, ParseException{
            IndexReader reader = DirectoryReader.open(dir);
            IndexSearcher searcher = new IndexSearcher(reader);
            Date date1 = new Date();
            QueryParser parser = new QueryParser("content", new StandardAnalyzer()); 
            Query query = parser.parse("内马尔");
            
            TopDocs rs = searcher.search(query, null, 10);
            Date date2 = new Date();
            
            System.out.println("lucene:" + String.valueOf(date2.getTime() - date1.getTime()));
            System.out.println(rs.totalHits);
            if(rs.totalHits != 0){
                ScoreDoc[] hits = rs.scoreDocs;
                //System.out.println(searcher.doc(rs.scoreDocs[0].doc));
            }
            //System.out.println("end");
        }
    }
  • 相关阅读:
    循序渐进学Python 1 安装与入门
    常用yum命令小结
    为CentOS配置网易163的yum源
    PHP合并数组+与array_merge的区别
    让Docker功能更强大的10个开源工具
    Docker入门系列8
    Docker入门系列7 动态映射端口port mapping
    e 的由来
    ROS教程5 使用串口
    1 ROS+ 使用ORB_SLAM2进行全场定位
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/5784649.html
Copyright © 2020-2023  润新知