教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 用Lucene.net如何实现高性能读写的方法实例

用Lucene.net如何实现高性能读写的方法实例

发布时间:2016-11-29   编辑:jiaochengji.com
教程集为您提供用Lucene.net如何实现高性能读写的方法实例等资源,欢迎您收藏本站,我们将为您提供最新的用Lucene.net如何实现高性能读写的方法实例资源
Lucene.net是Lucene的.net移植版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,在数据的索引上给予你更多的灵活性,而且其效率也很高。

Lucene.net介绍

Lucene.net是Lucene的.net移植版本,是一个开源的全文检索引擎开发包,即它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎。开发人员可以基于Lucene.net实现全文检索的功能。

Lucene.net是Apache软件基金会赞助的开源项目,基于Apache License协议。

Lucene.net并不是一个爬行搜索引擎,也不会自动地索引内容。我们得先将要索引的文档中的文本抽取出来,然后再将其加到Lucene.net索引中。标准的步骤是先初始化一个Analyzer、打开一个IndexWriter、然后再将文档一个接一个地加进去。一旦完成这些步骤,索引就可以在关闭前得到优化,同时所做的改变也会生效。这个过程可能比开发者习惯的方式更加手工化一些,但却在数据的索引上给予你更多的灵活性,而且其效率也很高。


lucene个人认为简单理解就是一个文本类数据库,想要查询肯定要先创建出一个数据库

1、创建lucene,创建前需要创建数据库及表,我现在是测试环境所以只创建了以下三列。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1872')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1872>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();
}</td></tr></table>



2、读取lucene,我们需要创建一个web程序来做测试

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5345')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5345>/// <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;
}</td></tr></table>



本章只讲述lucene的基础的读取,下次我们讲通过activemq及时生成lucene。

您可能感兴趣的文章:
用Lucene.net如何实现高性能读写的方法实例
jieba.NET中文分词及jieba.NET与Lucene.Net的集成
lucene入门简单实现
如何保证消息队列的高可用?
mysql中的锁、事务、并发控制的相关知识
如何成为php架构师
Python property函数:定义属性
彻底搞懂Python中的类
PHP抽象类和接口示例和区别
Redis 单数据多源超高并发下的解决方案

[关闭]
~ ~