Working with the Sitecore Query Builder field using SOLR

Sitecore’s query builder can be a very versatile tool for content authors if setup right. It can give the content authors the option to virtually select any set of items to display on a given module.
This, paired with the right set of custom facet definitions, can work wonders to optimize the content author experience.


Please refer my previous post about Adding search facets on computed fields in Sitecore 9 with SOLR, this will give you an idea about creating facets in the content editor folders / item searches.

The question addressed here, is once you have the content editor built query with you in the back end – how do you run this against your search engine to resolve this into a set of matching items?

Here’s the code to use with SOLR:

using Feature.Articles.Models;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.Linq;
using Sitecore.ContentSearch.Utilities;
using Sitecore.Data;
using Sitecore.Data.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using SearchHelper = Foundation.Search.Solr.Helpers.SearchHelper;

namespace Feature.Articles.Services
    public class ArticleTilesService
		public IEnumerable GetPagesByQuery(string query)
		    ISearchIndex index = ContentSearchManager.GetIndex("sitecore_master_index");
            using (IProviderSearchContext context = index.CreateSearchContext())
				List stringModel = SearchStringModel.ExtractSearchQuery(query);
				IQueryable result = LinqHelper.CreateQuery(context, stringModel);

				string contextLanguage = Sitecore.Context.Language.ToString();

				result = result
					.Where(r => r.Language == contextLanguage)
					.OrderByDescending(r => r.ArticleDate);

				SearchResults resultArticles = result.GetResults();
				return resultArticles.Select(r => r.Document).ToList();

Please note – It is advisable to add the following constraints / filters to the search query from back end code, in addition to the query passed in from Sitecore, simply because it is a rather simple oversight to be made on the part of the content authors.

  • Search Location: This should typically at least be your site node or depending on the context, you could make it as specific as the context allows you to be.
  • Search Language: This is an easy oversight by the content author – do remember to add this filter by context language to get desired results.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s