Smart search results count

Alexander Pinin asked on May 30, 2017 09:38

Hi everyone!
I'm sorry for such a stupid question, but how can I get an amount of Smart search result in my aspx.cs file?
I have been looking for an answer for about 2 hours, and have no results. On my page I have SearchResults, SearchDialog and UniPager controls. May be I can get this value from one of these controls?

Correct Answer

Anton Grekhovodov answered on May 30, 2017 10:27

Try to use this property:

SearchContext.CurrentSearchResults.Count
1 votesVote for this answer Unmark Correct answer

Recent Answers


Alexander Pinin answered on May 30, 2017 11:00 (last edited on May 30, 2017 11:01)

It should be global static SearchContext? All properties are null.

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 30, 2017 11:25

Hi Alexander,

Check with SearchHelper, you get the counts using this.

// Search Result
DataSet results = SearchHelper.Search(parameters);
int numberOfResults = parameters.NumberOfResults;

Thanks

Happy to help you

0 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 30, 2017 11:32

Hi, Prashant!
What is the parameters value? In the SearchResults control it is defined, but when I import it I have no access.

0 votesVote for this answer Mark as a Correct answer

Prashant Verma answered on May 30, 2017 11:37

Hi Alexander,

Parameters value type of Search parameters

        //   parameters:
        //   Search parameters
        //
        // Returns:
        //     DataSet with search results

        public static DataSet Search(SearchParameters parameters);

Thanks

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 30, 2017 12:43 (last edited on May 30, 2017 12:51)

It's not good to use SearchHelper.Search here, because it will perform the search process again.

Also, it's weird that CMS.Search.SearchContext.CurrentSearchResults property is empty (this property is populated inside SearchHelper.Search method in SearchResult control). I use it on my projects and it works correctly.

2 votesVote for this answer Mark as a Correct answer

Zach Perry answered on May 30, 2017 22:33

I would use what Anton said int count = SearchContext.CurrentSearchResults.Count; I am not 100% positive on this, but I thought if you used pagination that this count wouldn't be 100% accurate.

If you need an example of setting up search paramters, you can just look in the search results web part you are using:

SearchParameters parameters = new SearchParameters()
    {
        SearchFor = searchCondition,
        SearchSort = PredictiveSearchSort,
        Path = PredictiveSearchPath,
        ClassNames = PredictiveSearchDocumentTypes,
        CurrentCulture = PredictiveSearchCultureCode,
        DefaultCulture = CultureHelper.GetDefaultCultureCode(SiteContext.CurrentSiteName),
        CombineWithDefaultCulture = PredictiveSearchCombineWithDefaultCulture,
        CheckPermissions = PredictiveSearchCheckPermissions,
        SearchInAttachments = false,
        User = MembershipContext.AuthenticatedUser,
        SearchIndexes = PredictiveSearchIndexes,
        StartingPosition = 0,
        DisplayResults = PredictiveSearchMaxResults,
        NumberOfProcessedResults = 100 > PredictiveSearchMaxResults ? PredictiveSearchMaxResults : 100,
        NumberOfResults = 0,
        AttachmentWhere = null,
        AttachmentOrderBy = null,
        BlockFieldOnlySearch = PredictiveSearchBlockFieldOnlySearch,
    };
0 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 31, 2017 06:59

Prashant and Zachary, thank you for your answers. I don't think an example with parameters is suitable in my case. I saw how it is defined in the Kentico native SearchResult control. But I'm using something like this:

<cms:SearchDialog ID="SearchDialog1" runat="server" />

<cms:SearchResults ID="SearchResults1" runat="server" IgnoreTransformations="True">
   <ItemTemplate>
      ...
   </ItemTemplate>
</cms:SearchResults>

So in my aspx.cs file I have no access to parameters. If I want to execute the code below I have to make the second request to he database, as Anton said too.

DataSet results = SearchHelper.Search(parameters);
int numberOfResults = parameters.NumberOfResults;

I think I should check parameters of my Kentico, and find why this code does not work.

CMS.Search.SearchContext.CurrentSearchResults

Or copy SearchResult control and try to make some property with size.

0 votesVote for this answer Mark as a Correct answer

Alexander Pinin answered on May 31, 2017 08:11 (last edited on May 31, 2017 08:11)

Well, Anton, how could I be so stupid!
This code works fine CMS.Search.SearchContext.CurrentSearchResults
I tried to get this value in the wrong place, where context was empty.
Now I get it on the OnOnSearchCompleted event of SearchResults.

0 votesVote for this answer Mark as a Correct answer

UNRVLD Agency answered on May 31, 2017 10:38 (last edited on May 31, 2017 10:38)

0 votesVote for this answer Mark as a Correct answer

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