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

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

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

    laoding
    本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
    posts - 0,  comments - 37,  trackbacks - 0
    用lucene來建立搜索程序,在檢索的時候效率大大的提高了,但是卻以建立索引為代價,建立索引本身就是個耗內存大、時間長的過程(數據量比較大,數據少何必用lucene來建立全文檢索,個人拙見),從而索引的建立就是個瓶頸,如果我們建立好索引,然后每次更新數據后重新建立索引,無疑是不合理的,為什么不能在原先索引文件的基礎上再把新更新的加在上面呢?增量索引就是在建完索引的后,將數據庫的最后一條記錄的ID存儲起來,下次建立時候將這個ID拿到,從而可以把更新的數據拿到,并把這些更新數據的索引文件加在原先的索引文件里面,下面來看個簡單的例子
    數據庫有兩個字段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);//參數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增量索引的簡單實現
    2009-11-07 14:07 | 真爛
    您寫代碼真垃圾,這么爛的水平也要學習lucene,難為你了。  回復  更多評論
      
    # re: lucene增量索引的簡單實現
    2009-11-10 16:35 | 老丁
    @真爛
    還是那句話,請自重!  回復  更多評論
      
    # re: lucene增量索引的簡單實現
    2010-01-13 12:42 | 辛苦了
    樓主辛苦了   回復  更多評論
      
    # re: lucene增量索引的簡單實現
    2010-01-26 15:59 | 路過
    @真爛

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


    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(4)

    我參與的團隊

    文章分類(50)

    文章檔案(48)

    相冊

    朋友

    搜索

    •  

    積分與排名

    • 積分 - 96446
    • 排名 - 600

    最新評論

    主站蜘蛛池模板: 在线看片韩国免费人成视频| 久久免费的精品国产V∧| 国内精品免费麻豆网站91麻豆| 亚洲日韩精品射精日| 人与动性xxxxx免费| 性做久久久久免费看| 美女扒开屁股让男人桶爽免费| 精品免费国产一区二区三区| 亚洲av极品无码专区在线观看| 日本XXX黄区免费看| 亚洲国产成人精品无码区在线网站| 91精品国产免费久久国语蜜臀 | 亚洲自偷自偷精品| 精品久久久久久国产免费了| 久久精品国产亚洲一区二区三区| 永久免费无码日韩视频| 亚洲AⅤ无码一区二区三区在线| 大片免费观看92在线视频线视频 | 亚洲国产成人精品女人久久久 | 日本无吗免费一二区| 日本高清不卡中文字幕免费| 亚洲国产成人久久一区WWW| 免费无遮挡无遮羞在线看| 亚洲日韩欧洲无码av夜夜摸| 亚洲精品免费在线观看| 亚洲一区二区久久| 国产免费观看网站| 久久国产乱子伦精品免费午夜| 亚洲精品无码专区在线在线播放| 日韩精品无码免费一区二区三区| 亚洲二区在线视频| 国产一区二区三区在线免费观看| 成人无码精品1区2区3区免费看| 亚洲男人天堂av| 免费黄色app网站| 国产精品福利片免费看| 亚洲小说图片视频| 在线看片无码永久免费aⅴ| 国产精品黄页免费高清在线观看| 激情内射亚洲一区二区三区| 国产一区二区三区在线免费|