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>
        /// Update the search index with the contents of the section.
        /// </summary>
        /// <param name="section"></param>
        public static void UpdateIndexFromSection(Section section)
        {
            // Get ModuleLoader from the container. This needs to happen explicit here because
            // this is a static method
            ModuleLoader moduleLoader = ContainerAccessorUtil.GetContainer()[typeof(ModuleLoader)] as ModuleLoader;

            if (moduleLoader == null)
            {
                throw new NullReferenceException("Unable to find the ModuleLoader instance");
            }
            string       indexDir = HttpContext.Current.Server.MapPath(Config.GetConfiguration()["SearchIndexDir"]);
            IndexBuilder ib       = new IndexBuilder(indexDir, false);

            ModuleBase module = moduleLoader.GetModuleFromSection(section);

            if (module is ISearchable)
            {
                ib.UpdateContentFromModule(module);
            }

            ib.Close();
        }
        private void BuildIndex()
        {
            string       indexDir = Context.Server.MapPath(Config.GetConfiguration()["SearchIndexDir"]);
            IndexBuilder ib       = new IndexBuilder(indexDir, true);
            IList        sites    = base.CoreRepository.GetAll(typeof(Site));

            foreach (Site site in sites)
            {
                foreach (Node node in site.RootNodes)
                {
                    try
                    {
                        BuildIndexByNode(node, ib);
                    }
                    catch (Exception ex)
                    {
                        log.Error(String.Format("Dựng bảng nội dung cho nút {0} - {1} thất bại.", node.Id, node.Title), ex);
                    }
                }
            }
            ib.Close();
        }
예제 #4
0
        /// <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);
        }