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 老丁 阅读(10667) 评论(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


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

留言簿(4)

我参与的团队

文章分类(50)

文章档案(48)

相册

朋友

搜索

  •  

积分与排名

  • 积分 - 95305
  • 排名 - 599

最新评论