Predictive Search

Fernandes Pandia asked on May 12, 2020 06:22

How to implement predictive search in mvc deployment project? I use kentico 12 and custom search box. I already activated smart search but still don't know how to implement to mvc deployment project. I already tried this: https://docs.kentico.com/k12/configuring-kentico/setting-up-search-on-your-website/using-locally-stored-search-indexes/building-a-search-interface-for-local-indexes-in-mvc , but not working. It's show no result.

Correct Answer

Dmitry Bastron answered on May 13, 2020 12:19

Have you checked \App_Data\CMSModules\SmartSearch folders content for both CMS and MVC apps? Are they the same? If they are not - this is the issue. Also, check that your MVC application has write permissions on App_Data folder, this is important.

Have you also looked at the troubleshooting guide I pointed?

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on May 12, 2020 10:12

Hi Fernandes,

Few more questions:

  • What type is your index and how is it configured? Pages or Pages Crawler? What analyzer is set?
  • Do you see indexed documents in CMS? Do you see any errors on index rebuild?
  • If you try Search Preview in CMS, do you see any results?
  • Check Kentico Web Farms (as suggested here or here)
  • Could you post your search code that returns no results?
0 votesVote for this answer Mark as a Correct answer

Fernandes Pandia answered on May 13, 2020 05:24 (last edited on May 13, 2020 05:32)

  • I use local indexes, index type is Pages and analyzer type is Standard. (Based on this).
  • No error on index rebuild
  • CMS search show some result
  • Settings> Version & Synchronization->Web farm>Smart search indexes is checked.
  • I use this code.

Is this a problem? check this screenshot link

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 13, 2020 09:47

Yes, red status in Web Farms might be the problem because Web Farms are used to synchronize indexes between CMS and MVC. Check \App_Data\CMSModules\SmartSearch folder content for both CMS and MVC applications. Ideally, the content of these 2 folders must be identical. It seems that this folder on your MVC site is empty that's why you get no results.

Try going through this troubleshooting article and see if it helps.

0 votesVote for this answer Mark as a Correct answer

Fernandes Pandia answered on May 13, 2020 11:46 (last edited on May 13, 2020 11:48)

Web Farms status for both CMS and MVC now show Healty, but still no result. CMS search preview show some result but no result in the MVC. Debug result here. Search result from CMS here.

0 votesVote for this answer Mark as a Correct answer

Fernandes Pandia answered on May 14, 2020 05:28 (last edited on May 14, 2020 05:55)

\App_Data\CMSModules\SmartSearch folders for both CMS and MVC have the same content because i was replaced content in the MVC \App_Data\CMSModules\SmartSearch folder with CMS \App_Data\CMSModules\SmartSearch folder. You mean folders permission from folder properties>security right? Its have full control in permission. I already looked at troubleshooting guide that you gave to me. Is it okay if the code name of the smart search not ended with .index?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 14, 2020 09:58

Hmmm... then I'm running out of ideas. The very last I can think of is, in the code in this article there is a string with index code name:

public static readonly string[] searchIndexes = new string[] { "MVCSite.Index" };

In the CMS admin is code name (not display name) of your index the same?

Also, is your website "en-us" culture?

SearchParameters searchParameters = SearchParameters.PrepareForPages(searchText, searchIndexes, 1, PAGE_SIZE, MembershipContext.AuthenticatedUser, "en-us", true);
0 votesVote for this answer Mark as a Correct answer

Fernandes Pandia answered on May 14, 2020 10:35 (last edited on May 14, 2020 10:36)

My fault, i forgot to change Code Name. Its working now but its show no url for the result. How to get the url and how to show all the result?

In this code: SearchParameters searchParameters = SearchParameters.PrepareForPages(searchText, searchIndexes, 1, PAGE_SIZE, MembershipContext.AuthenticatedUser, "en-us", true);

That code request for page 1 only, what should I pass to the param for all the search result.

After the smart search working, how to add predictive search to my mvc?

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on May 14, 2020 11:04

Resolving URLs may depend on how do you route pages. If your page type has URL pattern specified (typically all my page types have URL pattern as it's easier to manage) in the code you can do the following:

foreach (SearchResultItem item in searchResult.Items)
{
    // item.Data contains your TreeNode object if it's a page, or Info class, if it is module custom class
    var document = item.Data as TreeNode;

    // this would be your search result page URL
    var documentUrl = URLHelper.ResolveUrl(document?.RelativeURL);
}

As for predictive search, I typically implement it similar to full search, but with Ajax call:

  • when user typed 3 or more symbols in the search field, run Ajax call in your javascript to your search controller
  • return and draw search results

But the only difference that will be required for it to work, you need to select "Starts with" analyzer for your search index so that search can return "coffee" results when you start typing "cof".

0 votesVote for this answer Mark as a Correct answer

Fernandes Pandia answered on May 14, 2020 13:51 (last edited on May 15, 2020 07:08)

var documentUrl = URLHelper.ResolveUrl(document?.RelativeURL); Is this url source from Live URL in CMS Pages>General>Live URL? If yes, how to change Live URL in CMS? CMS Live URL diffrent with actual URL.

For example this should be http://localhost/Kentico12_OCEC/Career.

0 votesVote for this answer Mark as a Correct answer

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