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 107 posts 343 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 109 posts 621 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 107 posts 343 karma points c-trib
    Mar 04, 2019 @ 10:19
    Louis Jackson-Rees
    0

    Unfortunately not, no

  • Ole Martin Bakke 109 posts 621 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 18 posts 119 karma points
    Apr 19, 2019 @ 16:25
    Chad
    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 45 posts 205 karma points
    Apr 25, 2019 @ 13:59
    Aleksander
    0

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

  • Bryna 66 posts 228 karma points
    Apr 25, 2019 @ 17:43
    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 109 posts 621 karma points
    Apr 26, 2019 @ 06:55
    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 45 posts 205 karma points
    Apr 29, 2019 @ 08:23
    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

  • Vincent 1 post 20 karma points
    Jul 26, 2019 @ 11:12
    Vincent
    0

    Does this work for you?

    I tried creating a custom examine index both with Ole's example and with the official example (https://our.umbraco.com/documentation/Reference/Searching/Examine/indexing/) , but im getting a write.lock file all the time. And changing the DefaultLockFactory doesn't work unfortunately.

  • Arnold Visser 418 posts 748 karma points hq c-trib
    Jun 05, 2019 @ 08:07
    Arnold Visser
    0

    The offical way to create custom indexes can be found in this Gist: https://gist.github.com/hartviglarsen/44743b32d4eea8eda3fb6d49b60dd547

    Note that you of course have to register the component in a composer too.

    (via Twitter: https://twitter.com/meisterhartvig/status/1136184529734049792)

  • Jonathon Cove 5 posts 75 karma points
    Feb 21, 2020 @ 15:46
    Jonathon Cove
    0

    Creating custom indexes can now be found in the Umbraco documentation: https://our.umbraco.com/documentation/reference/searching/examine/indexing/ (Scroll down to ''Overriding Index Creation")

    Tested and working in 8.4.1

  • Dan Evans 605 posts 957 karma points
    Mar 12, 2020 @ 14:33
    Dan Evans
    1

    Any reason why this is now done in code? Something that was pretty simple to edit and update is now a but is now a complex task. I appreciate that this gives more flexibility but config files are useful for basic settings.

Please Sign in or register to post replies

Write your reply to:

Draft