Copied to clipboard

Flag this post as spam?

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


  • jake williamson 207 posts 873 karma points
    Jan 19, 2023 @ 00:21
    jake williamson
    1

    how to trigger updates to a custom index in version 11

    hey out there,

    we've upgrading a v8 site to v11 - everything was going well until we hit indexing... the site has several large custom indexes that are based on content...

    luckily, the docos have been updated on how to create custom indexes! amazing!

    https://docs.umbraco.com/umbraco-cms/reference/searching/examine/indexing

    however... although we can follow the example and get the index created and populated we don't seem to be able to hook into a save and/or save and publish event to update the item in the index?!

    it feels like it should be something that recalls the IValueSetBuilder again to repopulate the item but we can't find anything that would trigger it?

    what are we missing?!

  • Tom W 39 posts 96 karma points
    Jan 23, 2023 @ 16:33
    Tom W
    0

    hi jake

    we're also having similar issues, but with an Umbraco 10.4.0 site, and not an upgrade.

    it's unclear whether we're meant to manually handle the manipulation of the index on publish/unpublish/delete, and if so, how to do that reliably.

    they recommend appending to the ExternalIndex but it's also not totally clear at the moment how to add new values to that e.g add dateparts for a DateTime to aid in searching - new fields, that don't exist on all content items, and don't exist as properties directly. the docs only mention changing field value types

    i'll let you know if we work out what you're missing, as we're missing it too at the moment!

  • Tom W 39 posts 96 karma points
    Jan 24, 2023 @ 11:58
    Tom W
    2

    hello!

    so, I couldn't work out how to append values to the External (PublishedContent) Index, but will get back to that

    For now, we discovered that the events were firing for Unpublish and Delete - that is, the records were being removed from the index.

    I also tried extending Umbraco.Cms.Infrastructure.Examine.Custom.ContentIndexPopulator to see if that would do the hooking up for me, but it couldn't find the Validator, even though I configure it all in my IConfigureNamedOptions<LuceneDirectoryIndexOptions>

    The index was working, the only issue being that new items were not added on Publish. Rebuilding the index worked with the custom IndexPopulator So the easiest thing to do, was to push values into the index on Publish

    ``` public class BlogPublishNotificationHandler : INotificationHandler

        public BlogPublishNotificationHandler(IExamineManager examineManager, BlogIndexValueSetBuilder blogIndexValueSetBuilder)
        {
            _examineManager = examineManager;
            _blogIndexValueSetBuilder = blogIndexValueSetBuilder;
        }
    
        public void Handle(ContentPublishedNotification notification)
        {
            foreach (var node in notification.PublishedEntities)
            {
                if (node.ContentType.Alias == WebsiteConstants.DocTypes.Blog)
                {
                    var index = _examineManager.Indexes.FirstOrDefault(s =>
                        s.Name == WebsiteConstants.ExamineIndexes.BlogIndexName);
    
                    var indexData = _blogIndexValueSetBuilder.GetValueSets(node);
                    index.IndexItems(indexData);
                }
            }
        }
    }
    

    and then register that in your Composer

    public class BlogExamineComposer : IComposer
        {
            public void Compose(IUmbracoBuilder builder)
            {
                builder.Services.ConfigureOptions<BlogIndexConfigureOptions>();
    
                builder.Services.AddExamineLuceneIndex<BlogIndex, ConfigurationEnabledDirectoryFactory>(WebsiteConstants.ExamineIndexes.BlogIndexName);
    
                builder.Services.AddSingleton<BlogIndexValueSetBuilder>();
    
                builder.Services.AddSingleton<IIndexPopulator, BlogIndexPopulator>();
    
                builder.AddNotificationHandler<ContentPublishedNotification, BlogPublishNotificationHandler>();
            }
        }
    

    Not ideal, but is works. I really think some more documentation would help a lot of people!

Please Sign in or register to post replies

Write your reply to:

Draft