public ActionResult Edit(SampleData data) { if (data.Id == 0) { ViewData["msg"] = "Id不能为空"; } else if (string.IsNullOrEmpty(data.Name)) { ViewData["msg"] = "Name不能为空"; } else if (string.IsNullOrEmpty(data.Description)) { ViewData["msg"] = "Description不能为空"; } else { if (GoLucene.Search(data.Id.ToString(), "Id").Any()) { ViewData["msg"] = "Id已存在"; } else { IndexQueue.GetInstance().AddQueue(new Data { SampleData = data, OptionType = IndexOptionType.Add }); } } return(View()); }
/// <summary> /// 删除Books表信息时 添加删除索引请求至队列 /// </summary> /// <param name="bid"></param> public void Del(int bid) { IndexQueue bvm = new IndexQueue(); bvm.Id = bid; bvm.IT = IndexType.Delete; starUserQueue.Enqueue(bvm); }
/// <summary> /// 修改Books表信息时 添加修改索引(实质上是先删除原有索引 再新增修改后索引)请求至队列 /// </summary> /// <param name="books"></param> public void Mod(StarUserInfo user) { IndexQueue bvm = new IndexQueue(); bvm.Id = user.Id; bvm.KeyWords = user.RealName; bvm.IT = IndexType.Modify; starUserQueue.Enqueue(bvm); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); log4net.Config.XmlConfigurator.ConfigureAndWatch( new System.IO.FileInfo(Path.Combine(Server.MapPath("~"), "log4net.config"))); //设置lucene索引文件的地址 GoLucene._luceneDir = Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath("/App_Data"), @"lucene_index"); //启动队列处理队列中的数据 IndexQueue.GetInstance().StartThread(); }
/// <summary> /// 更新索引库操作 /// </summary> private void CRUDIndex() { FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath + "\\starUser"), new NativeFSLockFactory()); bool isExist = IndexReader.IndexExists(directory); if (isExist) { if (IndexWriter.IsLocked(directory)) { IndexWriter.Unlock(directory); } } IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExist, IndexWriter.MaxFieldLength.UNLIMITED); while (starUserQueue.Count > 0) { Document document = new Document(); IndexQueue indexInfo = starUserQueue.Dequeue(); if (indexInfo.IT == IndexType.Insert) { document.Add(new Field("id", indexInfo.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("title", indexInfo.KeyWords, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); writer.AddDocument(document); } else if (indexInfo.IT == IndexType.Delete) { writer.DeleteDocuments(new Term("id", indexInfo.Id.ToString())); } else if (indexInfo.IT == IndexType.Modify) { //先删除 再新增 writer.DeleteDocuments(new Term("id", indexInfo.Id.ToString())); document.Add(new Field("id", indexInfo.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); document.Add(new Field("title", indexInfo.KeyWords, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS)); writer.AddDocument(document); } } writer.Close(); directory.Close(); }