Copied to clipboard

Flag this post as spam?

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


  • Diogo 8 posts 82 karma points
    Oct 26, 2016 @ 08:59
    Diogo
    0

    ReIndex Examine single node doesn't appear on the searcher

    I'm trying to reIndex a node into a specific indexer after the node is published. I have successfully done this on the Umbraco Events class like so:

        public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            Umbraco.Core.Services.ContentService.Published += ContentService_Published;
        }
    
        void ContentService_Published(Umbraco.Core.Publishing.IPublishingStrategy sender, Umbraco.Core.Events.PublishEventArgs<Umbraco.Core.Models.IContent> e)
        {
            var pois = e.PublishedEntities.Where(x => x.ContentType.Alias == DocumentTypeAlias.PointOfInterest);
            if (pois.Any())
            {
                foreach (var poi in pois)
                {
                    POIsDataService.IndexNode(poi);
                }
            }
        }
    

    The POIsDataService is the class where I centralize my indexing logic all together and looks like this:

    public class POIsDataService : ISimpleDataService
    {
        public IEnumerable<SimpleDataSet> GetAllData(string indexType)
        {
            var umbracoContext = DependencyResolver.Current.GetService<UmbracoContext>();
    
            var allPois = umbracoContext.ContentCache.GetAtRoot().SelectMany(x => x.Descendants(DocumentTypeAlias.POI))
                .Where(x => x.Path.StartsWith(ConfigurationManager.AppSettings[***]));
    
            var data = new List<SimpleDataSet>();
            foreach (var x in allPois)
            {
                try
                {
                    var examineNode = Convert(x, umbracoContext);
    
                    data.Add(examineNode);
                }
                catch (Exception ex)
                {
                    //TODO: log exception
                }
            }
            return data;
        }
    
        public static void IndexNode(IContent node)
        {
            var umbracoContext = DependencyResolver.Current.GetService<UmbracoContext>();
            var examineData = Convert(node.ToPublishedContent(), umbracoContext);
            var examineNode = examineData.RowData.ToExamineXml(node.Id, Constants.Configs.POIType);
            ExamineManager.Instance.IndexProviderCollection[Constants.Configs.POIIndexerName].ReIndexNode(examineNode, Constants.Configs.POIType);
        }
    
        ...
    
    }
    

    So I have the node.ToPublishedContent() that I took from the great code of Jeroen here https://gist.github.com/jbreuer/dde3605035179c34b7287850c45cb8c9 and it works just fine.

    The thing is, when I reindex everything the Searcher gets my node, however when I publish and ReIndex just one node the Searcher doesn't return it.

    If I use Luke reader for lucene I can see the node is there and indexed with the new information but I do have to re-open the index. I wonder if I need to do something like this (reopening the index or reloading the index) also on Umbraco before doing the search? And if so, how do I do that?

  • Diogo 8 posts 82 karma points
    Nov 02, 2016 @ 17:54
    Diogo
    0

    Well... This was fun...

    The solution for this is the following, you need to delete first and only afterwards you can reindex, at least this was what worked for me.

    public static void IndexNode(IContent node)
    {
        var umbracoContext = DependencyResolver.Current.GetService<UmbracoContext>();
        var examineData = Convert(node.ToPublishedContent(), umbracoContext);
        var examineNode = examineData.RowData.ToExamineXml(node.Id, Constants.Configs.POIType);
        -----> ExamineManager.Instance.IndexProviderCollection[Constants.Configs.POIIndexerName].DeleteNFromIndex(examineNode, Constants.Configs.POIType);
        ExamineManager.Instance.IndexProviderCollection[Constants.Configs.POIIndexerName].ReIndexNode(examineNode, Constants.Configs.POIType);
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft