API Questions on Kentico API.
Version 6.x > API > Searching a Custom Table with Smart Search View modes: 
User avatar
Member
Member
alan-adrury.org - 8/8/2012 7:10:37 AM
   
Searching a Custom Table with Smart Search
Hi,

I have a custom table built through Kentico like so.

[ItemID] [int] IDENTITY(1,1) NOT NULL,
[ItemCreatedBy] [int] NULL,
[ItemCreatedWhen] [datetime] NULL,
[ItemModifiedBy] [int] NULL,
[ItemModifiedWhen] [datetime] NULL,
[ItemOrder] [int] NULL,
[ItemGUID] [uniqueidentifier] NOT NULL,
[TrainingPlanName] [nvarchar](255) NOT NULL,
[TrainingPlanIsActive] [bit] NOT NULL,
[TrainingPlanCompletionDate] [datetime] NOT NULL,
[TrainingPlanAdditionalInformation] [nvarchar](max) NULL,
[TrainingPlanUserID] [int] NOT NULL,
[TrainingPlanStatus] [int] NOT NULL

I have created an index on the table using the TrainingPlanName column as the title and the TrainingPlanAdditionalInformation column as content. The following code performs the search on the index.

/// <summary>
/// Loads and filters training plans from the database.
/// </summary>
private void LoadTrainingPlans()
{
if (!string.IsNullOrWhiteSpace(txtSearchField.Text))
{
DataSet dataSet = SearchKeywords(txtSearchField.Text);

if (dataSet != null && dataSet.Tables.Count > 0)
{
var dataTable = dataSet.Tables[0];

dataTable.Columns["id"].ColumnName = "ItemID";
dataTable.Columns["title"].ColumnName = "TrainingPlanName";
dataTable.Columns["content"].ColumnName = "TrainingPlanAdditionalInformation";
}
else
{
pnlNoResults.Visible = true;
UniGridTrainingPlans.Visible = false;
}

UniGridTrainingPlans.GridName = "TrainingPlanUniGrid_Search.xml";
UniGridTrainingPlans.DataSource = dataSet;
UniGridTrainingPlans.DataBind();
}
}

/// <summary>
/// Search for the given keyboards (using kentico smart search) and return
/// a DataSet with the search results
/// </summary>
/// <param name="keywords">string containing the keywords to search for</param>
/// <returns><see cref="DataSet"/> containing the search results, or null if no result is found</returns>
private static DataSet SearchKeywords(string keywords)
{
DataSet ds = null;

var index = SearchIndexInfoProvider.GetSearchIndexInfo(TrainingPlanSearchIndex);
if (index != null)
{
int numberOfResults;

ds = SearchHelper.Search(
keywords,
SearchHelper.GetSort("##SCORE##"),
"/%",
"",
CMSContext.CurrentUser.PreferredUICultureCode,
CultureHelper.DefaultCulture.IetfLanguageTag,
false, false, false,
index.IndexName,
100, 0, 100,
CMSContext.CurrentUser,
out numberOfResults,
"", "");

if ((ds != null) && (ds.Tables.Count == 0))
ds = null;
}

return ds;
}

I would like to be able to show the TrainingPlanStatus or TrainingPlanIsActive column in the grid that displays the dataset returned from the search but I've been unable to do this. Alternatively, I've tried using query syntax like '+TrainingPlanIsActive:true' or '+TrainingPlanIsActive:0' but neither of these seems to have any effect despite the column being searchable. Can you suggest where I'm going wrong or if there is an alternative approach.

Thanks

Alan

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/9/2012 2:07:44 AM
   
RE:Searching a Custom Table with Smart Search
Hello,

What do you mean by "grid"? Where do you want to display the additional information? Have you tried to check the following article?

Best regards,
Boris Pocatko

User avatar
Member
Member
alan-adrury.org - 8/9/2012 2:51:39 AM
   
RE:Searching a Custom Table with Smart Search
Hi Boris,

Thanks for answering my post.

I'm trying to display the search results in a UniGrid, the status column I want to add will be an additional column in the UniGrid. I read the article you suggested and I had thought about trying the GetSearchValue(string columnName) method, will this work on smart search results based on custom tables?

I had considered a database look up for each search result returned to get the the status value but I wanted to investigate other means due to performance issues as mentioned in the article.

What about adding +TrainingPlanIsActive:true or something similar to the search string? The column is searchable so I could do this in code if the user selected a 'show only active content' option. I tried adding this to a search string that returned results but doing so caused nothing to be returned.

Thanks

Alan

User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/9/2012 3:01:40 AM
   
RE:Searching a Custom Table with Smart Search
Hello,

If you are looking for an example on how to customize the UniGrid, please check the following knowledge base article. I am not sure, if the following method will work on custom tables, however the second approach highlighted in the article definitely will. If you add any custom field to the search string it only indicates to search in those fields (it's an additional condition), not to return the content of that field.

Best regards,
Boris Pocatko

User avatar
Member
Member
alan-adrury.org - 8/9/2012 4:39:23 AM
   
RE:Searching a Custom Table with Smart Search
Boris,

Yes I understand that adding the custom search string won't add the column to the results but that is ok. My problem is that adding the +TrainingPlanIsActive:true string returns no results but I know it should. Maybe it's because the column is a bit column in the database?

Alan


User avatar
Kentico Consulting
Kentico Consulting
kentico_borisp - 8/9/2012 6:56:04 AM
   
RE:Searching a Custom Table with Smart Search
Hello,

Is the mentioned approach working for any of the fields in your custom table? Is your TrainingPlanIsActive field set as Content and Searchable in the definition of the custom table / Search fields? Additionally, are you able to search for that field without the special syntax? I've used the following syntax and it was working fine:

<searched text> +<custom boolean field>:true

Best regards,
Boris Pocatko