Command line search by tags and contents in the Knowledge base

Case

We need to implement the command line search available on portal user pages in the knowledge base not only by the primary field (name), but also by article contents and public tags.

Solution

The search can be developed in the "knowledge base search line" of a portal user page. It is a separate element that looks as a command line, but its logic implementation is contained in the KnowledgeBaseSearchModule schema.

This page can be debugged when logged in as a regular user via regular interface by opening it as a module using the following link:

http://your-website/0/Nui/ViewModule.aspx#PortalMainPageModule/

To implement an advanced filter instead of filtering by article title only, modify the following code by replacing the KnowledgeBaseSearchModule base schema:

/*sessionFilters.CustomFilters = { value: value, displayValue: value, primaryDisplayColumn: true };*/
 
var filters = Ext.create("Terrasoft.FilterGroup");
filters.addItem(Terrasoft.createColumnFilterWithParameter(
    Terrasoft.ComparisonType.CONTAIN, "Notes", value));
filters.addItem(Terrasoft.createColumnFilterWithParameter(
    Terrasoft.ComparisonType.CONTAIN, "Name", value));
filters.addItem(Terrasoft.createColumnFilterWithParameter(
    Terrasoft.ComparisonType.CONTAIN, "Keywords", value));
filters.logicalOperation = Terrasoft.LogicalOperatorType.OR;
 
var serializationInfo = filters.getDefSerializationInfo();
serializationInfo.serializeFilterManagerInfo = true;
 
sessionFilters.CustomFilters = {
    "null": {
        "displayValue": value,
        "filter": filters.serialize(serializationInfo)
    }
};

To record public tags into the "Keywords" column for further search by this column, implement the events of modifying, adding and deleting knowledge base tags by creating them in the KnowledgeBaseInTagV2 replacing object.

var userConnection = (UserConnection)HttpContext.Current.Session["UserConnection"];
Guid entityId = Entity.GetTypedColumnValue<Guid>("EntityId");
var esq = new EntitySchemaQuery(userConnection.EntitySchemaManager, "KnowledgeBaseInTagV2");
var publicTypeId = "D6FB4DE6-0809-41FE-A84F-6D245CBC5F32";
esq.AddColumn("Tag.Name");
var entityFilter = esq.CreateFilterWithParameters(FilterComparisonType.Equal,
    "Entity.Id", entityId);
var typeFilter = esq.CreateFilterWithParameters(FilterComparisonType.Equal,
    "Tag.Type", publicTypeId);
esq.Filters.Add(entityFilter);
esq.Filters.Add(typeFilter);
var entityCollection = esq.GetEntityCollection(userConnection);
string allTags = string.Empty;
foreach (var entity in entityCollection) {
    var tagName = entity.GetTypedColumnValue<string>("Tag_Name");
    allTags += tagName + ", ";
}
using (DBExecutor executor = userConnection.EnsureDBConnection()) {
    Update updateRelationshipQuery = new Update(userConnection, "KnowledgeBase");
    updateRelationshipQuery.Set("Keywords", Column.Parameter(allTags));
    updateRelationshipQuery.Where("Id").IsEqual(Column.Parameter(entityId));
    updateRelationshipQuery.Execute(executor);
}
return true;

The last thing is: after publishing the schemas, populate the "Keywords" column with public tags for already existing knowledge base records via the following script:

UPDATE KnowledgeBase
SET Keywords = ISNULL(res.Tags, '')
FROM (SELECT a.Id, Tags = (SELECT stuff((
                select ', ' + Name from (SELECT t.Name FROM KnowledgeBaseTagV2 t WHERE
                                t.TypeId = 'D6FB4DE6-0809-41FE-A84F-6D245CBC5F32'
                                and t.Id
                                IN (
                                                SELECT e.TagId FROM KnowledgeBaseInTagV2 e WHERE e.EntityId = a.Id
                                )
) tb FOR XML PATH('')), 1, 2, ''))
FROM KnowledgeBase a
GROUP BY a.Id) res
WHERE res.ID = KnowledgeBase.Id

Where "D6FB4DE6-0809-41FE-A84F-6D245CBC5F32" is a "public tag" type identifier.

Necessary conditions

Application version with a portal user page and the KnowledgeBaseSearchModule base schema.

Like 0

Like

Share

0 comments
Show all comments