通过队列解决Lucene文件并发创建索引
public sealed class SearchIndexManager
{
private static readonly SearchIndexManager searchIndexManager = new SearchIndexManager();
private SearchIndexManager()
{
}
public static SearchIndexManager GetInstance()
{
return searchIndexManager;
}
Queue<IndexContent> queue = new Queue<IndexContent>();
/// <summary>
/// 向队列中添加数据
/// </summary>
/// <param name="Id"></param>
/// <param name="title"></param>
/// <param name="content"></param>
public void AddQueue(string Id,string title,string content)
{
IndexContent indexContent = new IndexContent();
indexContent.Id = Id;
indexContent.Title = title;
indexContent.Content = content;
indexContent.LuceneEnum = Model.Enum.LuceneEnum.AddType;// 添加
queue.Enqueue(indexContent);
}
/// <summary>
/// 向队列中添加要删除数据
/// </summary>
/// <param name="Id"></param>
public void DeleteQueue(string Id)
{
IndexContent indexContent = new IndexContent();
indexContent.Id = Id;
indexContent.LuceneEnum = Model.Enum.LuceneEnum.DeleType;//删除
queue.Enqueue(indexContent);
} /// <summary>
/// 开启线程,扫描队列,从队列中获取数据
/// </summary>
public void StartThread()
{
Thread myThread = new Thread(WriteIndexContent);
myThread.IsBackground = true;
myThread.Start();
}
private void WriteIndexContent()
{
while (true)
{
if (queue.Count > )
{
CreateIndexContent();
}
else
{
Thread.Sleep();//避免造成CPU空转
}
}
}
private void CreateIndexContent()
{
string indexPath = @"C:\lucenedir";//注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。一定将路径名称写到web.config文件中
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());//指定索引文件(打开索引目录) FS指的是就是FileSystem
bool isUpdate = IndexReader.IndexExists(directory);//IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。
if (isUpdate)
{
//同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
if (IndexWriter.IsLocked(directory))
{
IndexWriter.Unlock(directory);
}
}
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引。这时在这里加锁。 while(queue.Count>)
{
IndexContent indexContent=queue.Dequeue();//将队列中的数据出队
writer.DeleteDocuments(new Term("Id", indexContent.Id.ToString()));
if (indexContent.LuceneEnum == Model.Enum.LuceneEnum.DeleType)
{
continue;
}
Document document = new Document();//表示一篇文档。
//Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
document.Add(new Field("Id", indexContent.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); //Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询) //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
document.Add(new Field("Title", indexContent.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("Content", indexContent.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS)); writer.AddDocument(document);
} writer.Close();//会自动解锁。
directory.Close();//不要忘了Close,否则索引结果搜不到
}
}
public enum LuceneEnum
{
/// <summary>
/// 添加
/// </summary>
AddType,
/// <summary>
/// 删除
/// </summary>
DeleType
}
enum LuceneEnum
public class IndexContent
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public LuceneEnum LuceneEnum { get; set; }
}
IndexContent
private static readonly SearchIndexManager searchIndexManager = new SearchIndexManager();要采用单例模式
private SearchIndexManager()
{
}
public static SearchIndexManager GetInstance()
{
return searchIndexManager;
}
在添加、删除需要检索的表单时调用队列的增加、删除
在Global调用
protected void Application_Start()
{
SearchIndexManager.GetInstance().StartThread();//开启线程扫描队列将数据取出来写到Lucene.NET中。
通过队列解决Lucene文件并发创建索引的更多相关文章
- Lucene.net 从创建索引到搜索的代码范例
关于Lucene.Net的介绍网上已经很多了在这里就不多介绍Lucene.Net主要分为建立索引,维护索引和搜索索引Field.Store的作用是通过全文检查就能返回对应的内容,而不必再通过id去DB ...
- Lucene 4.7 --创建索引
Lucene的最新版本和以前的语法或者类名,类规定都相差甚远 0.准备工作: 1). Lucene官方API http://lucene.apache.org/core/4_7_0/index.htm ...
- Oracle 并发创建索引
建索引时,我们为了建索引快,会加上并行,加上并行之后,此列索引就会是并行了.访问有并行度的索引时,CBO可能可能会考虑并行执行,这可能会引发一些问题,如在服务器资源紧张的时候用并行会引起更加严重的争用 ...
- Apache Lucene(全文检索引擎)—创建索引
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
- Lucene简介和创建索引初步
Lucene的使用 在全文索引工具中,都是由这样三部分组成 1:索引部分 2:分词部分 3:搜索部分
- 使用Lucene对预处理后的文档进行创建索引(可执行)
时间: 2015/3/18 杨鑫newlife 对于文档的预处理后.就要開始使用Lucene来处理相关的内容了. 这里使用的Lucene的过程例如以下: 首先要为处理对象机那里索引 二是构建查询对象 ...
- lucene简介 创建索引和搜索初步
lucene简介 创建索引和搜索初步 一.什么是Lucene? Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引 ...
- Lucene的配置及创建索引全文检索
Lucene 是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎(英文与德文两种西方语言).Lucene ...
- lucene创建索引的几种方式(一)
什么是索引: 根据你输入的值去找,这个值就是索引 第一种创建索引的方式: 根据文件来生成索引,如后缀为.txt等的文件 步骤: 第一步:FSDirectory.open(Paths.get(url)) ...
随机推荐
- FP error code老是忘记的看这里:只给出最常用的几个。
把常见的几个记牢,不要在比赛时纠结. 错误2:输入文件未找到. 错误106:数据读入的格式错误,往往是读入语句出错. 错误200:被零除. 错误201:范围检查错误,数组越界. 错误202:栈溢出. ...
- 如何提高Java并行程序性能??
在Java程序中,多线程几乎已经无处不在.与单线程相比,多线程程序的设计和实现略微困难,但通过多线程,我们却可以获得多核CPU带来的性能飞跃,从这个角度说,多线程是一种值得尝试的技术.那么如何写出高效 ...
- requirejs:研究笔记
模块化历史 模块化异步加载方式 后期维护 查找问题 复用代码 防止全局变量的污染 http://requirejs.cn/ http://requirejs.org/ 我的目录结构 总体步骤 < ...
- php变量和数组大小限制
前言:shopnc在默认拼接sql的时候会带上limit 1000 那么问题就来了,如果在使用系统的封装的方法,但是如果你没有带上->limit(false)就完蛋了 那么问题来了,在判断时候, ...
- java常用集合框架底层实现简介与注意点
Collection: ArrayList:1:底层实现是数组,默认长度是10.2:add(),判断是否数组越界,是数组扩容为原来的两倍.3:remove(),copy数组,size-1,释放空虚的空 ...
- Gerrit与Gitlab同步配置replication&其他配置
一.Gerrit与Gitlab同步配置 当配置好gerrit环境后,还需要与现有gitlab库进行同步配置,否则会影响现有开发与打包流程. 1.安装gerrit replication插件 unzip ...
- rem的使用
浏览器的默认字体高是16px. 兼容性: 目前,IE9+,Firefox.Chrome.Safari.Opera 的主流版本都支持了rem. 对于不支持的浏览器,要多写一个绝对单位的声明,这样浏览器就 ...
- LeetCode Moving Average from Data Stream
原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...
- 使用Canvas绘制背景图
原文 http://www.imququ.com/post/use-canvas-as-background-image.html 最近iCloud Web的Beta版换了UI,整体风格变得和iOS ...
- iOS,应用崩溃日志分析
参考资料:http://www.cocoachina.com/industry/20130725/6677.html 1.获得崩溃日志 2.崩溃日志实例 3.符号化崩溃日志 4.低内存闪退 获得崩溃日 ...