Monday, June 29, 2015

Getting Started with Sitecore Content Search using Lucene or Solr

Using Sitecore Content Search with Solr is basically identical to that with Lucene at the general use level, with the exception of the configuration.  Solr does have some more advanced features that Lucene does not have. 
To get started though with either all we need is a search context.  We can get this using the context item (or any item for that matter).  We simply cast the item to the SitecoreIndexableItem class and pass it into ContentSearchManager.CreatesearchContext and that will give us our context.

This is generally set up as:
using (var context = ContentSearchManager.CreateSearchContext((SitecoreIndexableItem)item)){
   //search code….
}

From inside this context we can get our search results by calling the generic method GetQueryable that takes all types that derive from SearchResultItem on our context and using Linq to filter our results.

An example where we get all items under the current item:
var results = context.GetQueryable<SearchResultItem>().Where(i => i.Paths.Contains(item.ID));

If you have custom fields you want to search on it is highly recommended that you create a class that derives from SearchResultItem and add your fields as properties.  What is really nice with this is that Sitecore content search will reflect on this class and do the search on those properties.

An example with an item with an author field:
public class MyCustomResultItem: SearchResultItem{
   
public string Author { get; set; }
}
var results = context.GetQueryable<MyCustomResultItem>().Where(i => string.Equals(i.Author,”me”));

At this point you can iterate through results and get the items using .GetItem() on each resulting item.

Using SignalR with Sitecore

If you are unfamiliar with SignalR and need to set it up I will be posting an intro to SignalR on my other blog (Programming Medeley) in a few days.  Otherwise reading this post I will assume that you have a basic understanding of what SignalR is and how it works.
When developing in Sitecore with SignalR there is one major issue that you will run into (aside from basic routing).  That issue is the fact that you do not have a Sitecore site context within your SignalR context.  This causes issues with getting item URLs or using the Glass Mapper.

Missing Site Context

The fix for this is actually quite simple though.  You simply need to set the Sitecore site context on every request. 
To get the SiteContext you simply call Sitecore.Sites.SiteContextFactory.GetSiteContext and pass in the hostname (which can be found within the context of the Hub as this.Context.Request.Url.Host) and an empty path and it will give you a SiteContext. 
Once we have this all we have to do is set the current site to this context by setting Sitecore.Context.Site to this SiteContext. 
I use the single line:
Sitecore.Context.Site = Sitecore.Context.Site ?? SitecontextFactory.GetSiteContext(this.Context.Request.Url.Host, “/”);

Routing

As for the routing issue that you may run into, this is the same as setting up WebApi.  You need to ensure the route is registered in the Application_Start of the Global.asax. 

The command for this is (using the Global Configuration):
GlobalConfiguration.Configuration.Routes.MapHttpRoute(“signalrHub”, “
signalr/{controller}”);