Lucene.net实现高性能读写

首先感谢”北京 – 晴晴的分享”, 咱们ttlsa运维生存时间群都乐于帮助和分享. 晴晴是咱们ttlsa第二位分享的妹纸. 以下是晴晴的原文. Lucene.net的优点在这里就不过多的说了,如果想了解直接百度百科吧。 http://baike.baidu.com/link?url=BKJ3DBq6c1Mp5xgQKmMbomeFX2ESx9QHOvbLNHrxj6GDJfMP1sjiQj2oCVmueuWqeuI1OEa-99VIw0YwfpoLG_ lucene个人认为简单理解就是一个文本类数据库,想要查询肯定要先创建出一个数据库 1、创建lucene,创建前需要创建数据库及表,我现在是测试环境所以只创建了以下三列。
id int
 title nvarchar(50)
 description nvarchar(200)
/// <summary>
/// 查询FilmTab所有信息
/// </summary>
/// <returns></returns>
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);
}

/// <summary>
/// 创建索引
/// </summary>
/// <param name="list">商品集合</param>
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程序来做测试
/// <summary>
/// 通过关键字查询lucene
/// </summary>
/// <param name="key">关键字</param>
/// <returns></returns>
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。

转载于:https://my.oschina.net/766/blog/211254

猜你喜欢

转载自blog.csdn.net/weixin_34162401/article/details/91547407