How to: Sitecore facet on comma separated single-line text

Intro:


How difficult is it to find a quick code example to implement facets on “Single-Line Text-field” in Sitecore using Lucene search? Well, I had to search at least 15~20 min to find the answer.
Even the one I found is not exactly what I was looking for. Again, I was looking for quick code snippet that I can insert into my code and continue on my business logic vs technically understanding how contest search Api works. So, that is the backdrop for this post.

What will you see in this post:
In this post I would like to share a quick code snippet of how exactly to use a single-line text field for faceting using Lucene.

Single-line text in Sitecore:


Singe line text field with comma separated values 


Code snippet:

using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
{
    results = context.GetQueryable<SearchResultItem>()
                        .Where(sri => sri.Parent == Sitecore.Data.ID.Parse(Guid.Parse("{2E8565A9-24EC-441A-8D44-EA47779D5B05}"))) //product repo parent
                        .Where(i => i.TemplateId == Sitecore.Data.ID.Parse(Guid.Parse("{535C290E-4053-4E5F-8E62-372C2304AA30}")))
                        .FacetOn(f => f["ProductCategory"]).GetResults();
                    //For every facet that you want, just continue to use the FacetOn()
}


In the above code, I was not sure on how to set the item for Search context. So, I used the ‘item.parent’ clause to restrict the items returned. Let me know in comments if there is any better method.

Unexpected issue:

After setting up the above, I was sure my code will execute. However, the demo gods were not on my side. I ran into below issue.

[NullReferenceException: Object reference not set to an instance of an object.]
   Sitecore.ExperienceExplorer.Business.Pipelines.HttpRequest.EnableExperienceModePipeline.Process(HttpRequestArgs args) +950
   (Object , Object[] ) +73
   Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +483
   Sitecore.Pipelines.DefaultCorePipelineManager.Run(String pipelineName, PipelineArgs args, String pipelineDomain) +21
   Sitecore.Nexus.Web.HttpModule.’ (Object  , EventArgs  ) +531
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +91


Root cause for this issue is because of my overridden setting of ‘website’ site in my custom config as below
<sites>
      <site patch:instead="site[@name='website']"
            name="website"
            virtualFolder="/"
            physicalFolder="/"
            rootPath="/sitecore/content/MyRoot "
            startItem="/home"
            database="web"
            domain="elasticpath"
            allowDebug="true"
            cacheHtml="true"
            htmlCacheSize="50MB"
            registryCacheSize="0"
            viewStateCacheSize="0"
            xslCacheSize="25MB"
            filteredItemsCacheSize="10MB"
            enablePreview="true"
            enableWebEdit="true"
            enableDebugger="true"
            disableClientData="false"
            cacheRenderingParameters="true"
            renderingParametersCacheSize="10MB"/>
    </sites>
Once I removed the highlighted it started working again. Any explanations from Sitecore community?

More information on above error is available on blog below
http://codingdennis.blogspot.in/2017/02/sitecoreexperienceexplorerbusinesspipel.html

Hope this blog comes handy to someone in future


Comments