Solr

Brief:

In the last article we saw integration of solr. This article we will look into implementation of solr with .net. Solr.Net is an open source library which is available at https://github.com/mausch/SolrNet/. This article we will look into the basics of indexing using solr.net.

Need : 

To create a search engine for the website to read, index and search the textfiles, documents, databases efficiently for

How It Works:

Let us create a simple application that will index our documents. You can the solr.net dlls to your application and start with the implementation. As mentioned in the earlier post you need to modify the schema.xml . Here you will need to mention the fields that needs to be indexed.

Let us create a class for the objects that needs to created from the documents that have product
information.” Solr Unique key “ attribute is used for primary key. In this class you need to mention the solr attributes to the fields that are created in the class:

public class Product
{
  [SolrField("productname")]
  public string ProductName { get; set; }
  [SolrField("datecreated")]
  public DateTime? DateCreated { get;set; }
  [SolrUniqueKey("productid")]
  public string ProductId {get;set;}
  [SolrField("productinformation")]
  public string ProductInformation {get;set;}

}
We will need to provide solr with collection of objects of above to perform indexing. Let us get the collection of object of products to from database. Let us say we have a sql connection class that will provide a the data from the database for product. Suppose SqlConnection.cs has a function GetProductDetails(). This class has a return of list of product.

public class SqlConnection 
{

 public List<Product> GetProductDetails()

  {
    //return list of products from the database

  }


Solr uses IOC to instantiate the classes. So we can use ISOlrOperations to get the data from the
database and provide it to the indexer. For this we will need to create an indexer class.

public class ProductIndexer
{
  private string connectionString;
  private string solrUrl;
  public ProductIndexer (string connectionString, string solrUrl)
  {
    this.connectionString = connectionString;
    this.solrUrl = solrUrl;
  }

  public void IndexFiles()
  {
    Startup.Init<Product>(this.solrUrl);    

    var solrWorker=ServiceLocator.Current.GetInstance<ISolrOperations<Product>();
    var files = new SqlConnection.GetProductDetails ();
    solrWorker.Add(files).Commit();
  }
}


At the end of the operations we need to commit the data for indexing. Once you run this application the data gets indexed. But make sure that solr has been started. After indexing is complete you can the indexes on http://localhost:8983/solr . You can create a simple console application and call the above indexer to index the documents.

Boston Byte Grabs a Spot in Clutch’s List of Top Software Developers in Massachusetts

Boston Byte is a collective of highly-skilled and highly-professional developers dedicated to solving your technological ...