The ISearchableContentType plugin type provides support for making content searchable. When the search index job runs this plugin is used to gather information about your content.
Why should I make my content searchable?
Search is a powerful feature in any community. If making your content discoverable then planning on a search strategy is important. Consider what properties are important to index for making your content distinguishable from other content on the search page.
Creating an ISearchableContentType plugin
The ISearchableContentType Plugin Type is defined in the Telligent.Evolution.Extensibility.Content.Version1 namespace. For the following example, we'll add ISearchableContentType to the existing content type plugin defined in the Content Type Example , updating the LinkItemContentType definition to:
public class LinkItemContentType : IContentType, ITranslatablePlugin, ISearchableContentType
The ISearchableContentType interface requires a few members:
GetContentToIndex()-- this retrieves content managed by the content type that needs to be indexed. Content returned from this method must be formatted into a SearchIndexDocumentSetIndexStatus()-- this is called to identify that one or more pieces of content have been indexed or unindexed. Normally, this would persist to a long term data store, but, since the example application does not have long-term storage, it is being stored in a local dictionary.ReindexAll()-- this is called to request that all content managed by the content type be flagged for reindexing. It should then be returned whenGetContentToIndex()is next called.GetViewSecurityRoles()-- this is called to retrieve roles which have permission to view a specific content item. Because the sample application does not implement it's own view permissions, our example will return the roles of the anonymous user (which should be "Everyone").GetViewHtml()-- this is called to get a viewable representation of the search result for search result listing. In our example, this will be very simple, but, search result rendering is a good candidate for using a IScriptablePlugin implementation.IsCacheable/VaryCacheByUser-- these properties control the cache support for the result of the GetViewHtml() method. In our example, since all users receive the same content, we identify the rendering as cacheable and not varied by user.
#region ISearchableContentType
private Dictionary<Guid, bool> _indexed = new Dictionary<Guid, bool>();
public bool IsCacheable { get; } = true;
public bool VaryCacheByUser { get; } = false;
public void SetIndexStatus(Guid[] contentIds, bool isIndexed)
{
foreach (var contentId in contentIds)
_indexed[contentId] = isIndexed;
}
public IList<Telligent.Evolution.Extensibility.Api.Entities.Version1.SearchIndexDocument> GetContentToIndex()
{
var contentToIndex = new List<Telligent.Evolution.Extensibility.Api.Entities.Version1.SearchIndexDocument>();
var searchIndexingApi = Telligent.Evolution.Extensibility.Apis.Get<ISearchIndexing>();
foreach (var application in LinksData.ListApplications())
{
foreach (var link in LinksData.ListLinks(application.ApplicationId))
{
if (!_indexed.TryGetValue(link.ContentId, out bool isIndexed) || !isIndexed)
{
var searchContent = searchIndexingApi.NewDocument(link.ContentId, link.ContentTypeId, "linkitem", link.Url, link.Name, link.Description);
searchContent.AddField("date", searchIndexingApi.FormatDate(link.CreatedDate));
contentToIndex.Add(searchContent);
}
}
}
return contentToIndex;
}
public int[] GetViewSecurityRoles(Guid contentId)
{
return Telligent.Evolution.Extensibility.Apis.Get<IRoles>().List(new RolesListOptions
{
Username = Telligent.Evolution.Extensibility.Apis.Get<IUsers>().AnonymousUserName
}).Select(r => r.Id.GetValueOrDefault()).ToArray();
}
public void ReindexAll()
{
_indexed.Clear();
}
public string GetViewHtml(IContent content, Target target)
{
return $"<h2><a href=\"{Apis.Get<IHtml>().EncodeAttribute(content.Url)}\">{content.HtmlName(target.ToString())}</a></h2>{content.HtmlDescription(target.ToString())}";
}
#endregion
Once the project is rebuild and the plugin is , the search index should update to include links from the sample application and search results should start including link-related results.