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
    Sep 24, 2018 @ 08:53
    jake williamson
    0

    how to automatically re index child nodes when a parent node is saved

    hey out there,

    i wanted to check if there's something built into examine/lucene or umbraco that can trigger reindexing of child nodes when a parent is saved?

    my structure is that when a child node is saved, i store some info from the parent node in the child document. however, if the info in the parent changes when it's saved, i need to rebuild the documents for the child nodes.

    at the moment, i've gone for this:

    private static void myIndexer_GatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
    {
        try
        {
            var content = ApplicationContext.Current.Services.ContentService.GetById(e.NodeId);
    
            foreach (var node in content.Children().Where(x => x.Published))
            {
                ApplicationContext.Current.Services.ContentService.PublishWithStatus(node);
            }
    
        }
        catch (InvalidOperationException)
        {
        }
    }
    

    it does work, but due to using the content service to get the node and it's children and then calling 'PublishWithStatus' there's a lot of unnecessary database activity...

    hitting the database isn't needed as this is all index work, there's nothing visible on the node in the back office.

    i'm wondering if i'm over complicating things and there's something built in to automatically re index child nodes when a parent is saved?

    cheers,

    jake

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Sep 24, 2018 @ 11:38
    Ismail Mayat
    100

    Jake,

    Why not republish the items in the parent on publish event https://our.umbraco.com/Documentation/Reference/Events/ContentService-Events just ensure that the current type that is being published is of type who children you want to republish.

    Regards

    Ismail

  • jake williamson 207 posts 873 karma points
    Sep 24, 2018 @ 18:51
    jake williamson
    0

    hi ismail,

    spot on, just what i was after ;)

    a bit of experimenting and i've found that hooking into the saved event does the trick:

    private static void ContentService_Saved(IContentService sender, SaveEventArgs<IContent> e)
    {
        foreach (var node in e.SavedEntities)
        {
            try
            {
                if (node.ContentType.Alias == "MyAlias")
                {
                    foreach (var content in node.Children())
                    {
                        sender.PublishWithStatus(content);
    
                    }
                }
            }
            catch (InvalidOperationException)
            {
            }
        }
    }
    

    interestingly along the way i found the 'PublishWithChildrenWithStatus()' method in the content service which would seem to be the method called when you right click a node in the back office, select publish and then check the 'Publish xxx and all its subpages'. not really usable for this scenario but useful for future reference.

    thank you for the suggestion, this is working a treat now.

Please Sign in or register to post replies

Write your reply to:

Draft