Search code examples
c#asp.netkentico

Assign Kentico smart search index to CMSSearchResults control datasource


EDIT: Maybe this will make my question more clear. I want to do this, but with a CMSSearchResults control and a search index as the DataSource:

// code-in-front
<asp:Repeater ID="Repeater" ruant="server"></asp:Repeater>

// code behind
private void BindDataToRepeater()
{
    DataSet ds = PopulateDataSet();

    repStuff.DataSource = ds;
    repStuff.DataBind();
}

I'm trying to implement a custom search user control. I have a <cms:CMSSearchResults> control in my ascx file like so:

<cms:CMSSearchResults ID="ssIssues" runat="server"></cms:CMSSearchResults>

And in my code behind I'm working on a method that should retrieve a typical custom table smart search index and use the data within the index as the data source for ssIssues. Here's what I have so far that should give you a rough idea of what I'm wanting to do:

private void BindSearchResults()
{
    SearchIndexInfo si = SearchIndexInfoProvider.GetSearchIndexInfo(int searchIndexId);

    // some tweaking to convert si to dataset

    ssIssues.DataSource = si;
    ssIssues.DataBind();
}

However, right off the bat I'm running into some issues and can't seem to find any documentation that will help me understand this part of the search api.

I'm getting the correct SearchIndexInfo object, but it doesn't seem to have any way of accessing the actual data in the index. Now, my understanding of how search indexes work in kentico is that they analyze a set of data in SQL Server and convert that data into a b-tree that's stored in the filesystem of the webserver in the Kentico project.

So, I was expecting a method in SearchIndexInfo that did something along the lines of parsing that index file and returning the b-tree as a LinkedList<t> or a method that could be provided some arguments to sort the linked list into a DataSet - or at least did the parsing and allowed me to do the converting. No such luck though, and I'm not sure where to go from here.

How exactly should I be binding a search index DataSet to the CMSSearchResults control?


Solution

  • Once you've already started with customization, I'd suggest you to build you own control from scratch, but utilize Kentico search API to get data from search index. Following is a code snippet from Kentico API Examples:

    private bool SearchText()
    {
        // Get the search index
        SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("MyNewIndex");
    
        if (index != null)
        {
            // Prepare parameters
            SearchParameters parameters = new SearchParameters()
            {
                SearchFor = "home",
                SearchSort = SearchHelper.GetSort("##SCORE##"),
                Path = "/%",
                ClassNames = "",
                CurrentCulture = "EN-US",
                DefaultCulture = CultureHelper.DefaultCulture.IetfLanguageTag,
                CombineWithDefaultCulture = false,
                CheckPermissions = false,
                SearchInAttachments = false,
                User = (UserInfo)CMSContext.CurrentUser,
                SearchIndexes = index.IndexName,
                StartingPosition = 0,
                DisplayResults = 100,
                NumberOfProcessedResults = 100,
                NumberOfResults = 0,
                AttachmentWhere = String.Empty,
                AttachmentOrderBy = String.Empty,
            };
    
            // Search
            DataSet results = SearchHelper.Search(parameters);
    
            // If found at least one item
            if (parameters.NumberOfResults > 0)
            {
                return true;
            }
        }
    
        return false;
    }