Improve Your Kentico Site Search Experience in Only a Matter of Minutes - Part 2
IntroductionThis is the second part of How to Improve Your Kentico Site Search Experience in Only a Matter of Minutes. Please be sure to read part 1 first before reading this post. The second part of this blog post series focuses on explaining how to create the solution.
Smart Search Filters to the Rescue
Kentico 8 introduced a new set of built-in functionality to let developers add filtering and ordering to a set of search results. The Smart search filter web part allows you to do both of these things. You only need to add it to your page template and configure the correct web part properties to connect the Smart search results web part to the Smart search filter web part.
To get started, go to the Design tab of your search results page template that contains either a Smart search results web part or Smart search dialog with results web part. From there choose which web part zone you want to have the filter show up in and click on the Add new web part menu button on the web part zone.
Once the Select web part dialog is open, find the Smart search filter web part and add it to your page template.
You can add as many of these filters as you want to a page template. In my example above I have added a few to get the desired results. Each Smart search filter represents one filter option group. As you can see below I have named the web part title property uniquely to be able to identify which web part holds which filter options (ex. "SmartSearchFilter - Content Type").
To connect the Smart search filter to the Smart Search results you need to configure the Search dialog web part ID property of the Smart search filter web part. In my example the normal Smart search results web part ID was named SmartSearchResults. I have copied that name into the filter’s property value as illustrated below. That is the way the web parts are connected.
Important Things to Know
Now that the filter is connected to the results. All that is left is to configure the Filter properties property group fields. And here is where the work comes in. Each filter has its own set of configuration that you can either statically provide a set of filter choices, or dynamically provide them.
The best place to get started on learning how to configure these properties is the Kentico documentation on Using search filters.The documentation tells us that Kentico uses the Lucene.NET syntax to create the filtering functionality. It takes a bit to get used to it, but the syntax is:
+Index field name;Value of the field;Displayed text
The first character must always be a plus or minus character. The plus character means the result must match the value of the field to come back in the results, while the minus character means exclude any matches.
The Index field name is what field of the index you are searching, the value of the field is what you are filtering for in the Index field, and the Displayed text is what text that the user sees on the results page. Again refer to the documentation for all the details, this is just the base pattern and there are more complex examples that it shows. No need to recreate those here.
As I mentioned you can statically type these in the Values field, or you can write a query to dynamically populate them.
Once you have the filter choices worked out, you can choose how you want the filter to appear from a set of form elements. Your choices in the Filter mode property are checkboxes, radio buttons, drop down, or text.
The last two remaining properties of note are the Filter clause and Filter is conditional properties. The Filter clause is a dropdown choice that lets you choose from None, Must, or Must not. Again check out the documentation. Must will be the most common choice here.
The Filter is conditional property defines whether the web part will act as a true filter to narrow results, or act as a way to control the sequencing or order of results that appear. Setting it to true will filter, while false tells the system to use the web part as a way to control the sequence. One note on the sort expression and direction. By default the Kentico Smart search results web parts always sort on score of the match, and that is always in descending order.
The results of adding the Smart search filter to the page template for the above screen shot is a nice looking filter. When a user clicks on one of the checkboxes in the filter, the page will automatically post back (refresh) and your search results will be narrowed down to matching results for that filter.
Smart Search Filter – Page Type Example
Now on to some examples to illustrate the different types of filters you can create. The example below would be classified as a Static Conditional filter. I recommend using this method when you have a very few static types of filters. It also works well when you want to use the Text or Date Range types of filters. The example below shows how to manually enter in a set of Page types as a way to narrow down the search results that matches the chosen filter. The matches would be defined as any result items that are of a certain Page type or class name as the Kentico API is concerned.
The Values field is set to:
The Filter clause is set to Must, and the Filter is conditional bit is checked. The end result is a nice set of checkboxes that filter down the search results to just the Locations page type for the Dancing Goat demo site as you have seen above where Chicago, New York, and LA are show on the page. Filtering instead fo Articles gives you a result set of:
Up NextPretty cool right? In very little time we have the ability to some simple faceted search in Kentico. Continue on reading part 3 of How to Improve Your Kentico Site Search Experience in Only a Matter of Minutes to see more real world examples and read the conclusion of the series.