public void AddIndexFile(int BusinessID)
{
//因为我是交替创建索引在不同的文件夹,所以需要获取当前索引文件夹
string IndexPath = GetCurrentIndexPath("Index");
IndexPath = Server.MapPath("/search/" + IndexPath + "/");
IndexSearcher searchBusiness = new IndexSearcher(IndexPath);
//增量索引,false
IndexWriter iw = new IndexWriter(IndexPath,sa,false); try
{
//根据商户信息删除指定索引(删除所有id相符的索引项)
Term delSearchTerm = new Term("BusinessID", BusinessID.ToString());
IndexReader reader = searchBusiness.GetIndexReader();
reader.DeleteDocuments(delSearchTerm);
reader.Close();
//以上删除只是做了标记,并没有真正删除。还能搜索出来
//查询出需要建立索引的记录
string Sql = @"SELECT DISTINCT BusinessID FROM Business
Sql += "where businessid=" + BusinessID;
//执行SQL
conn.Open();
SqlCommand cmd = new SqlCommand(Sql,conn);
cmd.CommandTimeout=20*60*1000;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
//添加每一条记录到索引文件
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
Document docu = new Document();
foreach(DataColumn col in ds.Tables[0].Columns)
{
Field tField;
if (col.ColumnName == "BusinessID" || col.ColumnName == "ReviewTime")
{
//对不需要搜索的字段,它的数字不进行处理
tField =new Field(col.ColumnName, ds.Tables[0].Rows [col.ColumnName].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED);
}else{
//因为这个版本的lucene不支持数字搜索,所以这里要对数字进行处理。
//以及部分特殊符号一并处理
tField = Field.Text(col.ColumnName,ds.Tables[0].Rows[col.ColumnName].ToString().Replace("1","一").Replace("2","二").Replace("3","三").Replace("4","四").Replace("5","五").Replace("6","六").Replace("7","七").Replace("8","八").Replace("9","九").Replace("0","零").Replace("+","+").Replace("-","-").Replace("&","—").Replace("|","|").Replace("!","!").Replace("(","(").Replace(")",")").Replace("{","{").Replace("}","}").Replace("[","[").Replace("]","]").Replace("^","……").Replace("/"","“").Replace("~","~").Replace("*","*").Replace("?","?").Replace(":",":").Replace("//","、").Replace("/","/").Replace("'","‘").Replace(" "," ").Replace(" "," ").ToLower());
}
//增加信息到lucene文档
docu.Add(tField);
}
//将增加的信息写入索引文件
iw.AddDocument(docu);
}
//对索引文件进行优化,这时将刚才删除的所以项正式删除
iw.Optimize();
}catch (Exception ex){
if (ex.GetType().ToString() != "System.Threading.ThreadAbortException")
{
AddError("Build Add Index Fail.");
}
}finally{
iw.Close();
conn.Close();
}
}
}
}
以上是关于增量索引的完整程序和说明,包含了修改数据的索引增量处理。
目前在项目中运行良好,暂时没有出现问题。本程序可直接用于增量和修改增量索引
{
//因为我是交替创建索引在不同的文件夹,所以需要获取当前索引文件夹
string IndexPath = GetCurrentIndexPath("Index");
IndexPath = Server.MapPath("/search/" + IndexPath + "/");
IndexSearcher searchBusiness = new IndexSearcher(IndexPath);
//增量索引,false
IndexWriter iw = new IndexWriter(IndexPath,sa,false); try
{
//根据商户信息删除指定索引(删除所有id相符的索引项)
Term delSearchTerm = new Term("BusinessID", BusinessID.ToString());
IndexReader reader = searchBusiness.GetIndexReader();
reader.DeleteDocuments(delSearchTerm);
reader.Close();
//以上删除只是做了标记,并没有真正删除。还能搜索出来
//查询出需要建立索引的记录
string Sql = @"SELECT DISTINCT BusinessID FROM Business
Sql += "where businessid=" + BusinessID;
//执行SQL
conn.Open();
SqlCommand cmd = new SqlCommand(Sql,conn);
cmd.CommandTimeout=20*60*1000;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
//添加每一条记录到索引文件
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
{
Document docu = new Document();
foreach(DataColumn col in ds.Tables[0].Columns)
{
Field tField;
if (col.ColumnName == "BusinessID" || col.ColumnName == "ReviewTime")
{
//对不需要搜索的字段,它的数字不进行处理
tField =new Field(col.ColumnName, ds.Tables[0].Rows [col.ColumnName].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED);
}else{
//因为这个版本的lucene不支持数字搜索,所以这里要对数字进行处理。
//以及部分特殊符号一并处理
tField = Field.Text(col.ColumnName,ds.Tables[0].Rows[col.ColumnName].ToString().Replace("1","一").Replace("2","二").Replace("3","三").Replace("4","四").Replace("5","五").Replace("6","六").Replace("7","七").Replace("8","八").Replace("9","九").Replace("0","零").Replace("+","+").Replace("-","-").Replace("&","—").Replace("|","|").Replace("!","!").Replace("(","(").Replace(")",")").Replace("{","{").Replace("}","}").Replace("[","[").Replace("]","]").Replace("^","……").Replace("/"","“").Replace("~","~").Replace("*","*").Replace("?","?").Replace(":",":").Replace("//","、").Replace("/","/").Replace("'","‘").Replace(" "," ").Replace(" "," ").ToLower());
}
//增加信息到lucene文档
docu.Add(tField);
}
//将增加的信息写入索引文件
iw.AddDocument(docu);
}
//对索引文件进行优化,这时将刚才删除的所以项正式删除
iw.Optimize();
}catch (Exception ex){
if (ex.GetType().ToString() != "System.Threading.ThreadAbortException")
{
AddError("Build Add Index Fail.");
}
}finally{
iw.Close();
conn.Close();
}
}
}
}
以上是关于增量索引的完整程序和说明,包含了修改数据的索引增量处理。
目前在项目中运行良好,暂时没有出现问题。本程序可直接用于增量和修改增量索引