In this article we will look into how to use SOLR to add and
query data using SOLR.NET
Install Apache Tomcat and SOLR
1. Install Java version 6 or above
2.Download Apache Tomcat -Core: 32-bit/64-bit Windows
Service Installer (pgp, md5) http://tomcat.apache.org/download-70.cgi
After you install apache tomcat you can check if service is
running by checking the location -http://localhost:8080/
3.Download solr (zip) file http://www.gtlib.gatech.edu/pub/apache/lucene/solr/4.10.3/
Details on how to install and run SOLR is clearly stated in
the below awesome articles
Use SOLR.NET to connect to SOLR
So now that we have Apache and SOLR running let’s look at how
to use this in ASP.NET(C#)
1.Download SOLR.NET - https://github.com/mausch/SolrNet
2.Compile the project and get the dlls from SOLRNET project
(there will be SolrNet.dll, Microsoft.Practices.ServiceLocation.dll, HttpWebAdapters.dll
and the pdb and xml files)
3.Create an empty ASP.NET project
4.Create a new folder in this project and copy the dlls from
SOLRNET project (step 2) to that folder.
5.Add references to the 3 dlls
6.Add Global.asax and initialize SOLR.NET connection in
application start
protected void Application_Start(object sender, EventArgs e)
{
Startup.Init<SOLRPOST.PostData.SolrSearchResult>("http://localhost:8080/solr");
}
7.Create a class which has the same fields as the schema in
SOLR. For testing purpose I added only 2 fields from schema
public class SolrSearchResult
{
[SolrField("id")]
public string id { get; set; }
[SolrField("title")]
public string title { get; set; }
}
8.Now we are ready to add documents to SOLR! Given below is the
function to add documents to SOLR
public void AddData()
{
ISolrOperations<SolrSearchResult>
solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrSearchResult>>();
SolrSearchResult
test = new SolrSearchResult() { id = "changeme2", title = "changeme2" };
solr.Add(test);
solr.Commit();
}
9.To search data use the below function
public void SearchData()
{
ISolrOperations<SolrSearchResult>
solr = ServiceLocator.Current.GetInstance<ISolrOperations<SolrSearchResult>>();
SolrQueryResults<SolrSearchResult>
results = solr.Query(new SolrQuery("id:\"changeme2\""));
foreach (SolrSearchResult
result in results)
{
Response.Write(result.title);
}
}
11 comments:
SOLRPOST generates namespace not found error. I am obviously missing a Using setting. What is it?
it's simply a namespace of your project.
everything else works well at my side
thanks. it helped...
Thanks for the post!
Unfortunately I am getting exception in the following second line line:
Startup.Init("http://localhost:8080/solr");
ISolrOperations solr = ServiceLocator.Current.GetInstance>();
"The exception says: Object reference not set to an instance of an object"
Hello, where is Startup defined? I get the error "The name 'Startup' does not exist in the current context" in my following code:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Startup.Init(http://localhost:8080/solr);
}
Add "using SolrNet;"
great thank you very much. this post very helpful to me
I'm getting error on SOLRPOST.PostData line in Global.asax file.
It says SOLRPOST type or namespace name not found.
When I call search method it gives me below error,
System.Xml.XmlException: 'Data at the root level is invalid. Line 1, position 1.'
Please help.
for those who got SOLARPOST error in GLobal.asax , i am adding code here
place this code in Global.asax
Startup.Init(“https://localhost:8983/solr”);
It clears the issue.
Post a Comment