高性能書き込みLucene.net

「 - 共有フランシス北京」を、我々は運用・保守グループのttlsa生存時間はフランシスを支援し、共有するために喜んでいる私たちの第二シェアttlsaの姉妹紙である次はここに利点フランシスLucene.netのテキストですが...ありがとう言っても過言、あなたが直接、Baiduの百科事典バーを知りたい場合。http://baike.baidu.com/link?url=BKJ3DBq6c1Mp5xgQKmMbomeFX2ESx9QHOvbLNHrxj6GDJfMP1sjiQj2oCVmueuWqeuI1OEa-99VIw0YwfpoLG_ luceneのは個人的には間違いなく最初のデータベースを作成する必要があり、Luceneの作成データベースを作成するには1.チェックアウトして、前にテーブルを作成したい、理解は単純なテキストデータベースクラスであることを考えます私は今だけ、テスト環境はとても以下の三つを作成しています。
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;
}
この章では、我々はActiveMQのタイムリーluceneのを通じて次の世代について話している、Luceneのを読んだだけの基礎を説明しています。

ます。https://my.oschina.net/766/blog/211254で再現

おすすめ

転載: blog.csdn.net/weixin_34162401/article/details/91547407