【www.bbyears.com--全文检索】
Lucene.net介绍
Lucene.net是Lucene的.net移植版本,是一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎。开发人员可以基于Lucene.net实现全文检索的功能。
Lucene.net是Apache软件基金会赞助的开源项目,基于Apache License协议。
Lucene.net并不是一个爬行搜索引擎,也不会自动地索引内容。我们得先将要索引的文档中的文本抽取出来,然后再将其加到Lucene.net索引中。标准的步骤是先初始化一个Analyzer、打开一个IndexWriter、然后再将文档一个接一个地加进去。一旦完成这些步骤,索引就可以在关闭前得到优化,同时所做的改变也会生效。这个过程可能比开发者习惯的方式更加手工化一些,但却在数据的索引上给予你更多的灵活性,而且其效率也很高。
lucene个人认为简单理解就是一个文本类数据库,想要查询肯定要先创建出一个数据库
1、创建lucene,创建前需要创建数据库及表,我现在是测试环境所以只创建了以下三列。
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();
}
2、读取lucene,我们需要创建一个web程序来做测试
/// 通过关键字查询lucene
///
/// 关键字
///
public static DataTable SearchFilmTbl(string key)
{
Analyzer analyzer = new StandardAnalyzer();//创建标准分词器,一定要和生成的lucne生成器一一对应
IndexSearcher indexsearcher = new IndexSearcher(LucenePath);//把写的分词器写好的地址加载进来
QueryParser queryParser = new QueryParser("title", analyzer);//通过title列进行搜索
Query query = queryParser.Parse(key);
//采样
Hits hits = indexsearcher.Search(query);//开始查询
DataTable filmTab = new DataTable();//创建空的datatable
if (hits.Length() > 0)
{
filmTab.Columns.Add("id");//创建datatable的列
filmTab.Columns.Add("title");
filmTab.Columns.Add("description");
for (int i = 0, count = hits.Length(); i < count; i++)
{
Document document = hits.Doc(i);
DataRow dr = filmTab.NewRow();////创建datatable的行
dr["id"] = Convert.ToInt32(document.Get("id"));
dr["title"] = document.Get("title").ToString();
dr["description"] = document.Get("description").ToString();
filmTab.Rows.Add(dr);//添加一行数据
}
}
indexsearcher.Close();
return filmTab;
}
本章只讲述lucene的基础的读取,下次我们讲通过activemq及时生成lucene。