Click here to Skip to main content
14,489,038 members

Using Solr for Search with .NET(C#)

Rate this:
3.43 (9 votes)
Please Sign up or sign in to vote.
3.43 (9 votes)
19 Oct 2012CPOL
Set-up of solr with .NET


Solr is an advanced search coming from Apache's Lucene project. Thanks to SolrNet, a .NET library for Solr, it is quite convenient to use Solr for search in ASP.NET.   

Install Apache Tomcat and Solr  

First of all, make sure you get the latest version of Apache Tomcat and Solr. (I installed Tomcat 7 and Solr 1.4.1 (zip version) as of September 2010.) 

Tomcat installation 

When installing Tomcat, make sure to remember the port you specify (normal for Solr is 8983). After installation, the Apache Tomcat Properties window should popup. If not, find Configure Tomcat in the start menu and make sure the web server's started. If it's started, you should find the default Tomcat startpage if you browse to http://localhost:8983.  

Solr installation 

Before you install Solr, stop the Tomcat web server (through the Configure Tomcat window).

  • Download Solr 1.4 from here
  • Download the latest Java JDK

When you've downloaded the Solr zip file (make sure it's the zip version!), unzip the archive and find the dist folder. In the dist folder, find the apache-solr-1.4.1.war file and copy it to C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps, renaming it to solr.war.

Now, we also need to create the Solr folder, which will host our Solr configuration files, indexes and so on. I created C:\Solr. You'll also need to copy the contents of the apache-solr-1.4.1\example\solr folder to your newly created Solr folder. When you're done, you should at least have the bin and conf folder.

Finally, we need to tell Tomcat where our Solr folder is located. Open up the Configure Tomcat window, navigate to the Java tab and add this row to Java Options: 

// -Dsolr.solr.home=c:\solr

It should look like this:

Image 1

Now, you should first start the web server and then be able to navigate to http://localhost:8983/solr. Installation and basic configuration done! 

Quick look at the configuration files

The Solr configuration files are important - you will use them to tell Solr what should be indexed and not. The most important config files are schema.xml and solrconfig.xml. These are located in the C:\Solr\conf folder.

Easier use of Solr with the SolrNet library

SolrNet is a great .NET library for Solr, making it all easier. Download assemblies (and samples) on the SolrNet Google Code page.

I'd like to thank A. Friedman for his contribution to the Solr and ASP.NET world. Here's his great blog post on Solr and SolrNet

Code snippets using SolrNet

Here's some code snippets from my Solr app. You can find them in the source code, although I found it being a good idea to post code for a couple of common actions using Solr and SolrNet for search.

Search the index and bind to Repeater: 

var search = new DefaultSearcher()
	.Search(query, 10, 1);

rptResults.DataSource = search.Result;

Re-index all data: 

new SolrBaseRepository.Instance<Player>().Start();
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Player>>();
var players = new PlayerRepository().GetPlayers();


 Remove from index: 

new SolrBaseRepository.Instance<Player>().Start();

var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Player>>();
var specificPlayer = new PlayerRepository().GetPlayer(id);


Adding multiple fields to the search index

By standard, Solr lets you index one field only, thanks to the defaultSearchField in schema.xml. It's easy to turn on indexing of multiple fields though, using copyField and an additional field which takes multi values.

What you have to do is to edit schema.xml a bit:

  1. Setup the fields you want to get indexed, using field.
  2. Create an additional field called "text", setting its multiValued property to true.
  3. Use copyField to copy data to this additional field.
  4. Use this additional field, "text", as the defaultSearchField.
Here's how: 
	<field name="id" type="int" indexed="true" stored="true" required="true" /> 
	<field name="firstname" type="text" indexed="true" stored="false" required="false" /> 
	<field name="lastname" type="text" indexed="true" stored="false" required="false" />
	<field name="text" type="text" indexed="true" stored="false" multiValued="true" />
<copyField source="firstname" dest="text" />
<copyField source="lastname" dest="text" />
<solrQueryParser defaultOperator=uot;AND" />  

Debugging Solr

If you encounter any problems with Solr, try this to get it working:

  • Turn off elevate.xml handler (comment appropriate lines in solrconfig.xml).
  • Case sensitive configuration files - make sure you spell copyField, multiValued etc correctly.
  • In solrconfig.xml, make sure you use matching data types to those you've defined in your ASP.NET app.

More Solr

Solr is really powerful and gives you a lot of options. I recommend the Solr Wiki for more information on what actually is possible.  

This link might also help you.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Software Developer from India.

Enthusiastic developer and like to work in C#, ASP.Net, ASP.Net MVC, WebApi, SQL Server and JavaScript/jQuery.

Comments and Discussions

QuestionLatest link of SolR 1.4.0 Pin
Ashish Gope6-Apr-18 20:47
MemberAshish Gope6-Apr-18 20:47 
GeneralMy vote of 1 Pin
kopernik.ru17-Jul-17 7:04
Memberkopernik.ru17-Jul-17 7:04 
Questionmy vote of 1 Pin
jerome dela cruz24-Jul-16 17:26
Memberjerome dela cruz24-Jul-16 17:26 
GeneralGreat Article. Pin
Ali Rasoulian20-May-15 1:56
professionalAli Rasoulian20-May-15 1:56 
GeneralMy vote of 1 Pin
Philip Murray25-Sep-14 1:00
MemberPhilip Murray25-Sep-14 1:00 
GeneralRe: My vote of 1 Pin
Kaushikhalvadiya25-Sep-14 22:52
professionalKaushikhalvadiya25-Sep-14 22:52 
GeneralRe: My vote of 1 Pin
Philip Murray26-Sep-14 3:22
MemberPhilip Murray26-Sep-14 3:22 
BugNo source code Pin
tlhIn`toq3-Jun-14 12:15
MembertlhIn`toq3-Jun-14 12:15 
GeneralRe: No source code Pin
Kaushikhalvadiya25-Sep-14 22:52
professionalKaushikhalvadiya25-Sep-14 22:52 
Questionsource code Pin
gbreb5-Feb-14 21:59
Membergbreb5-Feb-14 21:59 
AnswerRe: source code Pin
Kaushikhalvadiya25-Sep-14 22:49
professionalKaushikhalvadiya25-Sep-14 22:49 
AnswerRe: source code Pin
Kaushikhalvadiya25-Sep-14 22:51
professionalKaushikhalvadiya25-Sep-14 22:51 
QuestionGood Article Pin
Member 31169646-Dec-13 9:50
MemberMember 31169646-Dec-13 9:50 
AnswerRe: Good Article Pin
Kaushikhalvadiya11-Dec-13 6:18
professionalKaushikhalvadiya11-Dec-13 6:18 
GeneralRe: Good Article Pin
Member 311696416-Dec-13 4:54
MemberMember 311696416-Dec-13 4:54 
GeneralMy vote of 4 Pin
dale.newman1-May-13 8:56
Memberdale.newman1-May-13 8:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 19 Oct 2012


22 bookmarked