William Jenner (Partner) asked a question.

How to remove Dynamic Content from search index on publish? Using DynamicContentInboundPipe

Hello,

 

I am using Sitefinity Version 15.1.8333, with the .NET Core Renderer.

 

I have a custom module, and I have added a custom field to my module "IncludeInSearchIndex". When this is checked, I want my custom dyanmic content to be included in my search indexes. When it is unchecked, I want it to be removed from the index, and not to be seen in search results.

 

I want my content to be added/removed from search results/index on PUBLISH, not on full re-index. I am confident a full re-index works, but I cannot tell editors to do a full re-index every time they want to edit an article.

 

I have created a custom dynamic content inbound pipe, and overriden the "CanProcessItem" method.

  1. public class CustomDynamicContentInboundPipe : DynamicContentInboundPipe
  2. {
  3. public override bool CanProcessItem(object item)
  4. {
  5. bool canProcessBase = base.CanProcessItem(item);
  6.  
  7. if (canProcessBase && item is DynamicContent contentItem)
  8. {
  9. if (IsCustomArticle(contentItem))
  10. {
  11. return IsIncludedInSearchIndex(contentItem);
  12. }
  13. }
  14.  
  15. return canProcessBase;
  16. }
  17.  
  18. private bool IsCustomArticle(DynamicContent contentItem)
  19. {
  20. return contentItem.GetType().FullName == TypeResolutionService
  21. .ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Custom.CustomArticle").FullName;
  22. }
  23.  
  24. private bool IsIncludedInSearchIndex(DynamicContent contentItem)
  25. {
  26. if (DynamicContentHelper.TryGetField<bool>(contentItem, "IncludeInSearchIndex", out var fieldValue))
  27. {
  28. return fieldValue;
  29. }
  30.  
  31. return true;
  32. }
  33. }

And I have included this in Global.asax.cs:

 

  1. protected void Application_Start(object sender, EventArgs e)
  2. {
  3. // other stuff
  4. PublishingModule.Configured += ConfigureCustomPipes;
  5. // other stuff
  6. }
  7.  
  8. private void ConfigureCustomPipes(object sender, EventArgs e)
  9. {
  10. PublishingSystemFactory.UnregisterPipe(PageInboundPipe.PipeName);
  11. PublishingSystemFactory.RegisterPipe(PageInboundPipe.PipeName, typeof(CustomPageInboundPipe));
  12.  
  13. // Register custom dynamic content inbound pipe
  14. var modules = ModuleBuilderManager.GetManager().Provider.GetDynamicModuleTypes();
  15. foreach (var module in modules)
  16. {
  17. var pipeName = $"{module.GetFullTypeName()}Pipe";
  18.  
  19. PublishingSystemFactory.UnregisterPipe(pipeName);
  20. PublishingSystemFactory.RegisterPipe(pipeName, typeof(CustomDynamicContentInboundPipe));
  21. }
  22. }

My intention when creating this custom pipe is that when a DynamicContent item is published, the CanProcessItem method is hit, and we then return true or false based on if we want that content. When debugging, I can see I have a CustomArticle, and I can see that it has a field IncludeInSearchIndex with value true/false. I then return the true/false from the CanProcessItem method, but no matter what this CustomArticle still appears in search results pages. For the item I am testing, I have unchecked the custom field IncludeInSearchIndex, ensured it returns false in the pipe, but the content is still in my search results pages.

 

I have also disabled output caching for the search results page.

 

Would anybody be able to help with this issue? It's as if the DynamicContentInboundPipe is just not working.


Afif Zainuddin likes this.
  • Kevin Reed (Springthrough)

    Hi @William Jenner (Partner)​ ,

    Thanks for a well thought out question with code samples.

     

    I don't have an immediate answer for you unfortunately. My suggestion is to do more debugging in the `CanProcessItem()` function. Is it the `item is DynamicContent` bit on line 7 that is causing the if statement to be false? Run the site locally and put a breakpoint on line 4. What type is the object that is passed to CanProcessItem()?

     

    You'll notice that CanProcessItem in DynamicContentInboundPipe can handle a couple different types passed into it.

     

    image 

    You'll also notice that PublishingManager.CallSubscribedPipes passes in a PublishingSystemEventInfo instance, not a DynamicContent instance.

     

    image 

    Let me know what type of object you're seeing when you publish one item vs a full reindex, and I'm happy to provide additional thoughts if necessary. Hopefully the info above is enough to get you unstuck!

  • Rachel Berry (Customer)

    The issue likely stems from the DynamicContentInboundPipe only influencing the addition of content during publishing, not the removal of content that's already indexed. To remove the item on unchecking the field, you need to programmatically delete the corresponding search index item using the SearchService within a custom aessuccess org event handler for the IDataEvent or IContentEvent when the item's status changes on publish.