Sitecore Search: Difference between “Where” and “Filter”?

Anton Tishchenko
Anton Tishchenko
Cover Image for Sitecore Search: Difference between “Where” and “Filter”?

Imagine a situation, when you need to filter search result items based on the presence of layout.

ISearchIndex selectedIndex = ContentSearchManager.GetIndex(Search.Configuration.IndexName);
using (IProviderSearchContext context = selectedIndex.CreateSearchContext())
{
    var query = context
        .GetQueryable<SomeSearchResultItem>();
    
    //Should you use "where"?
    query = query.Where(i => i.HasLayout);
    //Or should you use "filter"?
    query = query.Filter(i => i.HasLayout);
}

Both methods will work. Usage of Where will give you only items with a layout. And usage of Filter will give you only items with a layout. But why does Sitecore have 2 APIs that do the same?

Indeed they are similar, but they are not the same. If we look under the hood for Solr queries that we get then we will see:

The difference between q and fq parameters is that q is used for relevance calculation and fq is not used. That is why it is a little bit better to use a Filter for query conditions that don't influence relevance. Also, fq relies on filterCache, which makes it faster compared to q.

You should not expect that replace of q to fq will make a huge performance boost. Most probably, you will not notice the difference. But if you can write more optimized code - do it! Always use Filter instead of Where, when it is possible.