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
|