• luence全文检索(数据库检索)



    注解:从数据库中查询所有数据然后放入luence中,然后在luence来检索

    package com.zhu.demo;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    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.document.TextField;
    import org.apache.lucene.index.CorruptIndexException;
    import org.apache.lucene.index.DirectoryReader;
    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.store.Directory;
    import org.apache.lucene.store.RAMDirectory;
    import org.apache.lucene.util.Version;
    /**
     *
     * Lucene与数据库结合使用
     *
     * @author YipFun
     */
    public class LuceneDemo06 {
        private static final String driverClassName="com.mysql.jdbc.Driver";
        private static final String url="??characterEncoding=utf-8";
        private static final String username="?";
        private static final String password="?";
    
        private static final Version version = Version.LUCENE_4_9;
        private Directory directory = null;
        private DirectoryReader ireader = null;
        private IndexWriter iwriter = null;
        //private IKAnalyzer analyzer;
        private Analyzer analyzer;
        private Connection conn;
    
        public LuceneDemo06() {
            directory = new RAMDirectory();            
        }
        public IndexSearcher getSearcher(){
            try {
                if(ireader==null) {
                    ireader = DirectoryReader.open(directory);
                } else {
                    DirectoryReader tr = DirectoryReader.openIfChanged(ireader) ;
                    if(tr!=null) {
                        ireader.close();
                        ireader = tr;
                    }
                }
                return new IndexSearcher(ireader);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public Connection getConnection(){
            if(this.conn == null){
                try {
                    Class.forName(driverClassName);
                    conn = DriverManager.getConnection(url, username, password);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return conn;
        }
        private Analyzer getAnalyzer(){
            if(analyzer == null){
                return new StandardAnalyzer();
            }else{
                return analyzer;
            }
        }
    
        public void createIndex(){
            Connection conn = getConnection();
            ResultSet rs = null;
            PreparedStatement pstmt = null;
            if(conn == null){
                System.out.println("get the connection error...");
                return ;
            }
            String sql = "select * from es_goods";
            try {
                pstmt = conn.prepareStatement(sql);
                rs = pstmt.executeQuery();
                IndexWriterConfig iwConfig=new IndexWriterConfig(getAnalyzer());
                iwConfig.setOpenMode(OpenMode.CREATE);
                iwriter = new IndexWriter(directory,iwConfig);
                while(rs.next()){
                    Integer goods_id = rs.getInt(1);             
                    String name = rs.getString(2);
                    String sn= rs.getString(3);
                    int band_id=rs.getInt(4);
                    int cat_id = rs.getInt(5);
                    int type_id= rs.getInt(6);
                    String goods_type = rs.getString(7);
                    String unit = rs.getString(8);
                    Double weight = rs.getDouble(9);
                    Integer market_enable = rs.getInt(10);  
                    String brief = rs.getString(11);
                    Long intro = rs.getLong(12);
                    Double price = rs.getDouble(13);
                    Double cost = rs.getDouble(14);
                    Double mktprice = rs.getDouble(15);
                    String params = rs.getString(16);
                    String specs = rs.getString(17);  
                    Integer have_spec = rs.getInt(18);
                    Long adjuncts = rs.getLong(19);
                    Integer create_time = rs.getInt(20);
                    Integer last_modify = rs.getInt(21);
                    Integer view_count = rs.getInt(22);
                    Integer buy_count = rs.getInt(23);
                    Integer disabled = rs.getInt(24);
                    Integer store = rs.getInt(25);
                    Integer enable_store = rs.getInt(26);
                    Integer point = rs.getInt(27);
                    String page_title = rs.getString(28);
                    String meta_keywords = rs.getString(29);
                    String meta_description = rs.getString(30);
                    String p20 = rs.getString(31);
                    String p19 = rs.getString(32);
                    String p18= rs.getString(33);  
                    String p17= rs.getString(34);
                    String p16= rs.getString(35);
                    String p15= rs.getString(36);
                    String p14= rs.getString(37);
                    String p13= rs.getString(38);
                    String p12 = rs.getString(39);
                    String p11= rs.getString(40);
                    String p10= rs.getString(41);  
                    String p9= rs.getString(42);
                    String p8= rs.getString(43);
                    String p7= rs.getString(44);
                    String p6= rs.getString(45);
                    String p5 = rs.getString(46);
                    String p4= rs.getString(47);
                    Integer p3= rs.getInt(48);
                    String p2= rs.getString(49);  
                    String p1 = rs.getString(50);
                    Integer sord= rs.getInt(51);
                    int have_field= rs.getInt(52);
                    int grade = rs.getInt(53);
                    Long goods_comment = rs.getLong(54);
                    Integer is_pack= rs.getInt(55);
                    String thumbnail= rs.getString(56);
                    String big= rs.getString(57);
                    String small= rs.getString(58);
                    String original= rs.getString(59);
                    String p21= rs.getString(60);
                    String p22= rs.getString(61);
                    String p23= rs.getString(62);
                    
                    Document doc = new Document();
                    
                    doc.add(new TextField("goods_id", goods_id+"",Field.Store.YES));//1
                    doc.add(new TextField("name", name+"",Field.Store.YES));//2
                    doc.add(new TextField("sn", sn+"",Field.Store.YES));//3
                    doc.add(new TextField("band_id", band_id+"",Field.Store.YES));//4
                    doc.add(new TextField("cat_id", cat_id+"",Field.Store.YES));//5
                    doc.add(new TextField("type_id", type_id+"",Field.Store.YES));//6
                    doc.add(new TextField("goods_type", goods_type+"",Field.Store.YES));//7
                    doc.add(new TextField("unit", unit+"",Field.Store.YES));//8
                    doc.add(new TextField("weight", weight+"",Field.Store.YES));//9
                    doc.add(new TextField("market_enable", market_enable+"",Field.Store.YES));//11
                    doc.add(new TextField("brief", brief+"",Field.Store.YES));//12
                    doc.add(new TextField("intro", intro+"",Field.Store.YES));//13
                    doc.add(new TextField("price", price+"",Field.Store.YES));//14
                    doc.add(new TextField("cost", cost+"",Field.Store.YES));//15
                    doc.add(new TextField("mktprice", mktprice+"",Field.Store.YES));//16
                    doc.add(new TextField("params", params+"",Field.Store.YES));//17
                    doc.add(new TextField("specs", specs+"",Field.Store.YES));//18
                    doc.add(new TextField("have_spec", have_spec+"",Field.Store.YES));//19
                    doc.add(new TextField("adjuncts", adjuncts+"",Field.Store.YES));//20
                    doc.add(new TextField("create_time", create_time+"",Field.Store.YES));//21
                    doc.add(new TextField("last_modify", last_modify+"",Field.Store.YES));//23
                    doc.add(new TextField("view_count", view_count+"",Field.Store.YES));//24
                    doc.add(new TextField("buy_count", buy_count+"",Field.Store.YES));//25
                    doc.add(new TextField("disabled", disabled+"",Field.Store.YES));//26
                    doc.add(new TextField("store", store+"",Field.Store.YES));//27
                    doc.add(new TextField("enable_store", enable_store+"",Field.Store.YES));//28
                    doc.add(new TextField("point", point+"",Field.Store.YES));//29
                    doc.add(new TextField("page_title", page_title+"",Field.Store.YES));//30
                    doc.add(new TextField("meta_keywords", meta_keywords+"",Field.Store.YES));//31
                    doc.add(new TextField("meta_description", meta_description+"",Field.Store.YES));//32
                     doc.add(new TextField("p20", p20+"",Field.Store.YES));//33
                    doc.add(new TextField("p19", p19+"",Field.Store.YES));//34
                    doc.add(new TextField("p18", p18+"",Field.Store.YES));//35
                    doc.add(new TextField("p17", p17+"",Field.Store.YES));//36
                    
                    doc.add(new TextField("p16", p16+"",Field.Store.YES));//37
                    doc.add(new TextField("p15", p15+"",Field.Store.YES));//38
                    doc.add(new TextField("p14", p14+"",Field.Store.YES));//39
                    doc.add(new TextField("p13", p13+"",Field.Store.YES));//40
                    doc.add(new TextField("p12", p12+"",Field.Store.YES));//41
                    doc.add(new TextField("p11", p11+"",Field.Store.YES));//42
                    doc.add(new TextField("p10", p10+"",Field.Store.YES));//43
                    doc.add(new TextField("p9", p9+"",Field.Store.YES));//44
                    doc.add(new TextField("p8", p8+"",Field.Store.YES));//45
                    doc.add(new TextField("p7", p7+"",Field.Store.YES));//46
                    doc.add(new TextField("p6", p6+"",Field.Store.YES));//47
                    doc.add(new TextField("p5", p5+"",Field.Store.YES));//48
                    doc.add(new TextField("p4", p4+"",Field.Store.YES));//49
                    doc.add(new TextField("p3", p3+"",Field.Store.YES));//50
                    doc.add(new TextField("p2", p2+"",Field.Store.YES));//51
                    doc.add(new TextField("p1", p1+"",Field.Store.YES));//52
                    doc.add(new TextField("sord", sord+"",Field.Store.YES));//53
                    doc.add(new TextField("have_field", have_field+"",Field.Store.YES));//54
                    doc.add(new TextField("grade", grade+"",Field.Store.YES));//55
                    doc.add(new TextField("goods_comment", goods_comment+"",Field.Store.YES));//56
                    doc.add(new TextField("is_pack", is_pack+"",Field.Store.YES));//57
                    doc.add(new TextField("thumbnail", thumbnail+"",Field.Store.YES));//58
                    doc.add(new TextField("big", big+"",Field.Store.YES));//59
                    doc.add(new TextField("small", small+"",Field.Store.YES));//60
                    doc.add(new TextField("original", original+"",Field.Store.YES));//61
                    doc.add(new TextField("p21", p21+"",Field.Store.YES));//62
                    doc.add(new TextField("p22", p22+"",Field.Store.YES));//63
                    doc.add(new TextField("p23", p23+"",Field.Store.YES));//64           
                    iwriter.addDocument(doc);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                try {
                    if(iwriter != null)
                    iwriter.close();
                    rs.close();
                    pstmt.close();
                    if(!conn.isClosed()){
                        conn.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    
        public void searchByTerm(String field,String keyword,int num) throws Exception{
             IndexSearcher isearcher = getSearcher();
             Analyzer analyzer =  getAnalyzer();
            //使用QueryParser查询分析器构造Query对象
            QueryParser qp = new QueryParser(field,analyzer);
            //这句所起效果?
           // qp.setDefaultOperator(QueryParser.OR_OPERATOR);
            qp.setDefaultOperator(QueryParser.AND_OPERATOR);
            try {
                Query query = qp.parse(keyword);
                ScoreDoc[] hits;
    
                //注意searcher的几个方法
                hits = isearcher.search(query, null, num).scoreDocs;
               // assertEquals(1, hits.length);
                System.out.println("the names is =");
                for (int i = 0; i < hits.length; i++) {
                    Document doc = isearcher.doc(hits[i].doc);
                    System.out.print(doc.get("goods_id")+" ");
                    System.out.println(doc.get("name")+" ");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    
        public static void main(String[] args) throws Exception {
            LuceneDemo06 ld = new LuceneDemo06();
            ld.createIndex();
            ld.searchByTerm("name", "平安", 100);
        }
    }
  • 相关阅读:
    5.基于文件验证的vsftp虚拟用户
    linux系统用户登录ftp
    4.ftp用户禁锢
    原码,反码,补码
    8map
    命令jhsdb jmap heap pid 1 报错
    20211112_mysql_8.0docker部署踩坑
    Windows 下 tail 查看日志命令工具分享
    前后端java+vue 实现rsa 加解密与摘要签名算法
    优化TTFB 至500ms内
  • 原文地址:https://www.cnblogs.com/xzjf/p/7110052.html
Copyright © 2020-2023  润新知