Copied to clipboard

Flag this post as spam?

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


  • mmaty 109 posts 281 karma points
    Jun 29, 2021 @ 06:48
    mmaty
    0

    TransformingIndexValues for Index is not always called

    Hello everybody, I wrote a Component to adjust the behavior of the MembersIndex. I convert phone numbers into tokens in the following way: the number +49 867 1234-567 is converted so that the hyphen is exchanged for a blank. This creates a token "567" that can be found in the search. The problem I have with this solution is that exactly the same code works on the QA system and it doesn't work on the production system (logger.Debug is not called). Are there any conditions that must be met for the TransformingIndexValues ​​handler to actually be called?

    Umbraco version: 8.11.1

    Thanks in advance for any help.

    This is my code:

    public class CustomizeIndexComposer : ComponentComposer<CustomizeMembersIndexComponent>
    {
    }
    
    public class CustomizeMembersIndexComponent : IComponent
    {
        private readonly IExamineManager examineManager;
        private readonly ILogger logger;
    
        public CustomizeMembersIndexComponent( IExamineManager examineManager, ILogger logger )
        {
            this.examineManager = examineManager;
            this.logger = logger;
        }
    
        public void Initialize()
        {
            // get the member index
            if (!examineManager.TryGetIndex( Constants.UmbracoIndexes.MembersIndexName, out IIndex index ))
                return;
    
            // add a custom field
            // this code always works    
            index.FieldDefinitionCollection.TryAdd( new FieldDefinition( "phone", FieldDefinitionTypes.FullText ) );
            // ... adding additional fields 
    
            ((UmbracoMemberIndex) index).TransformingIndexValues += CustomizeMembersIndexComponent_TransformingIndexValues;
        }
    
        private void CustomizeMembersIndexComponent_TransformingIndexValues( object sender, IndexingItemEventArgs e )
        {
            try
            {
                var value = e.ValueSet.GetValue( "phone" );
                if (value == null)
                {
                    logger.Debug( GetType(), "--phone: null" );
                    return;
                }
                var phone = ((string) value).Replace( "-", " " ).Replace( "+", "" );
                logger.Debug( GetType(), $"--phone: {(string)value}->{phone}" );
                e.ValueSet.Values.Remove( "phone" );
                e.ValueSet.Add( "phone", phone );
            }
            catch (Exception ex)
            {
                logger.Error( GetType(), ex );
            }
        }
    
        public void Terminate()
        {
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft