1.索引的追加
索引的追加是比较耗时的,如果等执行完毕再向用户返回结果,长时间的等待会使用户崩溃……。所以此处我们使用消息队列,保证第一时间响应用户,其他的耗时操作交给消息队列吧。
1 protected void AddIndex_Click(object sender, EventArgs e) 2 { 3 //search. AddIndex(); 4 string Insertstr = "insert into Article (Title,Contents,Time) values ('天天就要向上','天天就要向上','" + DateTime.Now + "') select @@identity "; 5 int id = Convert.ToInt32((DbHelperSQL.GetSingle(Insertstr))); 6 China.Model.LuceneIndexArticle ar = new China.Model.LuceneIndexArticle(); 7 ar.ID = id; 8 ar.Title = "天天就要向上"; 9 ar.Contents = "天天就要向上"; 10 ar.AddTime = DateTime.Now.ToString(); 11 IndexManage.NewsIndex.IndexAdd(ar); 12 }
1 /// <Summary> 2 /// 新增News时,添加新增索引到请求队列 3 /// </Summary> 4 /// <param name="artice"></param> 5 public void IndexAdd(China.Model.LuceneIndexArticle artice) 6 { 7 ArticeViewModel addartic = new ArticeViewModel(); 8 addartic.DocId = artice.ID; 9 addartic.Title = artice.Title; 10 addartic.HandlerType = IndexType.Insert; 11 addartic.Content = artice.Contents; 12 newsQueue.Enqueue(addartic); 13 }
2.删除一行数据,删除对应的索引
1 protected void Delete_oneIndex_Click(object sender, EventArgs e) 2 { 3 string delestr = "delete from Article where ID=" + 1386; 4 DbHelperSQL.ExecuteSql(delestr); 5 IndexManage.NewsIndex.IndexDel(1386); 6 }
1 /// <Summary> 2 /// 删除数据时,添加删除请求到消息队列 3 /// </Summary> 4 /// <param name="artic"></param> 5 public void IndexDel(int DocId) 6 { 7 ArticeViewModel delartic = new ArticeViewModel(); 8 delartic.OnlyKey = DocId.ToString(); 9 delartic.HandlerType = IndexType.Delete; 10 newsQueue.Enqueue(delartic); 11 }
3.修改索引
1 string upstr = "update News set Title='笑嘻嘻笑嘻嘻',Content='笑嘻嘻笑嘻嘻系' where DocId=" + 146475; 2 DbHelperSQL.ExecuteSql(upstr); 3 var wacth = Stopwatch.StartNew(); 4 China.Model.LuceneIndexArticle ar = new China.Model.LuceneIndexArticle(); 5 ar.ID = 146475; 6 ar.Title = "笑嘻嘻笑嘻嘻"; 7 ar.Contents = "笑嘻嘻笑嘻嘻"; 8 ar.AddTime = DateTime.Now.ToString(); 9 IndexManage.NewsIndex.IndexAdd(ar);
1 /// <Summary> 2 /// 对原有所以进行修改 3 /// </Summary> 4 /// <param name="artic"></param> 5 public void IndexModify(China.Model.LuceneIndexArticle artic) 6 { 7 ArticeViewModel Modartic = new ArticeViewModel(); 8 Modartic.DocId = artic.ID; 9 Modartic.Title = artic.Title; 10 Modartic.Content = artic.Contents; 11 Modartic.HandlerType = IndexType.Modify; 12 newsQueue.Enqueue(Modartic); 13 }
4.下面我们来看在消息队列中,追加、删除、修改索引的具体操作
1 FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexpath), new NativeFSLockFactory()); 2 bool isExsit = IndexReader.IndexExists(directory); 3 if (isExsit) 4 { 5 if (IndexWriter.IsLocked(directory)) 6 { 7 IndexWriter.Unlock(directory); 8 } 9 } 10 IndexWriter writer = new IndexWriter(directory, PanGuAnalyzer, false); 11 //IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExsit, IndexWriter.MaxFieldLength.LIMITED); 12 while (newsQueue.Count > 0) 13 { 14 Document document = new Document(); 15 ArticeViewModel news = newsQueue.Dequeue(); 16 if (news.HandlerType == IndexType.Insert) 17 { 18 document.Add(new Field("ID", news.DocId.ToString(), Field.Store.YES, Field.Index.ANALYZED)); 19 document.Add(new Field("Title", news.Title, Field.Store.YES, Field.Index.ANALYZED)); 20 document.Add(new Field("Contents", news.Content, Field.Store.YES, Field.Index.ANALYZED)); 21 document.Add(new Field("OnlyKey", news.OnlyKey, Field.Store.YES, Field.Index.ANALYZED)); 22 writer.AddDocument(document); 23 } 24 else if (news.HandlerType == IndexType.Delete) 25 { 26 writer.DeleteDocuments(new Term("OnlyKey", "News1388")); 27 } 28 else if (news.HandlerType == IndexType.Modify) 29 { 30 //先删除 再新增 31 writer.DeleteDocuments(new Term("OnlyKey", news.OnlyKey)); 32 document.Add(new Field("OnlyKey", news.OnlyKey, Field.Store.YES, Field.Index.NOT_ANALYZED)); 33 document.Add(new Field("Title", news.Title, Field.Store.YES, Field.Index.ANALYZED, 34 Field.TermVector.WITH_POSITIONS_OFFSETS)); 35 document.Add(new Field("Contnets", news.Content, Field.Store.YES, Field.Index.ANALYZED, 36 Field.TermVector.WITH_POSITIONS_OFFSETS)); 37 writer.AddDocument(document); 38 } 39 } 40 writer.Optimize(); 41 writer.Close(); 42 directory.Close();