Strategies for creating a meaningful Sitecore site search experience – Part 2

In the previous blog we explored indexing strategies that could be used for SOLR or Lucene. Those strategies took the configuration approach with no coding involved.

In this section of the blog we go beyond SOLR and Lucene and take a look at some strategies that can be used regardless of SOLR or Lucene and write custom code/logic. Moreover, this also enables you to add some sort of business logic behind the scenes should there be any need.

Strategy 3: Selective indexing Strategy – Code Approach

 

There are situations where further granulization is required. The selective indexing strategy using the configuration approach is not making the cut. You can build a custom processor to further granularise indexes. You can do it using <indexing.filterIndex.inbound> pipeline.

In addition you can specify a path in the content tree and exclude an item from indexing. Just implement a custom processor that checks path of the item being processed and decides whether it should be indexed or not.

For Example:

public class CustomInboundIndexFilter : InboundIndexFilterProcessor
{
public override void Process(InboundIndexFilterArgs args)
{
if (args.IndexableToIndex.AbsolutePath.StartsWith(“<excluded path>”))
args.IsExcluded = true;
}
}

Please note that the code above is just a sample to give an idea of possible customization. Please adapt it to your needs.

Strategy 4: Isolate Search Strategy – Code Approach

Add logic that will isolate a search to a particular location in the tree (roots) and templates via custom code.

 

Code Snippet example:

if (args.TemplateIds.Any())

queryable = queryable.Where(this.BuildTemplateExpression<ContentSearchResult>(args.TemplateIds));

 

public virtual Expression<Func<T, bool>> BuildTemplateExpression<T>(IEnumerable<ID> ids) where T : SearchResultItem

{

var predicate = PredicateBuilder.True<T>();

 

return ids.Aggregate(predicate, (current, id) => current.Or(x => x.TemplateId == id));

}

 

if (args.Roots.Any())

queryable = queryable.Where(this.BuildPathsExpression<ContentSearchResult>(args.Roots));

 

public Expression<Func<T, bool>> BuildPathsExpression<T>(IEnumerable<ID> ids) where T : SearchResultItem

{

var predicate = PredicateBuilder.True<T>();

 

return ids.Aggregate(predicate, (current, id) => current.Or(x => x.Paths.Contains(id)));

}

 

Conclusion

The above strategies should enable you to work with your indexing or isolating the search path based on your circumstances. You can pick the configuration or adding custom logic approach and let the search provider return meaningful search results.

Happy Searching!

Leave a Reply