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 872 karma points
    Apr 23, 2018 @ 17:33
    jake williamson
    0

    how do i add multinodetreepicker2 values to a lucene index?

    ok chaps, this feels like it should be easy but it's being a pain!

    how do i add multinodetreepicker2 node property values to a lucene index?!

    my initial thinking was to do something like this:

    public class Examine : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var helper = new UmbracoHelper(UmbracoContext.Current);
            ExamineManager.Instance.IndexProviderCollection["MyIndexer"].GatheringNodeData += (sender, e) => indexerSite_GatheringNodeData(sender, e, helper);
        }
    
        private void indexerSite_GatheringNodeData(object sender, IndexingNodeDataEventArgs e, UmbracoHelper helper)
        {
            var content = helper.TypedContent(e.NodeId);
    
            var nodes = content.GetPropertyValue<IEnumerable<IPublishedContent>>("entries");
    
            var entries = string.Join(",", nodes.Select(x => x.Name));
            e.Fields.Add("entry", entries);
        }
    }
    

    i'd expect 'nodes' to be a 'IEnumerable

    so i did some debugging and the property 'entries' is being returned as:

    Value = {Umbraco.Core.Udi[4]}
    

    expanding that has 4 entries:

    {umb://document/3457a1b68ef6471fb681d4fc8f864f8b}
    {umb://document/e0a41a0d11a5467480d33da3fadf6d5}
    {umb://document/90115d54778a47208fe8a95afc1a131}
    {umb://document/28904ea23b0040bcb0610a4b202af8}
    

    wtf?!

    i've dropped the above code into a controller to debug it and it works as expected so i'm guessing it's possibly something to do with how the helper is getting the typed content?

    without wanting to reinvent the wheel, i'm guessing there's a more standard way to do this what with the mntp being such a useful property datatype?!

    any suggestions on how to dig myself out of this one would be blardy amazing ;)

    cheers,

    jake

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 23, 2018 @ 19:40
    Dave Woestenborghs
    1

    Hi Jake,

    Have look at slide 51 of this presentation https://www.slideshare.net/dawoe/the-need-for-speed-uk-fest

    that is example of indexing a comma seperated string.

    Dave

  • jake williamson 207 posts 872 karma points
    Apr 23, 2018 @ 20:02
    jake williamson
    0

    hi dave, thank you for taking the time to reply, really appreciate it.

    i've taken a look at the suggested slide and implemented the code. the index now has a value however it's still the 'umb://document/gui' values e.g.

    entry: umb://document/3457a1b68ef6471fb681d4fc8f864f8b,umb://document/4cf5cad9b391459bb4ed0e69a44dd17a,umb://document/a12f8f826b2a40d588bedb8ee46c7063,umb://document/353516f7bad144b7a6d3a618f7249005

    the trick is that i need the 'Name' property from the selected document to then search against...

    so i need to do something inside the foreach loop to get the content based on the 'umb://document/3457a1b68ef6471fb681d4fc8f864f8b' value, then get the name property value and store that?

    i'll keep going through the slides but this is a bit of a head scratcher!

    cheers,

    jake

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 23, 2018 @ 20:08
    Dave Woestenborghs
    0

    Hi Jake,

    My assumption was that you you would just need the id of the selected item.

    Can you post your updated code ?

    dave

  • jake williamson 207 posts 872 karma points
    Apr 23, 2018 @ 20:17
    jake williamson
    0

    hi dave,

    sure thing:

    public class Examine : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var indexer = ExamineManager.Instance.IndexProviderCollection["JobsIndexer"];
            var luceneIndexer = indexer as LuceneIndexer;
            if (luceneIndexer != null) luceneIndexer.DocumentWriting += OnDocumentWriting;
        }
    
        private static void OnDocumentWriting(object sender, DocumentWritingEventArgs e)
        {
            var skills = e.Fields["jobSkills"].Split(',');
            foreach (var skill in skills)
            {
                e.Document.Add(new Field("skill", skill, Field.Store.YES, Field.Index.ANALYZED));
            }
        }
    }
    

    debugging the loop looks like this:

    enter image description here

    and if i do a search on the index i can see the values:

    enter image description here

    my thinking though is that i need the 'jobSkills' to be a comma seperated value of the skill node names e.g.

    Creative, Frontend, Backend, Admin
    

    i think i'm heading about it the right way, it's just getting the skill name rather than the 'umb://document/3457a1b68ef6471fb681d4fc8f864f8b' value thats the key?

    cheers,

    jake

  • Dave Woestenborghs 3504 posts 12133 karma points MVP 8x admin c-trib
    Apr 24, 2018 @ 06:52
    Dave Woestenborghs
    1

    Hi Jake,

    Can you try this ?

    public class Examine : IApplicationEventHandler
    {
        public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
        }
    
        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            var indexer = ExamineManager.Instance.IndexProviderCollection["JobsIndexer"];
            var luceneIndexer = indexer as LuceneIndexer;
            if (luceneIndexer != null) luceneIndexer.DocumentWriting += OnDocumentWriting;
        }
    
        private static void OnDocumentWriting(object sender, DocumentWritingEventArgs e)
        {
            var skills = e.Fields["jobSkills"].Split(',');
            var contentService = ApplicationContext.Current.Services.ContentService;    
            foreach (var skill in skills)
            {
                var udi = GuidUdi.Parse(skill);
    
                var content = contentService.GetById(uid.Guid);
    
                var skilltext = content.Name;
    
                e.Document.Add(new Field("skill", skilltext, Field.Store.YES, Field.Index.ANALYZED));
            }
        }
    }
    

    This uses the content service to get the item from UDI

    dave

  • jake williamson 207 posts 872 karma points
    Apr 24, 2018 @ 07:24
    jake williamson
    0

    hi dave,

    funny you should suggest that! i woke up in the middle of the night with that very same idea floating around in my head... never go to sleep with an unresolved problem eh?!

    the key was the line:

    var udi = GuidUdi.Parse(skill);
    

    i wasn't aware of 'GuidUdi.Parse()' which is far better than the string find and replace idea i was going to run with.

    it's an interesting one, tbh i didn't think it would be this complex to get mntp selections into an index but this does indeed work ;)

    i guess the only thing that i might try is to do the same thing but passing the umbraco helper to 'OnDocumentWriting'. the site i'm working on will have potentially thousands of nodes which means hitting up the content service with it's multiple database hits will be very slow... getting typedcontent using the helper should in theory be faster (i'll test it out).

    thank you for the replies and suggestions, great to have got a working solution!

    cheers,

    jake

Please Sign in or register to post replies

Write your reply to:

Draft