Copied to clipboard

Flag this post as spam?

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


  • Simon Todd 1 post 72 karma points
    Oct 18, 2021 @ 02:43
    Simon Todd
    1

    How to Index Blocklist Elements in Examine

    Hi,

    I've created a page containing a Block List with some elements.

    Every element in the block list has a field "contentTitle" wich I would like to add to the examine external index.

    I'm trying to find a way to index those properties, because in a page I could have any amounts of block element.

    Reading through the examine documentation I could not find anything related to Block List, where can I find some hint?

    Thanks in advance for your help.

  • Erik Eelman 81 posts 321 karma points
    Oct 18, 2021 @ 08:22
    Erik Eelman
    0

    Hi Simon,

    I've not worked with the blocklist myself, but i think you can add a custom field to the external index of umbraco with the values you need from your blocklist. For example you could create a custom index field with the value of all the blocklist items combined to search on.

        public class IndexComposer : ComponentComposer<IndexComponent>
        { }
    
        public class IndexComponent : IComponent
        {
            private readonly IExamineManager _examineManager;
            private readonly IUmbracoContextFactory _umbracoContextFactory;
    
            public IndexComponent(IExamineManager examineManager, IUmbracoContextFactory umbracoContextFactory)
            {
                _examineManager = examineManager;
                _umbracoContextFactory = umbracoContextFactory;
            }
    
            public void Initialize()
            {
                if (_examineManager.TryGetIndex(Constants.UmbracoIndexes.ExternalIndexName, out IIndex index))
                { 
                    index.FieldDefinitionCollection.AddOrUpdate(new FieldDefinition("blockListTitles", FieldDefinitionTypes.FullText));
    
                    if (index is BaseIndexProvider baseIndex)
                    {
                        baseIndex.TransformingIndexValues += OnTransformingIndexValues;
                    }
                }
            }
    
            private void OnTransformingIndexValues(object sender, IndexingItemEventArgs e)
            {
                if (e.ValueSet.Category != IndexTypes.Content) return;
    
                using (var context = _umbracoContextFactory.EnsureUmbracoContext())
                {
                    var contentId = int.Parse(e.ValueSet.Id);
                    var content = context.UmbracoContext.Content.GetById(contentId);
    
                    if (content is BlocklistPage contentPage) // Your page type
                    {
    // Add some checks if the page has any blocklist items
                        var blocks = contentPage.Value<IEnumerable<BlockListItem>>("myBlocksProperty");
                        e.ValueSet.Add("blockListTitles", string.Join(",", blocks.Select(x => x.Content.Value("contentTitle"))));
                    }
                }
            }
    
            /// <summary>
            /// There is no termination logic for this component
            /// </summary>
            public void Terminate()
            { }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft