Kentico 13 Smart Search

Nick Cave asked on August 9, 2024 10:14

Hi guys, I have a problem with my smart search. I created search index in my admin application and search there works as it should. The problem is that I can't perform search via controller as search results is always 0. here is my controller:

    /// <summary>
    /// Retrieves search results based on the provided searchText and renders the Index view.
    /// </summary>
    /// <param name="searchText">The text to search for.</param>
    /// <returns>The Index view with the search results.</returns>
    [Route("Search")]
    public IActionResult Index(string searchText)
    { SearchIndexInfo index = SearchIndexInfoProvider.GetSearchIndexInfo("CoreSiteSearchIndex");

            // Define search parameters
            SearchParameters parameters = new SearchParameters()
            {
                SearchFor = ViewBag.SearchText,
                SearchSort = "##SCORE##",
                Path = "/%",
                CurrentCulture = "EN-US",
                DefaultCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
                CombineWithDefaultCulture = false,
                CheckPermissions = true,
                SearchInAttachments = false,
                User = (UserInfo)MembershipContext.AuthenticatedUser,
                SearchIndexes = index.IndexName,
                StartingPosition = 0,
                DisplayResults = 300,
                NumberOfProcessedResults = 300,
                NumberOfResults = 0,
                AttachmentWhere = String.Empty,
                AttachmentOrderBy = String.Empty,
                ClassNames = ""
            };

            SearchResult results = SearchHelper.Search(parameters);

        // Store searchText in ViewBag
        ViewBag.SearchText = searchText;

        // Pass search results to the view
        return View(results);
    }
}

}

Recent Answers


Brenden Kehren answered on August 9, 2024 19:31

It looks like you're missing a method call to prepare the search parameters.

SearchParameters searchParams = SearchParameters.PrepareForPages(string searchText, string[] searchIndexCodeNames, int startingPage, int pageSize, MembershipContext.AuthenticatedUser, "en-us", true)
SearchResult searchResult = SearchHelper.Search(searchParams);

I'd remove your complex definition for search parameters and try the simple approach above first to at least get your results back.

0 votesVote for this answer Mark as a Correct answer

John Lougee answered on August 9, 2024 19:31

This can sometimes happen due to the index files not properly syncing over to the live site application due to an issue with the web farm servers or if syncing these files has been disabled in the settings. Are you able to get any results from the search preview for this index (Smart search application in the administration interface -> Edit the index ✎ -> Search preview tab? I assume it does and this is what you mean by it working in the Admin but I wanted to check to be sure.

Can you check the Web farms application within the administration interface to make sure that all of your web farm servers are showing a Healthy status: Monitoring web farm servers? If both sites are running and you are using automatic web farm mode but they are not showing healthy and/or you don't have at least two servers listed in there (one for admin, one for live site), then you can try restarting all web farm servers in the System application -> Restart all web farm servers button. Once the system receives requests to both admin and live site, they should start showing up in the Web farm application and transition to healthy after a few minutes.

Can you also check to make sure that the index files are being synced over to the live site project? The search indexes are stored in physical index files (Smart search overview) and you should be able to find them in the ~/{your live site project folder}/App_Data/CMSModules/SmartSearch/{Index code name} folder of the live site if they are being properly synced over. If you are not seeing them there, I would recommend checking the Settings application as well to make sure you are allowing syncing of these files. You can find the applicable setting in Settings -> Versioning & Synchronization -> Web farm -> in the Allow synchronization for section: Settings - Web farm

Lastly, I'd also check the Event log for any errors that might help diagnose the issue

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.