By Kaushikhalvadiya, 19 Oct 2012
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
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.
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 createdC:\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:
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; 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:
- Setup the fields you want to get indexed, using field.
- Create an additional field called “text”, setting its multiValued property to true.
- Use copyField to copy data to this additional field.
- Use this additional field, “text”, as the defaultSearchField.
<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" />
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.
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