private void IndexContent(SearchContent searchContent, IndexAction action) { // Index string indexDir = Context.Server.MapPath(Config.GetConfiguration()["SearchIndexDir"]); IndexBuilder ib = new IndexBuilder(indexDir, false); switch (action) { case IndexAction.Create: ib.AddContent(searchContent); break; case IndexAction.Update: ib.UpdateContent(searchContent); break; case IndexAction.Delete: ib.DeleteContent(searchContent); break; } ib.Close(); }
/// <summary> /// Builds the index. /// </summary> /// <param name="rebuild">if set to <c>true</c> [rebuild].</param> public override void BuildIndex(bool rebuild) { OnEvent(String.Format("CatalogIndexBuilder Started"), 0); IndexBuilder indexer = this.Indexer; DateTime lastBuild = DateTime.MinValue; // Parameters used to restart build from the previous error //string restartCatalogName = String.Empty; //int restartRecordKey = 0; // On complete rebuild no need to check date if (!rebuild) { lastBuild = indexer.GetBuildConfig().LastBuildDate; } ICatalogSystem system = CatalogContext.Current; // Get catalog lists CatalogDto catalogs = system.GetCatalogDto(); double percentage = 0; bool incremental = false; int allRecords = 0; int allCurrentRecord = 0; int allRecordsCount = GetTotalRecords(); int catalogCount = 1; // If date is set, we do incremental rebuild if (lastBuild != DateTime.MinValue) { incremental = true; } // remove deleted items first if (incremental) { int startingRecord = 0; int totalRemoved = 0; while (true) { int count = 0; LogDto dto = LogManager.GetAppLog("catalog", DataRowState.Deleted.ToString(), "entry", lastBuild.ToUniversalTime(), startingRecord, 100, ref count); // add up to a total number to calculate percentage correctly if (startingRecord == 0) { allRecordsCount += count; } if (count <= 0) { break; } foreach (LogDto.ApplicationLogRow row in dto.ApplicationLog) { allCurrentRecord++; if (allCurrentRecord % 20 == 0) { percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("Removing old entry from index ({1}/{2}) ...", allCurrentRecord, count), percentage); } totalRemoved += indexer.DeleteContent("_id", row.ObjectKey); } startingRecord += 100; } percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("CatalogIndexBuilder Removed {0} records.", totalRemoved), percentage); } // Index new or updated items foreach (CatalogDto.CatalogRow catalog in catalogs.Catalog) { string catalogName = catalog.Name; string defaultCurrency = catalog.DefaultCurrency; OnEvent(String.Format("Indexing {0} catalog ...", catalogName), percentage); int currentRecord = 0; int startingRecord = 0; int totalIndexed = 0; int lastRecordKey = 0; catalogCount++; while (true) { // Get Catalog Nodes CatalogSearchParameters pars = new CatalogSearchParameters(); CatalogSearchOptions options = new CatalogSearchOptions(); options.CacheResults = false; pars.CatalogNames.Add(catalogName); options.RecordsToRetrieve = 100; options.StartingRecord = startingRecord; if (incremental) { pars.SqlMetaWhereClause = String.Format("META.Modified > '{0}'", lastBuild.ToUniversalTime().ToString("s")); } int count = 0; CatalogEntryDto entryDto = CatalogContext.Current.FindItemsDto(pars, options, ref count, new CatalogEntryResponseGroup(CatalogEntryResponseGroup.ResponseGroup.CatalogEntryFull)); if (count <= 0) { break; } startingRecord += options.RecordsToRetrieve; List <string> languages = new List <string>(); languages.Add(catalog.DefaultLanguage); foreach (CatalogDto.CatalogLanguageRow row in catalog.GetCatalogLanguageRows()) { languages.Add(row.LanguageCode); } foreach (CatalogEntryDto.CatalogEntryRow row in entryDto.CatalogEntry) { currentRecord++; allCurrentRecord++; // Do not index non active entries if (!row.IsActive) { continue; } // In case of incremental, check if item already exists and delete it if (incremental) { indexer.DeleteContent("_id", row.CatalogEntryId.ToString()); } try { lastRecordKey = row.CatalogEntryId; totalIndexed += IndexCatalogEntryDto(indexer, row, defaultCurrency, languages.ToArray()); } catch (Exception) { percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("Failed to index catalog entry {0}({1}) in {2}.", row.Name, row.CatalogEntryId, catalogName), percentage); throw; } if (allCurrentRecord % 20 == 0) { percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("Indexing {0} catalog entries ({1}/{2}) ...", catalogName, allCurrentRecord, allRecordsCount), percentage); } } // Save index every 500 records if (allCurrentRecord % 500 == 0) { percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("Saving {0} catalog index file.", catalogName), percentage); Build config = indexer.GetBuildConfig(); // Preserve the information needed to restart the indexing on the exact same location as before BuildProperty prop1 = new BuildProperty(); prop1.name = "StartingRecord"; prop1.value = startingRecord.ToString(); BuildProperty prop2 = new BuildProperty(); prop2.name = "LastRecordKey"; prop2.value = lastRecordKey.ToString(); BuildProperty prop3 = new BuildProperty(); prop3.name = "CatalogName"; prop3.value = catalogName; config.Properties = new BuildProperty[] { prop1, prop2, prop3 }; indexer.SaveBuild(Status.Started); indexer.Close(); } if (startingRecord > count) { break; } } percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; catalogCount++; allRecords += totalIndexed; } OnEvent(String.Format("CatalogIndexBuilder Indexed {0} language records in {1} catalog(s)", allRecords.ToString(), (catalogCount - 1).ToString()), 99); OnEvent(String.Format("CatalogIndexBuilder Finished"), 100); }
/// <summary> /// Builds the index. /// </summary> /// <param name="rebuild">if set to <c>true</c> [rebuild].</param> public override void BuildIndex(bool rebuild) { OnEvent(String.Format("ProfileIndexBuilder Started"), 0); IndexBuilder indexer = this.Indexer; DateTime lastBuild = DateTime.MinValue; // On complete rebuild no need to check date if (!rebuild) { lastBuild = indexer.GetBuildConfig().LastBuildDate; } double percentage = 0; bool incremental = false; int allRecords = 0; int allCurrentRecord = 0; int allRecordsCount = GetTotalRecords(); // If date is set, we do incremental rebuild if (lastBuild != DateTime.MinValue) { incremental = true; } int startingRecord = 0; // remove deleted items first if (incremental) { int totalRemoved = 0; while (true) { int count = 0; LogDto dto = LogManager.GetAppLog("profile", DataRowState.Deleted.ToString(), "ACCOUNT", lastBuild.ToUniversalTime(), startingRecord, 100, ref count); // add up to a total number to calculate percentage correctly if (startingRecord == 0) { allRecordsCount += count; } if (count <= 0) { break; } foreach (LogDto.ApplicationLogRow row in dto.ApplicationLog) { allCurrentRecord++; if (allCurrentRecord % 20 == 0) { percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("Removing old profile from index ({1}/{2}) ...", allCurrentRecord, count), percentage); } totalRemoved += indexer.DeleteContent("_id", row.ObjectKey); } startingRecord += 100; } percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("ProfileIndexBuilder Removed {0} records.", totalRemoved), percentage); } int currentRecord = 0; startingRecord = 0; int totalIndexed = 0; while (true) { ProfileSearchParameters pars = new ProfileSearchParameters(); ProfileSearchOptions options = new ProfileSearchOptions(); options.CacheResults = false; options.RecordsToRetrieve = 100; options.StartingRecord = startingRecord; if (incremental) { pars.SqlMetaWhereClause = String.Format("META.Modified > '{0}'", lastBuild.ToUniversalTime().ToString("s")); } int count = 0; Account[] accounts = ProfileContext.Current.FindAccounts(pars, options, out count); if (accounts == null) { break; } if (count <= 0) { break; } startingRecord += options.RecordsToRetrieve; foreach (Account account in accounts) { currentRecord++; allCurrentRecord++; // In case of incremental, check if item already exists and delete it if (incremental) { indexer.DeleteContent("_id", account.PrincipalId.ToString()); } totalIndexed += IndexAccount(indexer, account); if (allCurrentRecord % 20 == 0) { percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; OnEvent(String.Format("Indexing profiles ({0}/{1}) ...", allCurrentRecord, allRecordsCount), percentage); } } if (startingRecord > count) { break; } } percentage = ((double)allCurrentRecord / (double)allRecordsCount) * 100; allRecords += totalIndexed; OnEvent(String.Format("ProfileIndexBuilder Indexed {0} records", allRecords.ToString()), 99); OnEvent(String.Format("ProfileIndexBuilder Finished"), 100); }