文章内容
2017/7/28 15:21:06,作 者: 黄兵
Lucene.Net 建立索引
在5月19日的时候,研究了Lucene.Net。
随着时间的推移,文章越来越多,以前检索是采用like '%%' 的方式,文章越多检索越慢。
之后改用Lucene.Net,加快检索速度。但是以前的文章就没有建立索引,一直没有时间为以前的文章建立索引。
今天有时间了,研究了一下,把我的代码共享出来。
首先说一下我的想法:
- 选取没有建立索引文章的id;
- 根据文章的id,遍历文章内容;
- 选取要建立索引内容的字段,建立索引。
代码如下:
[HttpGet]
public ActionResult CreateIndex()
{
using (UnitOfWork uow = new UnitOfWork())
{
var getBlogItem = uow.BlogRepository.GetAll().Where(x => x.Id < 645).ToList();
foreach (var item in getBlogItem.Select(x => x.Id))
{
try
{
//单个查询
Blog blogModel = new Blog();
blogModel.Id = getBlogItem.Where(x => x.Id == item).FirstOrDefault().Id;
blogModel.Title = getBlogItem.Where(x => x.Id == item).FirstOrDefault().Title;
blogModel.Body = getBlogItem.Where(x => x.Id == item).FirstOrDefault().Body;
blogModel.CreationTime = getBlogItem.Where(x => x.Id == item).FirstOrDefault().CreationTime;
SearchIndexManager.GetInstance().AddQueue(blogModel.Id.ToString(), blogModel.Title, blogModel.Body, blogModel.CreationTime);
ViewData["info"] = @"<div class='alert alert-success alert-dismissable'>\r\n<button aria-hidden='true' data-dismiss='alert' class='close' type='button'>×</button>\r\n 建立索引成功! <a class='alert-link' href='notifications.html#'>了解更多</a>.</div>";
}
catch (Exception ex)
{
ViewData["info"] = @"<div class='alert alert-danger alert-dismissable'>\r\n<button aria-hidden='true' data-dismiss='alert' class='close' type='button'>×</button>\r\n 建立索引失败! <a class='alert-link' href='notifications.html#'>了解更多</a>.</div>";
}
}
}
return View();
}
好了,其他没有什么重点,六百多篇文章,建立索引大概用了4分多,速度还可以。
有什么问题给我在下面留言。
黄兵的个人博客原创。
转载请注明出处:黄兵的个人博客 - Lucene.Net 建立索引
评论列表