예제 #1
0
        /// <summary>
        /// Set the supplied examine indexers (this may be called by the consumer to specify the Examine indexes to hook into)
        /// </summary>
        /// <param name="examineIndexers">names of Examine indexers to hook into (null or empty array = none)</param>
        internal static void SetExamineIndexers(string[] examineIndexerNames)
        {
            LookService.Instance._examineIndexersConfigured = true; // set flag so that hook indexing startup event doens't reset any conumser set configuration

            // all examine indexers that should be hooked into (string key = indexer name)
            var examineIndexers = new Dictionary <string, BaseUmbracoIndexer>(); // default to empty - ie, no examine indexers to hook into

            if (examineIndexerNames != null && examineIndexerNames.Any())
            {
                // we (might) have indexers to hook into
                examineIndexers = ExamineManager
                                  .Instance
                                  .IndexProviderCollection
                                  .Select(x => x as BaseUmbracoIndexer)   // UmbracoContentIndexer, UmbracoMemberIndexer
                                  .Where(x => x != null)
                                  .Where(x => examineIndexerNames.Contains(x.Name))
                                  .ToDictionary(x => x.Name, x => x);
            }

            // if there are indexers already registered, remove those that are not in the collection
            var removeEvents = LookService
                               .Instance
                               ._examineDocumentWritingEvents
                               .Where(x => !examineIndexers.ContainsKey(x.Key))
                               .ToDictionary(x => x.Key, x => x.Value);

            foreach (var removeEvent in removeEvents)
            {
                var indexer = ExamineManager.Instance.IndexProviderCollection[removeEvent.Key] as BaseUmbracoIndexer;

                indexer.DocumentWriting -= removeEvent.Value;

                LookService.Instance._examineDocumentWritingEvents.Remove(removeEvent.Key);
            }

            // add events if not already registered
            foreach (var examineIndexer in examineIndexers)
            {
                if (!LookService.Instance._examineDocumentWritingEvents.ContainsKey(examineIndexer.Key))
                {
                    EventHandler <DocumentWritingEventArgs> addEvent = (sender, e) => LookService.DocumentWriting(sender, e, examineIndexer.Key);

                    LookService.Instance._examineDocumentWritingEvents[examineIndexer.Key] = addEvent;

                    examineIndexers[examineIndexer.Key].DocumentWriting += addEvent;
                }

                LogHelper.Info(typeof(LookService), $"Hooking into Examine indexer '{ examineIndexer.Key }'");
            }
        }