Testing Sitecore ContentSeach API queries using the Linq Scratchpad

Every now and then, Sitecore keeps surprising me with small features, just waiting to be discovered. Recently I learned about the Linq Scratchpad admin page found in Sitecore, that allows you to create and execute queries using the ContentSearch API.

So what's the deal with this Linq Scratchpad anyway?

The Linq Scratchpad is a really cool little gem hidden under http://yourSitecoreInstance/sitecore/admin/linqscratchpad.aspx as part of the admin pages toolbox. It gives you the power to rapidly create and execute search index queries using the ContentSearch API, as you would do so in regular code. The great thing about this is that you can do experiments and fine-tune your queries in order to get the desired output you need, without the need for any code deployments whatsoever.

Enough talk, show me how it works!

When you navigate over to the Linq Scratchpad admin page, you'll see a code editor containing some sample code, along with buttons to run the query, clear the results and reset the page back to its default values:

By default, the sample code will fetch the first 10 items indexed into the search index. Additionally, the sample code provides you with a small example on how you can create your own custom SearchResultItem implementations with the TryYourOwnClass type.

The code can be executed by clicking the Run button in the lower right corner. Once executed, you'll be presented with a list of results returned from the search index:

In the result list, each row is a entry from the search index and each column represents the values of the properties on the mapped return type T, which is specified on the call GetQueryable<T> method (in this case SearchResultItem). Moreover, you also get an indication on how fast (or slow) your query is, making it possible for you to see if the changes you make to your query improves or decreases the performance of your query.

You've only seen the tip of the iceberg

Even though I've only just shown how to use the Linq Scratchpad using the default sample code, the ability to have a way to prototype ContentSearch queries is invaluable. Imagine that you want to test out which boosting value to use, adjust the fuzziness of a similarity query or something completely different. The Linq Scratchpad makes this possible without the need to write and re-deploy your code every single time you make a change. Moreover, the Linq Scratchpad supports everything you are able to do from your custom .NET solution, there really is no limits to what you can do - if the API supports it, you can try it out.

If you aren't familiar with this admin page, and work with the ContentSearch API, I highly recommend that you check out the Linq Scratchpad.

comments powered by Disqus