Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Louis Jackson-Rees 91 posts 318 karma points c-trib
    Feb 26, 2019 @ 19:10
    Louis Jackson-Rees
    1

    Umbraco 8 Examine Indexes

    Anyone got any examples or code i can take a look at for implementing a custom index set / searcher

  • Seb Smith 1 post 20 karma points
    Feb 26, 2019 @ 22:43
    Seb Smith
    0

    I was just about to ask this as well. In the previous version I added custom indexers using ExamineSettings.config. Is there a different way now?

    Thanks in advance

    Seb

  • Ole Martin Bakke 108 posts 618 karma points
    Mar 04, 2019 @ 10:07
    Ole Martin Bakke
    0

    Did you find any examples? Need to create a custom index set without all doc.types.

  • Louis Jackson-Rees 91 posts 318 karma points c-trib
    Mar 04, 2019 @ 10:19
    Louis Jackson-Rees
    0

    Unfortunately not, no

  • Ole Martin Bakke 108 posts 618 karma points
    Mar 04, 2019 @ 12:24
    Ole Martin Bakke
    102

    By looking at det core-code, I managed to get something working, but I do not know if I am on the right track here. I seems a bit like killing mosquitos with a canon, comparet to the old config-files.

    I created my own "CustomIndexCreator"-class, registered it as a composer and the created a component adding the index to the Examine-manager.

    public class CustomIndexCreator : LuceneIndexCreator, IUmbracoIndexesCreator
    {
    
        public CustomIndexCreator(IProfilingLogger profilingLogger,
            ILocalizationService languageService)
        {
            ProfilingLogger = profilingLogger ?? throw new System.ArgumentNullException(nameof(profilingLogger));
            LanguageService = languageService ?? throw new System.ArgumentNullException(nameof(languageService));
        }
    
        protected IProfilingLogger ProfilingLogger { get; }
        protected ILocalizationService LanguageService { get; }
    
        public override IEnumerable<IIndex> Create()
        {
            return new[]
            {
                CreateCustomIndex()
            };
    
        }
    
        private IIndex CreateCustomIndex()
        {
            var index = new UmbracoContentIndex(
                "CustomIndex",
                CreateFileSystemLuceneDirectory("Custom"),
                new UmbracoFieldDefinitionCollection(),
                new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30),
                ProfilingLogger,
                LanguageService,
                new ContentValueSetValidator(true, excludeItemTypes: new string[] { "DocTypeAliasToExclude",}));
            return index;
        }
    
    }
    

    The composer

     [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class CustomExamineComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Register(typeof(CustomIndexCreator));
            composition.Components().Append<CustomExamineComponent>();
        }
    }
    

    And the component

    public class CustomExamineComponent : IComponent
    {
        private readonly IExamineManager _examineManager;
        private readonly CustomIndexCreator _indexCreator;
    
        public CustomExamineComponent(IExamineManager examineManager, CustomIndexCreator indexCreator)
        {
            _examineManager = examineManager;
            _indexCreator = indexCreator;
        }
    
    
        public void Initialize()
        {
    
            //we want to tell examine to use a different fs lock instead of the default NativeFSFileLock which could cause problems if the AppDomain
            //terminates and in some rare cases would only allow unlocking of the file if IIS is forcefully terminated. Instead we'll rely on the simplefslock
            //which simply checks the existence of the lock file
            DirectoryFactory.DefaultLockFactory = d =>
            {
                var simpleFsLockFactory = new NoPrefixSimpleFsLockFactory(d);
                return simpleFsLockFactory;
            };
            foreach (var index in _indexCreator.Create())
                _examineManager.AddIndex(index);
    
        }
    
    
        public void Terminate()
        {
    
        }
    
    }
    

    Edit 29.04.2019 Added code to avoid write-lock on indexfile

  • Chad Levenhagen 7 posts 77 karma points
    30 days ago
    Chad Levenhagen
    0

    When I implement this, I get errors on successive restarts from visual studio because the write.lock file inside the custom directory is not being deleted.

  • Aleksander 29 posts 148 karma points
    24 days ago
    Aleksander
    0

    I'm getting write lock all over aswel... Have you found a solution?

  • Bryna 41 posts 175 karma points
    24 days ago
    Bryna
    0

    According to the behaviour have observed, the lock file normally isn't present when Umbraco first starts up, so perhaps the file can be removed on startup? It works for me, but I cannot say that is the proper solution. The only reason for its existence was for performance. if you delete the lock file on startup, it will get re-created the first time you utilize the search.

  • Ole Martin Bakke 108 posts 618 karma points
    23 days ago
    Ole Martin Bakke
    1

    I haven't tried this my self as I went with the built in indexes for now. But I found this in the source code for Umbraco.

    Try adding this first in the Initialize-method of the CustomExamineComposer, before the foreach(var index in _indexCreator...

                //we want to tell examine to use a different fs lock instead of the default NativeFSFileLock which could cause problems if the AppDomain
                //terminates and in some rare cases would only allow unlocking of the file if IIS is forcefully terminated. Instead we'll rely on the simplefslock
                //which simply checks the existence of the lock file
                DirectoryFactory.DefaultLockFactory = d =>
                {
                    var simpleFsLockFactory = new NoPrefixSimpleFsLockFactory(d);
                    return simpleFsLockFactory;
                };
    
  • Aleksander 29 posts 148 karma points
    20 days ago
    Aleksander
    0

    Third edit: I got the write lock once more...

    Yes this definetly fixes the write.lock!

    edit: and i added it after the foreach, so that seems fine

Please Sign in or register to post replies

Write your reply to:

Draft