Examine Custom Index TransformingIndexValues

I have configured custom index and TransformingIndexValues method to gather some additional information about nodes of type “article”. The problem is that when I create new “article” and click “Save &Publish” for first time TransformingIndexValues is not fired (or doesn’t work as expected). When I click second time “Save&Publish” everything is ok. Here is mu code:
`

private readonly IExamineManager _examineManager;
        private readonly CustomIndexCreator _indexCreator;
        private readonly IUmbracoContextFactory _contextFactory;

        public CustomExamineComponent(IExamineManager examineManager, CustomIndexCreator indexCreator, IUmbracoContextFactory contextFactory)
        {
            _examineManager = examineManager;
            _indexCreator = indexCreator;
            _contextFactory = contextFactory;
        }


        public void Initialize()
        {

            //we want to tell examine to use a different fs lock instead of the default NativeFSFileLock which could cause problems if the AppDomain
            //terminates and in some rare cases would only allow unlocking of the file if IIS is forcefully terminated. Instead we'll rely on the simplefslock
            //which simply checks the existence of the lock file
            DirectoryFactory.DefaultLockFactory = d =>
            {
                var simpleFsLockFactory = new NoPrefixSimpleFsLockFactory(d);
                return simpleFsLockFactory;
            };
            foreach (var index in _indexCreator.Create())
            {
                _examineManager.AddIndex(index);
                if (index.Name == "NewsSearchIndex")
                {
                    ((BaseIndexProvider)index).TransformingIndexValues += CustomExamineComponent_TransformingIndexValues; 
                }
               
            }

        }

        private void CustomExamineComponent_TransformingIndexValues(object sender, IndexingItemEventArgs e)
        {
            try
            {

                using (var ctx = _contextFactory.EnsureUmbracoContext())
                {
                    var cache = ctx.UmbracoContext.ContentCache;
                    if (e.ValueSet.ItemType == "articleDetailsPage")
                    {
                        var categoryItem = e.ValueSet.Values["category"];
                        if (categoryItem != null)
                        {
                            //more than one category
                            string[] array = categoryItem[0].ToString().Split(',');
                            if (array.Length > 0)
                            {
                                var sb = new StringBuilder();
                                for (int i = 0; i < array.Length; i++)
                                {
                                    Udi udi;
                                    if (Udi.TryParse(array[i], out udi))
                                    {
                                        //Get node by UDI
                                        var guid = GuidUdi.Parse(udi.ToString());
                                        var category = cache.GetById(guid.Guid);
                                        if (category != null)
                                        {
                                            sb.Append(string.Format("{0} ", category.GetProperty("title").Value()));

                                        }
                                    }
                                }
                                e.ValueSet.Add("categoryValue", sb.ToString());
                            }
                        }
                        //get the article
                        var article = cache.GetById(int.Parse(e.ValueSet.Id));
                        e.ValueSet.Add("year", article.Ancestor<YearFolder>().Name);
                        e.ValueSet.Add("month", article.Ancestor<MonthFolder>().Name);
                        e.ValueSet.Add("monthSortOrder", article.Ancestor<MonthFolder>().SortOrder);
                        e.ValueSet.Add("yearSortOrder", article.Ancestor<YearFolder>().SortOrder);
                    }
                    

            }
            catch (Exception ex)
            {
                throw;
            }
        }`

When I create item and click “Save&Publish” and then go to examine Indexes and check the item my custom fields are not there. After clicking "“save&publish” second time and to go the index - my custom values are there.
I am thinking that may be on first Save&Publish the index of the node is not created yet and I have to ensure that it exists and then add additional values.

Please help


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/97830-examine-custom-index-transformingindexvalues