/// <summary> /// 删除Videos表信息时 添加删除索引请求至队列 /// </summary> /// <param name="bid"></param> public void Del(int vid) { VideoViewMode bvm = new VideoViewMode(); bvm.Id = vid; bvm.IT = IndexType.Delete; VideoQueue.Enqueue(bvm); }
/// <summary> /// 修改Videos表信息时 添加修改索引(实质上是先删除原有索引 再新增修改后索引)请求至队列 /// </summary> /// <param name="Videos"></param> public void Mod(Video Videos) { VideoViewMode bvm = new VideoViewMode(); bvm.Id = Videos.Id; bvm.Title = Videos.Title; bvm.IT = IndexType.Modify; bvm.About = Videos.About; bvm.Tags = Videos.Tags; bvm.CategoryId = Videos.CategoryId; bvm.BigPicturePath = Videos.BigPicturePath; bvm.SmallPicturePath = Videos.SmallPicturePath; bvm.CreateTime = Videos.CreateTime; bvm.IsOfficial = Videos.IsOfficial; bvm.PlayCount = Videos.PlayCount; bvm.CommentCount = Videos.CommentCount; bvm.VideoState = Videos.VideoState; bvm.CreateManageId = Videos.CreateManageId; VideoQueue.Enqueue(bvm); }
/// <summary> /// 更新索引库操作 /// </summary> private void CRUDIndex() { FSDirectory fsDirectory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); bool isExist = IndexReader.IndexExists(fsDirectory); if (isExist) { if (IndexWriter.IsLocked(fsDirectory)) { IndexWriter.Unlock(fsDirectory); } } IndexWriter writer = new IndexWriter(fsDirectory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED); // writer.SetMergeFactor(30); try { #region 更新索引 while (VideoQueue.Count > 0) { Document document = new Document(); VideoViewMode Video = VideoQueue.Dequeue(); if (Video.IT == IndexType.Insert) { document.Add(new Field("id", Video.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("categoryId", Video.CategoryId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("title", string.IsNullOrEmpty(Video.Title) ? "" : Video.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("about", string.IsNullOrEmpty(Video.About) ? "" : Video.About, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("tags", string.IsNullOrEmpty(Video.Tags) ? "" : Video.Tags, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("videoState", Video.VideoState.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("playCount", Video.PlayCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("commentCount", Video.CommentCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("bigPicturePath", Video.BigPicturePath, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("smallPicturePath", Video.SmallPicturePath, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("createTime", Video.CreateTime.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("createTimeNum", Video.CreateTime.ToString("yyyyMMddHHmmss"), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("createManageId", Video.CreateManageId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("isOfficial", Video.IsOfficial.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); if (Video.IsOfficial == true) { document.Add(new Field("isofficialtrue", LanguageUtil.Translate("api_Business_Search_IndexManager_CRUDIndex_isofficialtrue_Insert"), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } document.GetField("title").SetBoost(2F); writer.AddDocument(document); } else if (Video.IT == IndexType.Delete) { writer.DeleteDocuments(new Term("id", Video.Id.ToString())); } else if (Video.IT == IndexType.Modify) { //先删除 再新增 writer.DeleteDocuments(new Term("id", Video.Id.ToString())); document.Add(new Field("id", Video.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("categoryId", Video.CategoryId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("title", string.IsNullOrEmpty(Video.Title) ? "" : Video.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("about", string.IsNullOrEmpty(Video.About) ? "" : Video.About, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("tags", string.IsNullOrEmpty(Video.Tags) ? "" : Video.Tags, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("videoState", Video.VideoState.ToString(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); document.Add(new Field("playCount", Video.PlayCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("commentCount", Video.CommentCount.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("bigPicturePath", Video.BigPicturePath, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("smallPicturePath", Video.SmallPicturePath, Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("createTimeNum", Video.CreateTime.ToString("yyyyMMddHHmmss"), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("createTime", Video.CreateTime.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("createManageId", Video.CreateManageId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("isOfficial", Video.IsOfficial.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); if (Video.IsOfficial == true) { document.Add(new Field("isofficialtrue", LanguageUtil.Translate("api_Business_Search_IndexManager_CRUDIndex_isofficialtrue_Modify"), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); } //设置索引字段权重 document.GetField("title").SetBoost(2F); writer.AddDocument(document); } } #endregion } catch (Exception ex) { #if !DEBUG LogBuilder.Log4Net.Error(LanguageUtil.Translate("api_Business_Search_IndexManager_CRUDIndex_catch"), ex.MostInnerException()); #else Console.WriteLine("更新索引操作CRUD失败:" + ex.MostInnerException().Message); #endif } finally { writer.Optimize(); //添加完后合并 writer.Close(); fsDirectory.Close(); } }