/// <summary> /// Rebuild Index /// </summary> private void RebuildIndex(List <Record_Language> recordLanguages, string indexPath) { var defaultLanguageID = AppSettingsUtility.GetGuid(AppSettingsKeys.DefaultLanguageID); Guid descriptionVariableID = new Guid(AppSettingsUtility.GetString(AppSettingsKeys.DescriptionVariableID)); foreach (var recordLanguage in recordLanguages) { SiteSearchItem siteSearchItem = new SiteSearchItem(); siteSearchItem.ID = recordLanguage.ID.ToString(); siteSearchItem.Title = recordLanguage.Name; var descriptionVariable = recordLanguage.Record.Variables.Where(r => r.VariableID == descriptionVariableID && r.LanguageID == defaultLanguageID).FirstOrDefault(); if (descriptionVariable != null) { siteSearchItem.Description = descriptionVariable.Value; } siteSearchItem.URL = new RecordData().GetRecordUrl(recordLanguage.ID, recordLanguage.Name); siteSearchItem.IsDeleted = recordLanguage.Record.IsDeleted || !recordLanguage.Record.IsActive; SiteSearchService.SaveToIndex(siteSearchItem, indexPath); recordProcessed++; } }
/// <summary> /// Save to Index /// </summary> /// <param name="item"></param> /// <param name="indexPath"></param> public static bool SaveToIndex(SiteSearchItem item, string indexPath) { bool isSuceess = false; try { //Delete First DeleteIndex(item, indexPath); if (item.IsDeleted) { return(true); } //Add to index var indexDirectory = FSDirectory.Open(new DirectoryInfo(indexPath)); var analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30); var writer = new Lucene.Net.Index.IndexWriter(indexDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED); var doc = new Document(); doc.Add(new Field("ID", item.ID, Field.Store.YES, Field.Index.NOT_ANALYZED)); //Set Boost in title var titleField = new Field("Title", item.Title, Field.Store.YES, Field.Index.ANALYZED); titleField.Boost = 1.3f; doc.Add(titleField); //Set Boost in Description var descriptionField = new Field("Description", item.Description, Field.Store.YES, Field.Index.ANALYZED); descriptionField.Boost = 1.1f; doc.Add(descriptionField); //Default boost 1f in Document Description doc.Add(new Field("DocumentDescription", item.DocumentDescription, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("URL", item.URL, Field.Store.YES, Field.Index.NOT_ANALYZED)); writer.AddDocument(doc); analyzer.Close(); writer.Dispose(); isSuceess = true; } catch (Exception ex) { ErrorLog.WriteLog("SiteSearchService", "SaveToIndex", ex, string.Empty); } return(isSuceess); }
/// <summary> /// Delete Index /// </summary> /// <param name="item"></param> private static void DeleteIndex(SiteSearchItem item, string indexPath) { try { var indexDirectory = FSDirectory.Open(new DirectoryInfo(indexPath)); IndexReader reader = IndexReader.Open(indexDirectory, false); reader.DeleteDocuments(new Term("ID", item.ID)); reader.Dispose(); } catch (Exception ex) { ErrorLog.WriteLog("SiteSearchService", "DeleteIndex", ex, string.Empty); } }
/// <summary> /// Map Lucene Document to Data /// </summary> /// <param name="doc"></param> /// <returns></returns> private static SiteSearchItem MapLuceneDocumentToData(Document doc) { SiteSearchItem item = new SiteSearchItem(); item.ID = doc.Get("ID"); item.Title = doc.Get("Title"); var description = doc.Get("Description"); description = description.StripHTMLTagsRegex(); description = description.StripText(300); item.Description = description; item.DocumentDescription = doc.Get("DocumentDescription"); item.URL = doc.Get("URL"); return(item); }
/// <summary> /// Save To Index /// </summary> /// <param name="item"></param> /// <param name="indexPath"></param> /// <returns></returns> public static void SaveToIndexAsync(SiteSearchItem item, string indexPath) { Task.Run(() => SaveToIndex(item, indexPath)); }