id int
title nvarchar(50)
description nvarchar(200)
///
/// 查询FilmTab所有信息
///
///
public static DataTable FindFilmTabAll(string id)
{
string where = " where 1=1 ";
if (!string.IsNullOrEmpty(id))
{
where += " and id="+id;
}
string sql = "select id,title,description from FilmTbl" + where;
return BaseInfoDB.GetTable(sql);
}
///
/// 创建索引
///
/// 商品集合
public static void CreateIndex(DataTable dt, string id)
{
if (!System.IO.Directory.Exists(LucenePath))
{
System.IO.Directory.CreateDirectory(LucenePath);
}
//建立分子器
Analyzer analyzer = new StandardAnalyzer();
bool iscreate = string.IsNullOrEmpty(id) ? true : false;//这里很重要哦,lucene默认是生成全部,但是不能填加一条数据也要生成全部吧???所以如果只是更新该参数就是false(不创建
全部)
IndexWriter indexwriter = new IndexWriter(LucenePath, analyzer, iscreate);
for (int i = 0, count = dt.Rows.Count; i < count; i++)
{
Document document = new Document();//创建一行数据,和datarow是相同意思
string Fieldid = dt.Rows[i]["id"].ToString();
St.WriteTextToFile("时间:" + DateTime.Now + ",ID:" + Fieldid + "\t\n", "D:\\luceneDemo\\LuceneDemoControl\\log.txt", true);//填加到文本日志
document.Add(new Field("id", Fieldid, Field.Store.YES, Field.Index.TOKENIZED));//创建字段
document.Add(new Field("title", dt.Rows[i]["title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));//创建字段
document.Add(new Field("description", dt.Rows[i]["description"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));//创建字段
indexwriter.AddDocument(document);
}
indexwriter.Optimize();//lucene优化方法,不建议总是 调用该方法,会影响速度,一天或几天调用一次就好
indexwriter.Close();
}
public static void main(string id)
{
DataTable dt = FilmTabDal.FindFilmTabAll(id);//获取到要存储到lucene的数据集
CreateIndex(dt, id);
//Console.WriteLine("完成");
//Console.Read();
}