Code Ramblings

My blog all about programming and .NET

Linq To Examine - A Short Introduction

December 6, 2015 19:43 by Craig Noble

Introduction

Linq to Examine is a project I have been working on for a while. It allows you to write queries based on Strongly Typed models and retrieve the results as that model from the Lucene indexes that Umbraco uses. You can view the documentation here.

As a senior developer I have spent a lot of time teaching other developers how to query the Lucene indexes, which was either via raw Lucene queries or by chaining queries using Umbraco Examine Fluent API. With so many different ways to access the index, I thought it would be nice to have a simpler interface to the index and for the developers to have a familiar method of accessing data from within C#.

You may be asking "why would you use Lucene and when should you use the Lucene indexes?". To be honest, there are a few factors to consider. You should probably ask yourself the following questions:

  • Are you potentially loading a lot of content (even paginated)?
  • Are you going to filter the results once you have them?
  • Is your content coming from more than one source node?

If your answer is yes to any of the above questions, then I imagine you will benefit from using the Lucene index. So why Lucene? Lucene is good for this because it's extremely fast at querying data stored in its indexes, queries happen directly at source and data can be automatically scored based on the query passed to it. Comparing Lucene to a solution where you access Umbraco's XML Cache and load everything in to memory to find valid results, Lucene is definitely a better solution!

What are your options when you want to access the Examine Index?

Umbraco Examine Fluent API

This is the built-in option that Umbraco provides out of the box. Let's look at a code example:

var Searcher = ExamineManager.Instance.SearchProviderCollection["ExternalIndexer"];
var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or);
var query = searchCriteria.Fields("nodeName","hello").And().Field("metaTitle",hello").Compile();
var searchResults = Searcher.Search(query);

Here we are trying to find a content node that has "hello" in its name, or any content that has "hello" in the metaTitle property.

Raw Lucene Query

This is another built-in option that Umbraco provides out of the box. Here is a code example for the same query as above:

var Searcher = ExamineManager.Instance.SearchProviderCollection["ExternalIndexer"];
var searchCriteria = Searcher.CreateSearchCriteria(BooleanOperation.Or); 
var query = searchCriteria.RawQuery("nodeName:hello metaTitle:hello");
Searcher.Search(query).

Linq To Examine

Using Linq to Examine, you can do it as you would in good ol' C#. Here is the code example:

var results = new Index<SearchResult>().Where(r => r.Name.Contains("hello") || r.MetaTitle.Contains("hello")).ToList();

Conclusion

So that was a short introduction to Linq To Examine and its alternatives. I hope you can see why I wanted to start writing this library for Umbraco. It has certainly saved a lot of time and headaches. I have spent a lot of time fine-tuning the lucene queries it generates, so that it provides me with expected results.

In part 2, I will get showing you how to get started with Linq To Examine so that you can start getting data from the Lucene index!

If you'd like to try the package out, you can download the Umbraco package here, or you can download via Nuget here.

Please feel free to add any comments. I will answer any questions you have

Recent posts

  • How to achieve an A rating on Qualys SSL Labs with Windows

    I went to Qualys SSL Labs recently to find out how secure my SSL certificate was and unfortunately received a C rating. This did surprise me, because this was based on a base Windows 2012 R2 installation with all the new Windows Updates. See here how to fix this.

  • C# 6 - String Interpolation

    String Interpolation is a new feature to C# that was introduced in version 6. It allows you to create a string with one or more placeholders in it whereby the placeholders are then evaluated and replaced with values relating to variables within the available context. Here's a short summary of what it allows you to do.

  • Bonobo Git Server - GitHub Trends

    Hosted Git services such as GitHub and BitBucket have a number of benefits. No server maintenance, constantly being updated with new features and bug fixes, not to mention a good community and a whole host of online tutorials. But can Bonobo Git Server convince me self-hosted Git Server solutions are a good idea?

  • Let's Encrypt - How to set up on a Windows Server and help make the Web a safer place!

    Let's Encrypt is a free tool that allows you to create, manage and auto renew SSL certificates for web services, web applications or any sort of applications where secure transportation of data can utilised! Learn how to install it on your Windows Server!

  • Linq To Examine - A Short Introduction

    Linq to Umbraco is an open source library for Umbraco that allows you to easily access data from the Lucene Index. Here I'll introduce you to Linq To Examine, which will help you write fast pages that can access lots of data and perform searches with minimal code!