Chris Umbel

Solrnet, a Solr Client Library for .Net

One of the strength's of Solr is it's ease of consumption by other platforms due to its REST API and response writers which include XML, JSON, native Ruby and native Python code.

If you're trying to consume a Solr service from .Net you could easily use a WebClient and parse the results with .Net's System.Xml namespace and perhaps even build an object wrapper on top of it. Luckily that work's already been done with the solrnet library.

In this post I'll outline the fundamentals of solrnet usage.


This article assumes you have a .Net development environment such as Visual Studio and a functional Solr install in servlet container. I'll also assume that you understand how to configure Solr's schema. If that's not the case please consult the official Solr wiki.

Sample Schema

For demonstrative purposes I'll assume the following field declarations in schema.xml.



Project Setup

With the basic system in place now it's time to download solrnet from its project site on Google Code then add references to SolrNet.dll and Microsoft.Practices.ServiceLocation.dll (included with SolrNet) to a project.


Now let's write some bloody code! Consider the following class declaration which defines the document's we'll be working with. In this case it's an article, much like a blog post, with a key, title, textual content and a list of tags.

Notice the SolrUniqueKey and SolrField attributes decorating the properties. That facilitates the mapping of the properties to field's in Solr.

using System;
using System.Collections.Generic;
using SolrNet;
using SolrNet.Attributes;
using SolrNet.Commands.Parameters;
using Microsoft.Practices.ServiceLocation;
class Article {
    public int ID { get; set; }

    public string Title { get; set; }

    public string Content { get; set; }

    public List Tags { get; set; }

Writing Data

Model defined we can now connect to Solr and save some articles. The following code locates our Solr instance (running locally on port 8080 in my case), creates some documents and commits them to the index.

// find the service  
("http://localhost:8080/solr"); ISolrOperations
solr = ServiceLocator.Current.GetInstance>(); // make some articles solr.Add(new Article() { ID = 1, Title = "my laptop", Content = "my laptop is a portable power station", Tags = new List() { "laptop", "computer", "device" } }); solr.Add(new Article() { ID = 2, Title = "my iphone", Content = "my iphone consumes power", Tags = new List() { "phone", "apple", "device" } }); solr.Add(new Article() { ID = 3, Title = "your blackberry", Content = "your blackberry has an alt key", Tags = new List() { "phone", "rim", "device" } }); // commit to the index solr.Commit();

Basic Querying

Of course the primary purpose of Solr is performing search queries. Consider the following examples which does a general full-text search on the word "power" and a tag search for "phone":

// fulltext "power" search  
Console.WriteLine("POWER ARTICLES:");
powerArticles = solr.Query(new SolrQuery("power")); foreach (Article article in powerArticles) { Console.WriteLine(string.Format("{0}: {1}", article.ID, article.Title)); } Console.WriteLine(); // tag search for "phone" Console.WriteLine("PHONE TAGGED ARTICLES:"); ISolrQueryResults
phoneTaggedArticles = solr.Query(new SolrQuery("tag:phone")); foreach (Article article in phoneTaggedArticles) { Console.WriteLine(string.Format("{0}: {1}", article.ID, article.Title)); }
which produces the following output
1: my laptop
2: my iphone

2: my iphone
3: your blackberry


One of my personal favorite features of Solr is faceting which enables aggregate counts to be returned along with query results. Faceting is well supported in solrnet.

The following example displays counts per tag of articles matching the "device" tag:

articles = solr.Query(new SolrQuery("tag:device"), new QueryOptions() { Facet = new FacetParameters { // ask solr for facets Queries = new[] { new SolrFacetFieldQuery("tag") } } }); foreach (Article article in articles) { Console.WriteLine(string.Format("{0}: {1}", article.ID, article.Title)); } Console.WriteLine("\nTAG COUNTS:"); foreach (var facet in articles.FacetFields["tag"]) { Console.WriteLine("{0}: {1}", facet.Key, facet.Value); }

with the following output:

1: my laptop
2: my iphone
3: your blackberry

device: 3
phone: 2
apple: 1
computer: 1
laptop: 1
rim: 1

Wrapping up

Solrnet makes consumption of a Solr service easy, but I've only covered the basic concepts here. Other features of Solr such as spell checking and match highlighting are also handled. The solrnet wiki will tell you more.

Mon Mar 08 2010 23:03:00 GMT+0000 (UTC)

Follow Chris
RSS Feed