How to display smart search total results count?

Marcel Guldemond asked on July 22, 2016 18:14

I would like to display the total count of a search, plus the query that the user entered at the top of the page above the actual search results webpart.

Is there a macro to display the total count of results for a page? Or do I have to create a custom macro to go and get this value from somewhere?

Or does the universal pager webpart have an option to display totals?

thanks, Marcel

Recent Answers


Chetan Sharma answered on July 23, 2016 18:10

To display total number of results on your live site, you can place following code to your transformations

<%# DataItemIndex == 0 ? "<p>Total results: " + DataItemCount + "</p>" : "" %>

Thanks, Chetan

0 votesVote for this answer Mark as a Correct answer

Marcel Guldemond answered on July 25, 2016 21:48

Hi Chetan, thanks for your answer. Unfortunately that doesn't seem to work: I get the total number of results on the current page, which is 10. If there are 62 results, I still get 10. (I put the macro code into the transformation for the search results.)

I also still don't know how to get the query string inputted by the user so I can display that in the header above the search results.

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on July 27, 2016 08:48

Hi Marcel,

  1. Yes in case you are using pagination this code will not show the exact count. There is no starightaway solution for this out of the box within Kentico. The way you can bypass this is to set no of results per page to a high number. My question to you will be do you really need pagination or can you get away with it?

  2. If answer to above question is no then you can use javascript to update the number of results on the page by counting the child divs. Let me know if you need code for this. You can also write custom code for this as you suggested

  3. In order to show query that user entered, you can display that using a static html webpart. E.g For example, on pages with URLs like /Search?q=hello world, {? q ?} resolves into hello world. You can use this code inside a static html webpart

<div>You search for {? q ?}</div>

I hope this helps you a tiny bit.

Cheers Chetan

1 votesVote for this answer Mark as a Correct answer

Marcel Guldemond answered on July 27, 2016 16:27

Thanks for the answer Chetan, I didn't know that {? querystringvariable ?} pulled values from the query string, so that was helpful.

As far as the total search results count goes, that's fairly disappointing that there's no built in method or property for that, considering it's commonly displayed on most search pages. Meaning most internet users will expect to see the total number of results.

I guess I'll have to add code to SearchResults.ascx.cs to put the total results (since they're available there) into the Session object and then add a custom macro to get the value from the Session object?

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on July 27, 2016 17:36

Yes, that will be a right approach. If you can refer this article, it will help you add your own key in the search results like "Count" and use that value to display no of results or any other method that you think.

Regards, Chetan

0 votesVote for this answer Mark as a Correct answer

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