Trigger search and examine results from code

Don Rea asked on November 4, 2021 21:08

We have a Kentico 11-managed site, and a feature request for eliminating any content types from the search screen content type filter dropdown that would not yield any results in combination with the already-submitted search terms, if any. The only way I can think to do that is to run each possible filtered search under the hood and see if any have no results. But I absolutely cannot find any place where the search logic is actually called, so I don't know how to call it myself. My questions are, 1. Is this a workable approach? Is it possible to initiate a Smart Search from our own code, and examine the results? 2. If it is workable, how can we run a search from code? 3. Is there a better approach? I've found that sometimes this CMS has features I never would have thought to look for.

Recent Answers


David te Kloese answered on November 5, 2021 09:48

My approach would be to make the page type selection inversed.

I assume you're using Portal engine? Are you using the "default" Search results Web Part?

What you do in it's config is select the page types you want to include. e.g. "articles, news, content, some other custom types". And create logic that would add all types not selected.

By default the smartsearchresults code has the following:

srchResults.ClassNames = ClassNames;

That get's the classes from the Web Part config. You have to extend it with the logic of the new exclude dropdown filter you want.

The search index itself is overall, and including everything. So all types and content items should be indexed already. In the results you're just filtering.

0 votesVote for this answer Mark as a Correct answer

Don Rea answered on November 5, 2021 15:45

I believe I haven't been clear in my description of what I've been asked to do, so I'm going to elaborate and hope you have some more patience.

Our search results screen has a Smart Search Filter in Text box mode, for a user to enter search terms, which will of course be already populated from the query string if they came to the screen from a search form on another screen or have already entered a value on this screen. There is also a Smart Search Filter in Dropdown list mode, for optionally filtering the results by certain Page types. What I've been asked to do is to remove types from that dropdown dynamically, when the screen loads, if those types in combination with the value of the text box filter (if there is a value) would leave no results.

I am new enough to Kentico that I'm not sure I understand your original reply, but it sounds like you're describing how to set up the page type filter, which is already there and working. I did not realize until just this morning that such a filter is something that had to have been created by my predecessors, but now I see where in the Filter Properties the custom page types are being added. I apologize for my lack of clarity arising from my lack of understanding what I'm asking about.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on November 5, 2021 15:58

Can you make the results and the filter part of the same control? So you can easily just populate the filter when done processing the results?


if not; quick outside the box idea.

on your search results list... add a hidden piece of data displaying the number of items per page type.

e.g.:

<input type="hidden" id="customId" name="results-NewsArticles" value="5">

....

<input type="hidden" id="customId" name="results-JobItems" value="0">

And have a jquery script that will hide/remove the values from the dropdown based on the above. As it's not secret or anything just to help the user


Alternatively

Assuming the filter is a secondary web part? you could also look into using the RequestStockHelper that would allow you to communicate between the two Web Parts (results and filter). And basically force the dropdown to reload it's data after the search results are processed and you know type x, y and z or not present.

I can't find any proper example online of this, but I know it's used somewhere in the solution by Kentico's default Web Parts (might be ecommerce).

0 votesVote for this answer Mark as a Correct answer

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