Pages

Showing posts with label nutch lucene. Show all posts
Showing posts with label nutch lucene. Show all posts

Tuesday, 23 July 2013

RESTful Java Web Service For Solr


You have your Solr server set-up, now what?  You want people to be able to perform queries and have some control over what can be input, well you need a Java Web Service!  This is a short guide on how to create your Java Web Service which may not be tailored to your particular needs but you can tweak it as you please.

Set-Up

To set up the Java Web Service you will need:
  • Netbeans 7 with GlassFish
  • Solr Set-up and Running


Steps Involved

Project Set-Up

  • Start Netbeans and Go to FileNew ProjectJava WebWeb Applications and then hit Next.
  • Give your project a name and then hit Next
  • Ensure that Glassfish is the selected server and hit Finish

HelloResource.java

  • Right click the Default Package and create a new Java Class and call it HelloResource.java
  • Enter the following code:

    import java.net.URL;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.QueryParam;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.URLConnection;
     
    /**
     *
     * @author alamil
     */
     
    //Looks for hello in the pathname
    @Path("hello")
    public class HelloResource {
     
        /**
         *
         * @param arg
         * @param rows
         * @return
         */
        @GET
        @Path("/query")     //Looks for /query in the pathname
        @Produces("text/xml")       //Returns xml
        public String hello(@QueryParam("q") String arg, @QueryParam("rows") String rows, @QueryParam("filter") String filter){
     
            //Variables
            String xmldoc = "";
            String inputLine;
     
            //If the user has not selected a number of rows to display then 50 is set to defualt
            if (rows == null) 
                rows = "50";
     
            //If the user has not selected fields to filter on then it uses the default
            if (filter == null)
                filter = "id,title";
     
            try{
     
                //Trys to connect to the Solr Server with the query
                URL solr = new URL("http://localhost:8080/solr/select?q=url:(" + arg.replaceAll(" ","%20") 
                        + ")^25%20text:(" + arg.replaceAll(" ","%20") + ")&fl=" + filter + "&rows=" + rows);
     
                URLConnection yc = solr.openConnection();
     
                //Reads the returned xml file from the server
                BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
     
                while ((inputLine = in.readLine()) != null){ 
                        xmldoc = xmldoc + inputLine;
                }
     
                //Close the file
                in.close();
     
                //Return the xml document (Replacing the %20's with spaces)
                return xmldoc.replaceAll("%20", " ");
            }catch (Exception e){
                    return "Exception";
            }
        }
    }

RESTConfig.java

  • Right click the Default Package and create a new Java Class and call it RESTConfig.java
  • Enter the following code:

    import javax.ws.rs.core.Application;
    import javax.ws.rs.ApplicationPath;
    /**
     *
     * @author alamil
     */
    @ApplicationPath("SearchInt")
    public class RESTConfig extends Application {
     
    }

Testing

  • Go to RunRun Project and your server should start
  • A browser window will open to the index.html page that is there which you can tweak and edit to your own liking
  • Start your solr server
  • In your browser, Navigate to:
    localhost:8080/HelloRest/SearchInt/hello/query?q=[query term]&filter=[filter term]&rows=[rows]
  • The input parameters set up are:
    1. q=… : representing the keywords in the query
    2. rows=… : representing the number of rows you wish to have returned
    3. filter=… :representing which fields are displayed to the user
Your server and Solr server may be running from the same port which can cause problems