Pages

Monday 22 July 2013

C# Web Service To Query Solr (REST)

Preparation

You should have a Solr Server set-up on port 8080, configured to use Tika's extracting reuqest handler as well as having the highlighting functionality: 
http://amac4.blogspot.co.uk/2013/08/setting-up-highlighting-for-solr-4.html
http://amac4.blogspot.co.uk/2013/07/setting-up-tika-extracting-request.html
http://amac4.blogspot.co.uk/2013/07/setting-up-solr-with-apache-tomcat-be.html
Now that you have your Solr server up and running, now what? How do you query? How to you make use of the server? Well, you need to create a web service that will query Solr when the method is called from an ASP page. If you want your search service to be used on the web then you should most certainly consider creating a web service to provide you with a clean solution to the problem.
I am a novice when it comes to C# and this was my first involvement with the language, but due to the similarities it holds with Java, it wasn't difficult to understand the basics and get the web service up and running. It will return an xml file based on the input parameters and has the capability of highlighting too

You will require:

  • Microsoft Visual Studio with .NET framework of 3.5 or higher

Running The Web Service

  • Open Microsoft Visual Studio
  • Go to File→New Project and ensure that .NET Framework 3.5 is selected from the pull-down menu. Then select ASP.NET Web Service Application using Visual C#. Give your Web Service a relevant name and hit OK
  • Input the source code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Net;
    using System.Xml;
    using System.Web.UI;
     
    namespace Query
    {
        /// <summary>
        /// Queries the Solr Server based on input parameters and returns the XML response
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class Service1 : System.Web.Services.WebService
        {
     
            [WebMethod(Description = "Query Solr server and return XML response")]
            public XmlDocument QuerySolr(string keyword, string notKeyword, string exact, string rows, string filter,
                string fuzzy, string highlight)
            {
     
     
                /*  Default values should the user not enter anything */
                if (rows.Equals(""))
                    rows = "50";
     
                if (keyword.Equals("") && notKeyword.Equals("") && exact.Equals(""))
                    keyword = "*";
     
                if (filter.Equals(""))
                    filter = "id,title";
     
                if (!exact.Equals(""))
                    exact = " \"" + exact + "\"";
     
                if(!highlight.Equals(""))
                    highlight = "&hl=true&hl.fl=content";
     
                /* Should the user require that a certain word or words are not present */
                if (!notKeyword.Equals(""))
                {
     
                    /* Split each word */
                    string[] nwords = notKeyword.Split(null);
                    notKeyword = "";
     
                    /* Loops and adds the hyphen to each word */
                    for (int i = 0; i < nwords.Length; i++)
                    {
                        notKeyword = notKeyword + "-" + nwords[i] + " ";
                    }
                    notKeyword = notKeyword.Substring(0, notKeyword.Length - 1);
     
                }
     
                /* Should the user enable a fuzzy search a tilda must be added on to the end of each word */
                if (fuzzy.Equals("true") && !keyword.Equals(""))
                {
     
                    /* Splits the words by whitespace */
                    string[] words = keyword.Split(null);
                    keyword = "";
     
                    /* Loops and adds the tilda to each word */
                    for (int i = 0; i < words.Length; i++)
                    {
                        keyword = keyword + words[i] + "~ ";
                    }
                    keyword = keyword.Substring(0, keyword.Length - 1);
     
                }    
     
                /* Will throw an error should it have problems connecting to the Solr server */
                try
                {
     
                    /* Connecting to the Solr server and querying it with the search criteria that the user has provided*/
                    WebClient client = new WebClient();
                    string text = client.DownloadString(
                        "http://localhost:8080/solr/select?q=url:("+ keyword + " " + notKeyword + exact + ")^20%20text:("+ keyword
                        + " " + notKeyword + exact + ")&fl=" + filter + "&rows=" + rows + highlight);
     
                    /* The response is returned as a string so it must turn it into an XML document */
                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(text.Replace("%20", " ").Replace("\\", "/"));   //Changes to windows style filepath
     
                    return xml;
     
                }
                catch (Exception)
                {
                    /* Should an exception occur it returns an XML document with details of the problem */
                    XmlDocument xmle = new XmlDocument();
                    xmle.LoadXml("<Problem>Solr server cannot be reached</Problem>");
     
                    return xmle;
                }
            }
     
        }
    }
  • To test the Web Service hit the Green Triangle in the Top Menu
  • A Browser Window should open and display the Web Services that are currently being offered, click on the Query Solr Web Service
  • You should be able to enter the search criteria beneath and hit invoke to test your results

1 comment:

  1. I don't even know how I ended up here, but I thought this post was great.
    I do not know who you are but definitely you are going to a famous blogger if you aren't already ;) Cheers!


    Take a look at my weblog - favicon generator

    ReplyDelete