Copied to clipboard

Flag this post as spam?

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


  • louisjrdev 107 posts 344 karma points c-trib
    Feb 26, 2019 @ 19:10
    louisjrdev
    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 112 posts 624 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.

  • louisjrdev 107 posts 344 karma points c-trib
    Mar 04, 2019 @ 10:19
    louisjrdev
    0

    Unfortunately not, no

  • Ole Martin Bakke 112 posts 624 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 122 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 73 posts 239 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 112 posts 624 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 778 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 26 posts 101 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 629 posts 1016 karma points
    Mar 12, 2020 @ 14:33
    Dan Evans
    2

    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.

  • Adrian 3 posts 71 karma points
    Jul 05, 2021 @ 07:23
    Adrian
    0

    Any way to exclude specific content types from the examine results?

    Sure you can do this as a filter within the results but is there a way to do this from within the examine query?

    Seems a bit overkill to create a custom search index just to omit 1 content type? (I am using tabbed content items which do not have a URL, these still appear in the results and then go to 404's)

  • Bo Jacobsen 590 posts 2366 karma points
    Dec 01, 2021 @ 08:27
    Bo Jacobsen
    1

    Hi Adrian.

    Yes you can do that in your Index or Searcher search query. My example is with a Searcher that can search over multiple Indexes.

    if (Examine.ExamineManager.Instance.TryGetSearcher("searcherName", out var searcher))
    {
        // Just for demostration
        var searchQuery = "Can you find me, when google cannot";
    
        // GroupedNot
        var results1 = searcher
                .CreateQuery()
                .GroupedOr(new string[] { "field1", "field2" }, searchQuery)
                .And()
                .GroupedNot(new string[] { Examine.LuceneEngine.Providers.LuceneIndex.ItemTypeFieldName }, "nodeTypeAlias1", "nodeTypeAlias2", "nodeTypeAlias3")
                .Execute(0);
    
        // AndNot
        var results2 = searcher
                .CreateQuery()
                .GroupedOr(new string[] { "field1", "field2" }, searchQuery)
                .AndNot(x => x.Field(Examine.LuceneEngine.Providers.LuceneIndex.ItemTypeFieldName, "nodeTypeAlias1"))
                .Execute(0);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft