Copied to clipboard

Flag this post as spam?

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


  • Lee 1130 posts 3088 karma points
    Jan 14, 2023 @ 08:07
    Lee
    0

    Unable To Get An Examine Custom Index Item To Work In V11

    I am unable to get the following code to work. I was trying to do exactly the same a Paul has done in his blog, only for V11.

    https://codeshare.co.uk/blog/how-to-search-by-picked-multi-node-tree-picker-values-in-umbraco-v8/

    I have IConfigureNamedOptions set up and have debugged the code and it is getting hit.

    public class ConfigureExternalIndexOptions : IConfigureNamedOptions<LuceneDirectoryIndexOptions>
    {
        public void Configure(string? name, LuceneDirectoryIndexOptions options)
        {
            if (name is global::Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName)
            {
                options.FieldDefinitions.AddOrUpdate(new FieldDefinition("blogDate", FieldDefinitionTypes.DateTime));
                options.FieldDefinitions.AddOrUpdate(new FieldDefinition(Constants.ExamineFields.SearchableBlogCategories, FieldDefinitionTypes.FullText));
            }
        }
    
        // Part of the interface, but does not need to be implemented for this.
        public void Configure(LuceneDirectoryIndexOptions options)
        {
            throw new System.NotImplementedException();
        }
    }
    

    I then have a IComponent which gets the values and adds them to the index to the new field.

    public class IndexerComponent : IComponent
    {
        private readonly IExamineManager _examineManager;
        private readonly IUmbracoContextFactory _umbracoContextFactory;
    
        public IndexerComponent(IExamineManager examineManager,
            IUmbracoContextFactory umbracoContextFactory)
        {
            _examineManager = examineManager ?? throw new ArgumentNullException(nameof(examineManager));
            _umbracoContextFactory =
                umbracoContextFactory ?? throw new ArgumentNullException(nameof(umbracoContextFactory));
        }
    
        public void Initialize()
        {
            if (_examineManager.TryGetIndex(global::Umbraco.Cms.Core.Constants.UmbracoIndexes.ExternalIndexName, out var externalIndex))
            {
                ((BaseIndexProvider)externalIndex).TransformingIndexValues += IndexerComponent_TransformingIndexValues;
            }
        }
    
        private void IndexerComponent_TransformingIndexValues(object? sender, IndexingItemEventArgs e)
        {
            if (int.TryParse(e.ValueSet.Id, out var nodeId))
            {
                switch (e.ValueSet.ItemType)
                {
                    case "blogPage":
                        using (var umbracoContext = _umbracoContextFactory.EnsureUmbracoContext())
                        {
                            var contentNode = umbracoContext.UmbracoContext.Content?.GetById(nodeId);
                            if (contentNode != null)
                            {
                                var categories = contentNode.Value<IEnumerable<IPublishedContent>>("blogCategories");
                                var publishedContents = categories as IPublishedContent[] ?? categories?.ToArray();
                                var updatedValues = e.ValueSet.Values.ToDictionary(x => x.Key, x => x.Value.ToList());
                                if (publishedContents != null && publishedContents.Any())
                                {
                                    if (updatedValues.ContainsKey(Constants.ExamineFields.SearchableBlogCategories))
                                    {
                                        updatedValues[Constants.ExamineFields.SearchableBlogCategories] = new List<object> { string.Join(" ", publishedContents.Select(x => x.Key.ToString("N"))) };
                                    }
                                    else
                                    {
                                        updatedValues.Add(Constants.ExamineFields.SearchableBlogCategories,new List<object> { string.Join(" ", publishedContents.Select(x => x.Key.ToString("N"))) });   
                                    }
                                }
                                e.SetValues(updatedValues.ToDictionary(x => x.Key, x => (IEnumerable<object>)x.Value));
                            }
                        }
                        break;
                }
            }
        }
    
        public void Terminate()
        {
        }
    }
    

    This is all being hit, and the value added to the updateValues dictionary when I hit save. But, when I hit save again, those values are not in the index?

    Any idea where I am going wrong with this?

  • Lee 1130 posts 3088 karma points
    Jan 17, 2023 @ 10:25
    Lee
    100

    Seems this was working! A local development environment issue I think

  • Heather Floyd 610 posts 1033 karma points MVP 6x c-trib
    Jul 06, 2023 @ 22:22
    Heather Floyd
    0

    I'm coming across something similar... Any idea what the local environment issue was?

  • Thomas 319 posts 606 karma points c-trib
    Nov 29, 2023 @ 11:42
    Thomas
    0

    Did you find a solution ? :) Having same issue

  • Thomas 319 posts 606 karma points c-trib
    Nov 29, 2023 @ 11:50
    Thomas
    0

    It actullay worked in 11 but not in 12.3.3 now

  • Joshua Crittenden 14 posts 124 karma points
    Jul 31, 2024 @ 21:27
    Joshua Crittenden
    0

    For anybody finding this, you shouldn't perform service lookups within the TransformingIndexValues event.

    ie. umbracoContext.UmbracoContext.Content?.GetById(nodeId);

    Umbraco Pitfalls

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies