Do you need to implement a site search within your Umbraco website? Then read on. Search is technically one of the pages on a lot of projects. A basic search is pretty easy to get started, but the further down the rabbit hole you get, the more complicated things become.

Before I panic you too much, this article is aimed at the majority of you who probably only need a basic site search, that allows people to search and get relevant results. If you need things like faceted filtering, indexing of content from an API, search that integrates with your e-commerce PIM then this article isn't for you, good luck, though!

Getting Started With Search

I've talked previously about Lucene/Examine within Umbraco here.

This article is going to follow a very similar path, but the main difference will be the focus towards the MVC parts, more than the Lucene bit. To get started, you'll need to configure two config files to create a new search index. A search index is a collection of files that will live on your web server that will contain details of your web pages.

The search will do all the main searching against these files, rather than the Umbraco database. In order to get started, you need to create a new index and define everything that you want to be included in your site search. In your 'Config' folder you should find two files:

First, in the ExamineIndex.config we will create a new index, like so:

To create an index, you need to follow the format listed above. In the IncludeNodeTypes add the document types you want to be included in the search. In the IndexUserFields you include the custom fields within your document types you want to be included.

Within IndexAttributeFields you add the default Umbraco properties you want to be included. The other important thing is the set name. This is a custom name you can call whatever you want. This is the name you will use to reference your index in your code, so it's worth making a note of! Next, we'll need to add an index and search provider in Examine Settings.config, which will look like this:

This node needs to be placed within the -> tag. Now we can finally start writing some code!

Like most of my MVC tutorials, I'm assuming you're using route-hijacking. In terms of architecture, this is the approach you should follow and it is considered bad practice to put logic within your views.

In the example above, we use the ExamineManager to select which search provider to use. This is the name you defined above. We then perform the search, passing in a search term into the 'Search' method within the provider and then I use some simple LINQ to ordering the results based on the relevance.

As long as everything has been set-up correctly, your query should return results. Within the view, you can then use Razor to render the list.