Subscribe

Close

Thank you for visiting!

Please consider subscribing to the RSS feed or following me on Twitter.

Get Started Using Solr for Search with ASP.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. I'll show you how. (You can also download the sample app right off, if you'd like to)

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).

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:

Solr folder in Tomcat

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.

Sample ASP.NET app with SolrNet for download

I've developed a sample web application for you, using SolrNet for search in ASP.NET.

Basically, you've got some data in the SQL Server database and use SolrNet to find items in the search index and present all you want from the database.

Here's what you should know:

  • Map fields to Solr using attributes (Player.cs in the classes folder)
  • This is sample code, not everything might be suitable for a production environment
  • You should use Linq to Sql, NHibernate or similar for better scaling and easier data access

Download the sample Solr app

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;
rptResults.DataBind();

Re-index all data:

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

solr.Add(players);
solr.Commit();

Remove from index:

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

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

solr.Delete(specificPlayer);
solr.Commit();

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:
<fields>
	<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" />
</fields>
<copyField source="firstname" dest="text" />
<copyField source="lastname" dest="text" />
<uniqueKey>id</uniqueKey>
<defaultSearchField>text</defaultSearchField>
<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.

Comments

Awesome! Looking forward to you getting your search implementation completed in our great project. :)

Hello,
Thanks for providing such a nice tool. I implemented the code as it is and I am unable to index data. The error is as follows

SEVERE: org.apache.solr.common.solrException: ERROR: unknown field 'firstname'

I checked the schema.xml file and it has firstname. Please let me know what am I doing wrong. Thanks.

Restarting the TomCat resolved the issue. Also, I commented the DataImporter section in the schema.xml file for other error.

Pat - I'm glad to hear you're using Solr and my solution, and you just had to restart TomCat to get Solr working too!

Hi
thank you for this article
but
I follow up and apply all the steps but in the end
I taked this error.

(my Tomcat serve still work I install all the thing)
"The remote server returned an error: (400) Bad Request."

Take me a feedback for this problem

best regards

@dejavu:

Make sure your XML files are well-formed and Solr fields are of the correct type. You can also try restarting Apache.

Good luck!

Thanks for the article.Its really great.I wnat to know how to index .Doc,.txt and .PDF format.

How will I use a LatLongType field in .NET with SOlrNet. I know we can modify parser techniques. But I really don't how? Could you please point me in the right direction?

Where can I find Configure Tomcat window ?

I'm getting this error when I got to http://localhost:8983/solr/

There are no SolrCores running.
Using the Solr Admin UI currently requires at least one SolrCore.

great article

which changes are necessary to work this app on solr 4.6.0 instead of 1.4.1??

i am getting error at
<---------------
Server Error in '/' Application.
No connection could be made because the target machine actively refused it 127.0.0.1:8080
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:8080

Source Error:


Line 43: var specificPlayer = new PlayerRepository().GetPlayer(player.FirstName, player.LastName, player.Position);
Line 44:
Line 45: ----------> solr.Add(specificPlayer);
Line 46: solr.Commit();
Line 47: }


Source File: C:\Users\Kishan\Desktop\SolrSampleWithSolrNet(1)\Solr\Classes\DefaultIndexer.cs Line: 45
----------------->

in visual studio i am getting error
---------
solrconnectionException was unhandeled by user code
Unable to connect to the remote server

plz help me
Thanks
----------

To the top