Fortis Release: 3.5.1 – search upgrades!

We’ve just released version 3.5 of Fortis a few days ago and have now also released a minor update bumping the version to 3.5.1. The main focus of the new version was to re-work the current implementation of how we handle interacting with the Sitecore search API. Prior to Sitecore 7.2 there were some issues with the Sitecore expression parser and the fact it didn’t handle using interfaces amongst a few other issues. For this reason with Fortis 3.0.X we would need to inject in our own custom expression parser for Sitecore to use which meant using our own custom index classes. Thankfully Sitecore 7.2 onwards corrects these issues which means we can now just add in our own custom document mapper an use the standard Sitecore search API! Below we outline what the main changes are with the new version and what to expect if you’re upgrading from version 3.0.X.

Quick Summary

  • Sitecore 7.2+ is now required
  • The dependencies on Lucene and Solr have been moved from the Fortis binary and into their own binaries. There are now two new NuGet packages for Lucene and Solr depending on which search you are using.
  • The .Search<T>(IProviderSearchContext context, IExecutionContext executionContext = null) method on the item factory has been moved to a new item search factory. This method is now also obsolete and will no longer be used but has been kept for backwards compatibility.
  • New search methods have been added which accept an IQueryable<T> and apply filters.
  • The GetResults<TSource> Sitecore extension method has been wrapped, the use of this is optional.

Requirements

Sitecore 7.2+ – see the installation page in the quick guide for a full matrix on what versions of Fotis work with Sitecore.

Breaking changes

IQueryable<T> Search<T>(IProviderSearchContext context, IExecutionContext executionContext = null)

This search method previously on the item factory has now been moved to the new item search factory. See further down for details on how to use the new item search factory.

New Features

A new IItemSearchFactory has been added which is now the main factory for handling search queries. Below is a simple example of using the new factory and the new FilteredSearch(IQueryable queryable) method. This method applies the newly added extension methods for adding filters to the queryable based on the IItemWrapper interface. The following filters are added when using this method.

  • Template (based on the interface passed in as T)
  • Current context language (Sitecore.Context.Language)
  • The item is not a standard values item
  • The latest version of the item only
public class MySearchProvider
{
	private readonly IItemSearchFactory _itemSearchFactory;

	public MySearchProvider(IItemSearchFactory itemSearchFactory)
	{
		_itemSearchFactory = itemSearchFactory;
	}

	public void Run()
	{
		List<Page> results = new List<Page>();

		using (var searchContext = ContentSearchManager.GetIndex("<index name>").CreateSearchContext())
		{
			IQueryable<IPage> queryable = searchContext.GetQueryable<IPage>();

			queryable = _itemSearchFactory.FilteredSearch<IPage>(queryable)
						.Where(item => item.Title.Value == "My Page")
						.OrderBy(item => item.Title);

			results = queryable.ToList();
		}
	}
}

If you do not want to have the filters automatically applied you can use the Search(IQueryable queryable) method. This will only apply the template filter to the queryable. If you need to customise the query completely you can omit using the FilteredSearch or Search methods completely.

List<Page> results = new List<Page>();

using (var searchContext = ContentSearchManager.GetIndex("<index name>").CreateSearchContext())
{
	IQueryable<IPage> queryable = searchContext.GetQueryable<IPage>();

	queryable = _itemSearchFactory.Search<IPage>(queryable)
				.Where(item => item.Title.Value == "My Page")
				.OrderBy(item => item.Title);

	results = queryable.ToList();
}

Fortis now also works seamlessly with the Sitecore extension method GetResults(). We also provide a method on the item search factory which wraps the Sitecore method and instead returns interface versions of the SearchResult and SearchHit classes. The returned object can also be used outside of the search context using.

using (var searchContext = ContentSearchManager.GetIndex("<index name>").CreateSearchContext())
{
	IQueryable<IPage> queryable = searchContext.GetQueryable<IPage>();

	queryable = _itemSearchFactory.Search<IPage>(queryable)
				.Where(item => item.Title.Value == "My Page")
				.OrderBy(item => item.Title);

	var results = itemSearchFactory.GetResults(queryable)
}
Posted in Fortis, Release and tagged , , .