Copied to clipboard

Flag this post as spam?

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


  • Thomas Kassos 54 posts 265 karma points
    Aug 19, 2019 @ 15:45
    Thomas Kassos
    0

    Examine index with custom data. How to Add and Remove items ?

    Hi!

    I have create an Examine Index with custom data following the steps from this article https://24days.in/umbraco-cms/2016/custom-data-and-examine-searching/.

    From this article I have implement only the solution till to create the Index and no to rebuild it.

    What I am trying to achieve is a better approach to update the Index and no to rebuild the entire Index every few minutes. I am trying to add a node to examine Index on publish and remove it on UnPublush and on Trashed events.

    To achieve that I have create a event handler

        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Published += ApiPage_Published;
            ContentService.UnPublished+= ApiPage_UnPublished;
            ContentService.Trashed += ApiPage_Trashed;
        }
    

    First I start working with the ApiPage_Trashed

        private void ApiPage_Trashed(IContentService sender, MoveEventArgs<IContent> e)
        {
            var nodeID = e.Entity.ContentType.Id.ToString();
    
            try
            {
                ExamineManager.Instance.IndexProviderCollection["ApiPageIndexer"].DeleteFromIndex(nodeID);
    
                var query = "Dynamic";
                var searcher = ExamineManager.Instance.SearchProviderCollection["ApiPageSearcher"];
    
                var searchCriteria = searcher.CreateSearchCriteria();
                var searchQuery = searchCriteria.Field("nodeName", query);
                var searchResults = searcher.Search(searchQuery.Compile()).ToList();
    
                LogHelper.Info<string>($"node with id {nodeID} has been removed from ApiPageIndexer");
            }
            catch (Exception ex)
            {
                LogHelper.Error<ContentEvents>($"node with id {nodeID} has faild to be removed from ApiPageIndexer", ex);
            }
    
        }
    

    (the search code exists only for the debugging)

    So the code runs without any error but the Node still exist in the Index.

    Am I missing something? any ideas on that?

    Also does anyone know how I can add a node to the Index?

    Thanks, Thomas

  • Thomas Kassos 54 posts 265 karma points
    Oct 22, 2019 @ 10:41
    Thomas Kassos
    1

    I had found what was wrong, I was picking the wrong Id

    e.Entity.ContentType.Id.ToString()
    

    this id was from the document type and no from the node. Bellow I am sharing my approach - It's working! :)

               try
                {
                    foreach (var node in e.MoveInfoCollection)
                    {
                        if (node.Entity.ContentType.Alias == ApiVersion.ModelTypeAlias)
                            ExamineManager.Instance.IndexProviderCollection[ExamineAliases.ApiPageIndexerName].DeleteFromIndex(node.Entity.Id.ToString());
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error<ContentEvents>("bla bla", ex);
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft