Do you need to implement a site search within your Umbraco website, then read on. On most projects, you will need to implement a site search. Searches can range from simple to complex. The good news is that creating a basic site search with Umbraco is also pretty easy as it ships with a search provider call Examine. I've talked previously about Lucene and Examine within Umbraco here. This article will show you how to implement a search in MVC using it. If you need things like faceted filtering of search results, AI-powered recommendations, indexing of content multiple sources then this article isn't for you and you will need to use a different tool, good luck, though!
Getting Started With Search
A search index is a collection of files that will live on your web server (within App_data
by default) that will contain details of your web pages. The Lucene/Examine search will query these files, rather than the Umbraco database. This means your search queries will be fast and will not affect site performance. If you want your search to only search certain pages on your site, you'll need to create a new custom search index. If you have a blog and you only want your blog posts to be searchable, you would create a search index that is limited to the blog post document types. In order to get started with a new index, you will need to define one in config. The files to do this can be found within the Config
within the sites webroot. In this folder, you should find two files:
Within ExamineIndex.config
, create a new index:
To create an index, follow the format listed in the example above. In IncludeNodeTypes
add the document types you want to be included in the search and in IndexUserFields
, include the custom fields within the document types you want to be included. Within IndexAttributeFields
add the default Umbraco document-type properties that you want to be included in the search index. The other important thing to set is the index name. You can set the index name to be whatever you want it to, it will never be public-facing. You will use this name to reference your index in your code. Enabling an index is done within the ExamineSettings.config
file. The config required to enable an index looks like this:
With this set, you can finally start writing the search code!
In this code, I'm assuming you're using route-hijacking with Umbraco. In terms of architecture, it is considered bad practice to put logic within your views so you want to do as much logic in your controllers as possible. In the example above, within the default Index
action, the ExamineManager
is used. The index name is passed in to select which search index to use. The name
is the value you defined in the step above. To perform a search, a search parameter called searchTerm
is passed in to get relevant results. LINQ is used on results to ordering the search results based on relevance. This data is then passed back to the view. This code is using RenderMvcController
so the routing should be hooked up for you. Remember to create a document type called SearchPage
within the CMS though!
The code in the view is pretty simple. Iterate through the results collection and render a list of HTML elements. I have omitted the code to call the controller. I would probably use AJAX and Javascript to call the endpoint. More information on how to do this can be found here. Happy Coding 🤘