Remove deleted content from indexed pages
In this article you will find:
Goal
The purpose of this article is to demonstrate how to remove deleted content from already indexed pages.
Prerequisite
Configured Connector - Connector: Applying Settings
Register custom search service - Register custom search service
Â
Overview
The idea behind this article is an edge case in which if you have placed e.g. a news widget on a page and you have indexed that page. On the frontend page all of the news will be visible and we would be able to search them within the current page results as they are a part of its content. If you then delete one of the news articles you will continue to see information about it when you type in the search bar of the page. The following sample fixes this issue as it republishes the page and updates it in the index.
The issue has been reported to Progress, but still has no official resolution - Knowledgebase article
Setup query field
In order for the following sample to work correctly you need to have made the IdentityField queryable. To do so follow these steps:
Open the dashboard of you Hawksearch instance.
Navigate to the Workbench
Under Data Configuration select Fields
Locate IdentityField and open it for edit
In the Advanced options check this:
Â
Create custom search service
To remove the deleted content from already indexed pages you need to create a custom search service class and override the RemoveDocuments method. Please refer to the code snippet below.
Â
using System;
using System.Collections.Generic;
using System.Linq;
using Hawksearch.Data;
using Hawksearch.SDK.Search;
using Hawksearch.Search;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Search.Data;
using Telerik.Sitefinity.Utilities.TypeConverters;
using Telerik.Sitefinity.Workflow;
using SearchQuery = Hawksearch.SDK.Search.SearchQuery;
namespace SitefinityWebApp.Search
{
public class CustomSearchService : HawksearchService
{
public override void RemoveDocuments(string indexName, IEnumerable<IDocument> documents)
{
var client = this.InitializeClient();
var manager = HawkManager.GetManager();
var indexMapping = manager.GetIndexMappings().FirstOrDefault(i => i.SitefinityIndex == indexName);
foreach (var item in documents)
{
var id = item.IdentityField.Value.ToString();
var searchQuery = new SearchQuery();
if (indexMapping != null)
{
searchQuery.IndexName = indexMapping.HawkIndex;
}
searchQuery.Keyword = id;
var results = client.Search(searchQuery);
var result = results.Results.FirstOrDefault();
if (result != null)
{
var pageIds = this.GetPageIds(result);
foreach (var pageId in pageIds)
{
this.RepublishPage(pageId);
}
}
}
base.RemoveDocuments(indexName, documents);
}
private void RepublishPage(Guid pageId)
{
var manager = PageManager.GetManager();
var page = manager.GetPageNode(pageId);
var bag = new Dictionary<string, string>();
bag.Add("ContentType", typeof(PageNode).FullName);
WorkflowManager.MessageWorkflow(page.Id, typeof(PageNode), null, "Publish", false, bag);
}
private List<Guid> GetPageIds(Result result)
{
var resultPageIds = new List<Guid>();
var locationsService = SystemManager.GetContentLocationService();
if (result.Document.ContainsKey("contenttype"))
{
var contentType = result.Document["contenttype"].FirstOrDefault();
if (contentType != null)
{
var type = TypeResolutionService.ResolveType(contentType, false);
if (type != null)
{
var mappedManager = ManagerBase.GetMappedManager(type, "");
if (result.Document.ContainsKey("originalitemid"))
{
var itemId = result.Document["originalitemid"].FirstOrDefault();
if (itemId != null)
{
var pageIds = locationsService.GetItemLocations(type, mappedManager.Provider.Name, Guid.Parse(itemId)).Select(il => il.PageId).ToList();
resultPageIds.AddRange(pageIds);
}
}
}
}
}
return resultPageIds;
}
}
}