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();
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