<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    laoding
    本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風(fēng)的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
    posts - 0,  comments - 37,  trackbacks - 0
    用lucene來建立搜索程序,在檢索的時候效率大大的提高了,但是卻以建立索引為代價,建立索引本身就是個耗內(nèi)存大、時間長的過程(數(shù)據(jù)量比較大,數(shù)據(jù)少何必用lucene來建立全文檢索,個人拙見),從而索引的建立就是個瓶頸,如果我們建立好索引,然后每次更新數(shù)據(jù)后重新建立索引,無疑是不合理的,為什么不能在原先索引文件的基礎(chǔ)上再把新更新的加在上面呢?增量索引就是在建完索引的后,將數(shù)據(jù)庫的最后一條記錄的ID存儲起來,下次建立時候?qū)⑦@個ID拿到,從而可以把更新的數(shù)據(jù)拿到,并把這些更新數(shù)據(jù)的索引文件加在原先的索引文件里面,下面來看個簡單的例子
    數(shù)據(jù)庫有兩個字段id和title,話不多說,直接上代碼,一看便知

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    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.IndexWriter;

    public class Index {

        
    public static void main(String[] args) {
            
    try {
                Index index 
    = new Index();
                String path 
    = "d:\\index";//索引文件的存放路徑
                String storeIdPath = "d:\\storeId.txt";//存儲ID的路徑
                String storeId ="";
                storeId 
    = index.getStoreId(storeIdPath);
                ResultSet rs 
    = index.getResult(storeId);
                index.indexBuilding(path, storeIdPath, rs);
                storeId 
    = index.getStoreId(storeIdPath);
                System.out.println(storeId);
    //打印出這次存儲起來的ID
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        
    public ResultSet getResult(String storeId) throws Exception{
            Class.forName(
    "com.mysql.jdbc.Driver").newInstance();
            String url 
    = "jdbc:mysql://localhost:3306/ding";
            String userName 
    = "root";
            String password 
    = "ding";
            Connection conn 
    = DriverManager.getConnection(url,userName,password);
            Statement stmt 
    = conn
                .createStatement();
            ResultSet rs 
    = stmt
                .executeQuery(
    "select * from newitem where id > '"+storeId+"'order by id");
            
    return rs;
        }

        
    public boolean indexBuilding(String path,String storeIdPath, ResultSet rs) {// 把RS換成LIST原理一樣

            
    try {
                Analyzer luceneAnalyzer 
    = new StandardAnalyzer();
                
    // 取得存儲起來的ID,以判定是增量索引還是重新索引
                boolean isEmpty = true;
                 
    try { 
                    File file 
    = new File(storeIdPath);
                    
    if (!file.exists()) {
                        file.createNewFile();
                    }
                    FileReader fr 
    = new FileReader(storeIdPath);
                    BufferedReader br 
    = new BufferedReader(fr);                 
                    
    if(br.readLine()!= null) {
                        isEmpty 
    = false;
                     }
                     br.close();
                     fr.close(); 
                    } 
    catch (IOException e) { 
                       e.printStackTrace();
                  }

                IndexWriter writer 
    = new IndexWriter(path, luceneAnalyzer, isEmpty);//參數(shù)isEmpty是false表示增量索引
                String storeId = "";
                
    boolean indexFlag = false;
                String id;
                String title;
                
    while (rs.next()) {
                    
    // for(Iterator it = list.iterator();it.hasNext();){
                    id = rs.getString("id");
                    title 
    = rs.getString("title");
                    writer.addDocument(Document(id, title));
                    storeId 
    = id;//將拿到的id給storeId,這種拿法不合理,這里為了方便
                    indexFlag = true;
                }
                writer.optimize();
                writer.close();
                
    if(indexFlag){
                    
    // 將最后一個的ID存到磁盤文件中
                    this.writeStoreId(storeIdPath, storeId);
                }
                
    return true;
            } 
    catch (Exception e) {
                e.printStackTrace();
                System.out.println(
    "出錯了" + e.getClass() + "\n   錯誤信息為:   "
                        
    + e.getMessage());
                
    return false;
            }

        }


        
    public static Document Document(String id, String title) {
            Document doc 
    = new Document();
            doc.add(
    new Field("ID", id, Field.Store.YES, Field.Index.TOKENIZED));
            doc.add(
    new Field("TITLE", title, Field.Store.YES,
                    Field.Index.TOKENIZED));
            
    return doc;
        }

        
    // 取得存儲在磁盤中的ID
        public static String getStoreId(String path) {
            String storeId 
    = "";
            
    try {
                File file 
    = new File(path);
                
    if (!file.exists()) {
                    file.createNewFile();
                }
                FileReader fr 
    = new FileReader(path);
                BufferedReader br 
    = new BufferedReader(fr);
                storeId 
    = br.readLine();
                
    if (storeId == null || storeId == "")
                    storeId 
    = "0";
                br.close();
                fr.close();
            } 
    catch (Exception e) {
                e.printStackTrace();
            }
            
    return storeId;
        }

        
    // 將ID寫入到磁盤文件中
        public static boolean writeStoreId(String path,String storeId) {
            
    boolean b = false;
            
    try {
                File file 
    = new File(path);
                
    if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fw 
    = new FileWriter(path);
                PrintWriter out 
    = new PrintWriter(fw);
                out.write(storeId);
                out.close();
                fw.close();
                b
    =true;
            } 
    catch (IOException e) {
                e.printStackTrace();
            }
            
    return b;
        }
    }

    這里代碼寫的比較簡單,很多需要改進的地方,自己改進就行了,這里只是說明了增量索引的原理,望指正。

    posted on 2009-05-31 16:37 老丁 閱讀(10725) 評論(11)  編輯  收藏 所屬分類: 搜索引擎 lucene

    FeedBack:
    # re: lucene增量索引的簡單實現(xiàn)
    2009-11-07 14:07 | 真爛
    您寫代碼真垃圾,這么爛的水平也要學(xué)習(xí)lucene,難為你了。  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2009-11-10 16:35 | 老丁
    @真爛
    還是那句話,請自重!  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2010-01-13 12:42 | 辛苦了
    樓主辛苦了   回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2010-01-26 15:59 | 路過
    @真爛

    光說人家的不好,有本事你寫個來瞧瞧?
    損人不利己~~~  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2010-09-07 10:14 | 游客12
    @真爛
    你要爛的地方說出來,這才有道理,你如果光說爛,說不出理由來,可見你也挺爛的  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2011-01-09 10:55 | 剛子
    你寫的東西,思路還是有點亂,建議整理一下  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2011-02-19 21:21 | jychen
    如果數(shù)據(jù)庫中間刪除了一個記錄?  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2011-10-17 10:01 | jackkan
    真正NB的人就不會在這里了!為人還是要低調(diào)和謙虛  回復(fù)  更多評論
      
    # 友情分享
    2012-10-27 12:01 | 路人甲
    有關(guān)lucene的問題,用lucene構(gòu)建實時索引
    http://www.360doc.com/content/12/0514/22/1542811_211066061.shtml  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)
    2015-07-31 16:49 | 不爽
    @真爛
    關(guān)你鳥事  回復(fù)  更多評論
      
    # re: lucene增量索引的簡單實現(xiàn)[未登錄]
    2016-01-30 16:17 | aa
    @真爛
    沙比一個!你說人家爛,你特么倒是寫一個試試啊。  回復(fù)  更多評論
      
    本博客主為學(xué)習(xí)和復(fù)習(xí)之用,無關(guān)其他,想罵人的繞道
    Email:dkm123456@126.com
    大家一起交流進步
    QQ:283582761


    <2015年7月>
    2829301234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    留言簿(4)

    我參與的團隊

    文章分類(50)

    文章檔案(48)

    相冊

    朋友

    搜索

    •  

    積分與排名

    • 積分 - 96426
    • 排名 - 600

    最新評論

    主站蜘蛛池模板: 成人特黄a级毛片免费视频| 亚洲国产精品乱码在线观看97| 毛片免费观看网址| 青青草原1769久久免费播放| 免费人妻精品一区二区三区| 亚洲日本乱码卡2卡3卡新区| 久久亚洲国产成人精品性色| 91精品国产亚洲爽啪在线观看| 亚洲不卡无码av中文字幕| 成年在线网站免费观看无广告| 免费无码VA一区二区三区| 国产精品玖玖美女张开腿让男人桶爽免费看| 激情内射亚洲一区二区三区爱妻| 亚洲国产高清人在线| 亚洲欧洲日产国码无码久久99 | 亚洲va无码专区国产乱码| 亚洲Aⅴ无码一区二区二三区软件| 日本免费电影一区| 成人毛片免费视频| 一二三四在线播放免费观看中文版视频| 小草在线看片免费人成视久网| 两个人看的www免费| 久久久久久久国产免费看| 日韩免费在线中文字幕| 日本精品久久久久久久久免费| 免费亚洲视频在线观看| 男男gay做爽爽的视频免费| 亚洲A∨精品一区二区三区下载| 亚洲男人的天堂网站| 亚洲小说图区综合在线| 亚洲午夜无码久久久久小说| 亚洲欧洲日韩极速播放| 亚洲欧美综合精品成人导航| 亚洲日韩中文字幕无码一区| 久久久久久亚洲精品影院| 亚洲熟妇无码一区二区三区 | 欧洲精品成人免费视频在线观看 | 亚洲中文字幕一二三四区苍井空| 2020亚洲男人天堂精品| 亚洲成AV人片高潮喷水| 国产午夜亚洲精品不卡|