In this tutorial, you will learn how to implement a simple search engine that you can use within your Umbraco 8 website. Out-of-the-box, Umbraco uses the Lucene search index. Every time you save or modify a page within Umbraco the search index is updated using Examine. You can find the outputted search index files within the
App_data folder within the webroot. To create a simple site search we will use this index as the source of search data.
Examine is a Umbraco API layer that sits on top of Lucene. When searching for pages within Umbraco you will be interfacing with the Examine APIs. To perform a search you will need to get access to an
indexer. The default Umbraco index for published content is called
ExternalIndex. This is the index we will be using to power our search as it contains all the content pages and media items that have been published on the site. The way the search works has changed since Umbraco V7. For example, if you used Umbraco 7 or below, you may notice that in Umbraco v8 the
ExamineSettings.config which used to live within the
config folder, no longer exists. The way in which we will build and configure indexes for V8 has changed. If you find online tutorials that reference V7 and below, they will not work. To create your own custom index, you will need to create a custom Umbraco component. We will not go into custom indexes in this tutorial, but it is useful to note that
ExamineSettings.config is no longer a thing.
In order to write a search index, you will need to query the index. This can be done in code like this:
Performing a simple search against everything in the index is done like this:
If you wanted to only search for pages filtered by a certain document-type, you would update the query like this:
To add sorting by a certain field, you can do this:
That really is all there is to create a simple site search. Create a page controller as normal that accepts a query-string parameter called a query. Within your controller, write a simple controller that queries the index using the code listed above. The code to do this could look like this:
With the view model code looking like this:
This code is also using the Nuget package PagedList. The view will be determined by your design, however, all the view will contain is a simple textbox, button and a way to render the list:
You should not need to worry about any custom routing. That covers pretty much the basics of creating a simple search. Happy coding!