Using Solr from C# Made Dead Easy with SolrNet
I was having a conversation with an solutions architect a few days ago about a specific Solr project with .NET. He was telling me some of the details of what they were going to build, how they were going to call Solr’s REST API for querying and ….
Stop the press… Stop right there… I said….
You are going to call Solr’s REST API? Really? The fact that Solr has a REST API is massively useful, but why do you want to reinvent the wheel.
If you want to use Solr from .NET, I explained, the best way is to use SolrNet(https://github.com/mausch/SolrNet). SolrNet is an Apache Solr client for .Net that allows for you to use Solr from .NET in a super easy and efficient way. It abstracts Solr in such an easy way that you basically just create a POCO object (Plain Old Clr Object – not to wrongly called Plain Old C# Object as many people do) and presto, you have functions to Add, Delete, Commit and more. Let me show you a small sample I built about a week ago in literally just a few minutes to index a few thousand mails from Pluralsight’s (the best online training resource available IMHO – and yes I am a bit biased here!) author distribution list.
To create this demo, where I indexed about 4k emails I did the following
– Download Solr, 4.10 at the time [takes a few minutes to download]
– Clone collection1 and rename it authorslist, don’t forget to change the collection name
– Change in schema.xml so that you have the following fields (and remember this is just a quick and dirty example that I wanted to show how provide a searchable index of all author mails inside a Wordpress site. Looks like this:
<field name=”itemid” type=”string” indexed=”true” stored=”true” required=”true” multiValued=”false” />
<field name=”subject” type=”text_general” indexed=”true” stored=”true”/>
<field name=”sent” type=”date” indexed=”true” stored=”true” omitNorms=”true”/>
<field name=”sendername” type=”string” indexed=”true” stored=”true” multiValued=”false”/>
<field name=”recipients” type=”string” indexed=”true” stored=”true” multiValued=”true”/>
<field name=”body” type=”text_general” indexed=”true” stored=”true”/>
<field name=”htmlbody” type=”text_general” indexed=”false” stored=”true”/>
– Make itemid uniquekey instead of id
– Modify solrconfig.xml to add a new requesthandler, so that I can play a bit with facets and weights, nothing out of the ordinary
– And now to index the mails I just wrote a few lines of code to read from my Outlook, using the Office Primary Interop Assemblies (known as PIAs) in a console application
– Add SolrNet to the console app
– Create a POCO object to represent whatever I added to the schema. It should look like this. Please notice the attributes, which is what tells SolrNet what it is representing in Solr. This is my POCO object:
public class MailSolr {
[SolrUniqueKey(“itemid”)]
public string ItemId { get; set; }[SolrField(“subject”)]
public string Subject { get; set; }[SolrField(“sent”)]
public DateTime Sent { get; set; }[SolrField(“sendername”)]
public string SenderName { get; set; }[SolrField(“recipients”)]
public ICollection Recipients { get; set; }[SolrField(“body”)]
public string Body { get; set; }[SolrField(“htmlbody”)]
public string HtmlBody { get; set; }
}
– And write the following lines to index
ISolrOperations solr = GetSolr();
int it = 0;
foreach (Microsoft.Office.Interop.Outlook.MailItem i in lM)
{
it++;
MailSolr m = new MailSolr();
m.ItemId = i.EntryID;
m.Subject = i.Subject;
m.Sent = i.SentOn;
m.SenderName = i.SenderName;
m.Body = i.Body;
m.HtmlBody = i.HTMLBody;solr.Add(m);
}
solr.Commit();
As you can see adding a document with SolrNet is dead easy. Simply connect to Solr, create a new instance of your POCO object, solr.Add() and solr.Commit(). How much easier would you want it to be?
Next Tuesday I will talk about the NIH syndrome (not invented here) around SolrNet.
Hello Xavier,
I am currently developing smart search in .net web api application and using solr .. I went through your course in Pluralsight and it helped me set up with good standalone solr configuration as per my requirements.. I need help setting up high availability… actually we will have two webserver both will have same data of solr and in third server thinking to have zookeeper … how can I make connection and start inserting document via zookeeper in c#
Hello Tejendra. A load balancer is a good way to start when working with SolrCloud.