Simple search page for Find in Alloy MVC
When you think about a search product, you probably start’s to think about the free text search directly. For EPiServer Find, that’s shame, even if it’s understandable.
I don’t see EPiServer Find just as a standard search product, I see it more like a very powerful API together with a document database. When you need to receive collection of data, I think you should get the data from the document database whenever you can, instead of the SQL server.
With this said, I’m going to show you how easy it is to create a standard free text search, together with a simple facet, in Alloy MVC.
First of all, I’m going to create a new model, because the PageViewModel<SearchPage> is not enough. We need a search result collection in the model. We could have used the SearchPageModel in an even easier example, or used it as the base class for the new model, but I will not do that in this example. The argument for not inheriting from SearchPageModel is that we now can use more properties in our view, even if I’m not going to do it in this example.
The properties needed are a string with the searched query, and the result. I will use UnifiedSearch in EPiServer Find, and I will therefore have the UnifiedSearchResults as the type for the Results property.
In the controller, we will create a new instance of the model, search against UnifiedSearch, if the search query isn’t empty, and then we will set the result property.
When searching against unified search, I’m also including “apply best bets”, and “track” in the search.
The model should of course use the model created earlier.
What we should do first is creating a textbox, and a button, and make sure we can send necessary data back to the index method in the controller.
After the textbox and button, we should write the result, if it exists with link to the page representing each result item.
We use the Html-helper “BeginForm” and we send the routed values back to the controller, together with the search query from the textbox.
From the result property, we use the Url, Title, and Excerpt properties to show the result.
To make it a little bit more fun, I’m going to add a facet to the search.
The model needs to contain a collection of facet results. I’m going to create a class containing a link text, and route values. The route values will be used when filtering the results.
In the controller, I will add a terms facet on the unified search. I will use the “SearchSection” property for the facet. To get the facet result back, I will use the same syntax on the result (TermsFacetFor(x => x.SearchSection). This will retrieve the facet result for the “SearchSection”.
When setting the “Sections” property on the model, we need to create a section model for every result in the facet result. I will set the link text as the facet term together with the number of hits. The values property will use the routed data, but it will set a new value (sections) to the facet term. Now we can filter result by adding a new parameter to the method called “sections”.
The last thing I need to do is adding a filter when the sections parameter has been set. I will use “FilterHits” to only filter the result, not the facets.
In the view, I will check if the sections property has been set. If it contains values, I will use the Html-helper “RouteLink” to create a link for each result.
If you are going to try this out, please include the following namespaces in your controller:
More to read
For more information about Unified search, please read: Joels blog about Unified Search
For more information about Alloy MVC, see this article