SearchHelper.Search is returning null

santosh maroju asked on June 12, 2019 14:36

I am using the dlls from the CMS Project and working in a seperate Web Api environment.

 public object SearchKey(string key)
        {
            DataSet result = null;
            try
            {

                SearchIndexInfo articleIndex = SearchIndexInfoProvider.GetSearchIndexInfo("ArticleIndex");
                SearchIndexInfo GAndPIndex = SearchIndexInfoProvider.GetSearchIndexInfo("GuidesAndPublication");
                SearchParameters pars = new SearchParameters()
                {
                    SearchFor = key,
                    Path = null,
                    CurrentCulture = CultureHelper.EnglishCulture.IetfLanguageTag,
                    DefaultCulture = null,
                    CombineWithDefaultCulture = false,
                    CheckPermissions = false,
                    SearchInAttachments = false,
                    SearchIndexes = articleIndex.IndexName,
                    User = MembershipContext.AuthenticatedUser,
                    DisplayResults = 100,
                    NumberOfProcessedResults = 100,
                    NumberOfResults = 100,
                    AttachmentWhere = null,
                    AttachmentOrderBy = null,
                    ClassNames = "custom.article",
                    SearchSort = null

                };
                result = SearchHelper.Search(pars);

            }
            catch (Exception ex)
            {

            }
            return result;
        }

I have given references to CMS.Search,CMS.Search.Lucene3,CMS.Search.TextExtractors,CMS.SearchProviderSql. I thinks these dlls are enough for the search functionality.

The Search Preview in Smart search application gives result but this Api failed to do so. Am I missing any parameters here ? Please help.

I am unable to find any solution to similar problems from the discussion forum.

Correct Answer

Dmitry Bastron answered on June 13, 2019 19:11

The way you'd want to interact with the search probably wouldn't work. Even if you copy the search index folder locally and access it from your application after some time it will become outdated as Kentico website will be updating the search index and your copy will remain outdated.

In this case, I would suggest you to write API controller or HTTP handler in your Kentico website solution which will be calling SearchHelper.Search method internally with the parameters passed to the controller/handler and just return the search results.

And the separate application you're writing will be calling API or handler with search string parameter. And in this case you wouldn't need to reference Kentico libraries and deal with this complicated structure. Hope it makes sense.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on June 12, 2019 15:10

Hi Santosh,

  1. What version of Kentico do you use?
  2. During the debug do you see articleIndex and GAndPIndex being retrieved from the database correctly and these are not null?
  3. Have you tried to reference Kentico.Libraries nuget package instead of direct referencies? Please refer to the documentation.
0 votesVote for this answer Mark as a Correct answer

santosh maroju answered on June 12, 2019 15:50 (last edited on June 12, 2019 15:52)

Hi Bastron, I am using Kentico10. Both articleIndex and GAndPindex are being retrieved from database, but the IndexFileSize is shown 0 during debug. I did not try nuget packages but direct references.

All the configurations are done in the application from the document.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on June 12, 2019 16:33 (last edited on June 12, 2019 16:34)

Try to set the path to your index location with this code:

SearchHelper.CustomSearchPath = "Path\\To\\Index\\Folder\\";
SearchHelper.Search(pars);

By default, the following path is used "App_Data\CMSModules\SmartSearch\". And I guess your custom application is located outside of the site folder that's why the default index path is not working for you.

0 votesVote for this answer Mark as a Correct answer

santosh maroju answered on June 13, 2019 07:46 (last edited on June 13, 2019 08:37)

The site is hosted on other server.I am working on my development machine. I tried using complete path with site host address in customSearchPath, but still gives null.I also tried by copying CMSModules folder into custom applicaton App_Data and set path to customSearchPath, still it gave null.

my search expression is a simple word(eg. "agriculture"). Should it be like fieldname:key ?

0 votesVote for this answer Mark as a Correct answer

Dragoljub Ilic answered on June 13, 2019 12:01

Hi Santosh,

When you copied your search index from server to your dev machine (MVC side), are you able to see any files on this path: ~/App_Data/CMSModules/SmartSearch/<Index code name>

If yes, you can open indexer with Lucene Luke All tool. When you search from 'agriculture' from there, you must see some results, otherwise something went wrong directly with the index and not with your code. You can also check from Kentico admin is site and culture properly assigned.

Best regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

santosh maroju answered on June 13, 2019 12:39 (last edited on June 13, 2019 12:46)

Hi Ilic,

From the admin dashboard-> smart search -> index -> smart search Preview page I am able to see the results.

https://drive.google.com/file/d/1VsF3v6JjbHvQeAvHovb4NEO1TzFlFQxT/view?usp=sharing

can we search kentico site hosted in other server,my custom site is on localhost, because I am doing the same. But all the credentials to the database are same as the given site.

0 votesVote for this answer Mark as a Correct answer

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