public sealed class Queue_Lucene //利用队列 处理Lucene.Net增加索引
{
private static Queue_Lucene instance = null;
private Queue_Lucene()
{
}
public static Queue_Lucene GetInstance()
{
object o = "";
if (instance == null)
{
lock (o)
{
if (instance == null)
{
instance = new Queue_Lucene();
}
}
}
return instance;
}
private static ConcurrentQueue<IndexModelQueue> queue = new ConcurrentQueue<IndexModelQueue>(); //线程安全的 队列
//添加到队列
public void AddQueue(int id, string content, string title)
{
IndexModelQueue model = new IndexModelQueue();
model.Id = id;
model.Content = content;
model.Title = title;
model.Type = QueueType.Add;
queue.Enqueue(model);
}
//开启线程进行索引的创建
public void StartCreateThread()
{
Thread thread = new Thread(BeginCreate);
thread.IsBackground = true;
thread.Start();
}
private void BeginCreate()
{
while (true)
{
if (queue.Count > 0)
{ //队列有数据,进行处理
CreateIndex();
}
else
{
Thread.Sleep(3000); //当队列空闲时,当前线程睡眠3秒
}
}
}
private void CreateIndex()
{
// string indexPath = HttpContext.Current.Server.MapPath("/Admin/search");//注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。
string indexPath = HostingEnvironment.MapPath("/Admin/search"); //映射到服务器的绝对路径
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 > 0) //一直添加索引,知道队列内无数据
{
IndexModelQueue model = null;
if (queue.TryDequeue(out model))
{
writer.DeleteDocuments(new Term("Id", model.Id.ToString()));//删除与ID相等的文档.
if (model.Type == QueueType.Add) //如果队列内数据类型为ADD 则继续下面操作
{
Document document = new Document();//表示一篇文档。
//Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
document.Add(new Field("Id", model.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", model.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("ContentDescription", model.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);
}
}
}
writer.Close();//会自动解锁。
directory.Close();//不要忘了Close,否则索引结果搜不到
}
}
public class IndexModelQueue
{
public int Id { get; set; }
public string Content { get; set; }
public string Title { get; set; }
public QueueType Type { get; set; }
}
/// <summary>
/// 队列中需要数据类型。Add 增加索引 Delete删除索引
/// </summary>
public enum QueueType
{
Add, Delete
}
}
{
private static Queue_Lucene instance = null;
private Queue_Lucene()
{
}
public static Queue_Lucene GetInstance()
{
object o = "";
if (instance == null)
{
lock (o)
{
if (instance == null)
{
instance = new Queue_Lucene();
}
}
}
return instance;
}
private static ConcurrentQueue<IndexModelQueue> queue = new ConcurrentQueue<IndexModelQueue>(); //线程安全的 队列
//添加到队列
public void AddQueue(int id, string content, string title)
{
IndexModelQueue model = new IndexModelQueue();
model.Id = id;
model.Content = content;
model.Title = title;
model.Type = QueueType.Add;
queue.Enqueue(model);
}
//开启线程进行索引的创建
public void StartCreateThread()
{
Thread thread = new Thread(BeginCreate);
thread.IsBackground = true;
thread.Start();
}
private void BeginCreate()
{
while (true)
{
if (queue.Count > 0)
{ //队列有数据,进行处理
CreateIndex();
}
else
{
Thread.Sleep(3000); //当队列空闲时,当前线程睡眠3秒
}
}
}
private void CreateIndex()
{
// string indexPath = HttpContext.Current.Server.MapPath("/Admin/search");//注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。
string indexPath = HostingEnvironment.MapPath("/Admin/search"); //映射到服务器的绝对路径
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 > 0) //一直添加索引,知道队列内无数据
{
IndexModelQueue model = null;
if (queue.TryDequeue(out model))
{
writer.DeleteDocuments(new Term("Id", model.Id.ToString()));//删除与ID相等的文档.
if (model.Type == QueueType.Add) //如果队列内数据类型为ADD 则继续下面操作
{
Document document = new Document();//表示一篇文档。
//Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
document.Add(new Field("Id", model.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", model.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("ContentDescription", model.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document);
}
}
}
writer.Close();//会自动解锁。
directory.Close();//不要忘了Close,否则索引结果搜不到
}
}
public class IndexModelQueue
{
public int Id { get; set; }
public string Content { get; set; }
public string Title { get; set; }
public QueueType Type { get; set; }
}
/// <summary>
/// 队列中需要数据类型。Add 增加索引 Delete删除索引
/// </summary>
public enum QueueType
{
Add, Delete
}
}