/// <summary> /// 根据网站id创建内容索引 /// </summary> public static void CreateIndex(string wenSiteIds, string wenSiteShortName) { try { if (!new WebSiteApp().IsSearch(wenSiteIds) || !new WebSiteApp().IsMSearch(wenSiteIds)) { throw new Exception("该站点未启用全站搜索功能!"); } if (new WebSiteApp().IsOverSize(wenSiteShortName)) { throw new Exception("该站点空间已不足,请联系管理员!"); } if (!string.IsNullOrEmpty(wenSiteShortName)) { ContentApp contentApp = new ContentApp(); LUCENCEINDEXPATH = string.Format(LUCENCEINDEXPATH, wenSiteShortName); INDEX_DIR = new DirectoryInfo(LUCENCEINDEXPATH); List <ContentEntity> contents = contentApp.GetListByWebSiteId(wenSiteIds); using (IndexWriter iw = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(INDEX_DIR), analyzer, true, IndexWriter.MaxFieldLength.LIMITED)) { foreach (ContentEntity content in contents) { Document doc = new Document(); if (content.Id != null) { doc.Add(new Field("Id", content.Id, Field.Store.YES, Field.Index.ANALYZED)); if (content.FullName != null) { doc.Add(new Field("FullName", content.FullName, Field.Store.YES, Field.Index.ANALYZED)); } if (content.Author != null) { doc.Add(new Field("Author", content.Author, Field.Store.YES, Field.Index.ANALYZED)); } if (content.Content != null) { doc.Add(new Field("Content", content.Content, Field.Store.YES, Field.Index.ANALYZED)); } } iw.AddDocument(doc); } iw.Commit(); iw.Optimize(); iw.Dispose(); } } } catch (Exception ex) { throw ex; } }
/// <summary> /// 根据网站id创建内容索引 /// </summary> public static void CreateIndex(string wenSiteIds) { if (!string.IsNullOrEmpty(wenSiteIds)) { WebSiteApp webSiteApp = new WebSiteApp(); ContentApp contentApp = new ContentApp(); WebSiteEntity webSite = webSiteApp.GetFormNoDel(wenSiteIds); if (webSite != null) { LUCENCEINDEXPATH = string.Format(LUCENCEINDEXPATH, webSite.ShortName); INDEX_DIR = new DirectoryInfo(LUCENCEINDEXPATH); List <ContentEntity> contents = contentApp.GetListByWebSiteId(wenSiteIds); IndexWriter iw = new IndexWriter(Lucene.Net.Store.FSDirectory.Open(INDEX_DIR), analyzer, true, IndexWriter.MaxFieldLength.LIMITED); foreach (ContentEntity content in contents) { Document doc = new Document(); if (content.Id != null) { doc.Add(new Field("Id", content.Id, Field.Store.YES, Field.Index.ANALYZED)); if (content.FullName != null) { doc.Add(new Field("FullName", content.FullName, Field.Store.YES, Field.Index.ANALYZED)); } if (content.Author != null) { doc.Add(new Field("Author", content.Author, Field.Store.YES, Field.Index.ANALYZED)); } if (content.Content != null) { doc.Add(new Field("Content", content.Content, Field.Store.YES, Field.Index.ANALYZED)); } } iw.AddDocument(doc); } iw.Commit(); iw.Optimize(); iw.Dispose(); } } }