Solr + Jersey REST API

Solr + Jersey REST API

Introduction

This blog deals with Solr – a standalone enterprise search server, with a REST-like API. We store documents in it through a process called “indexing” via XML, JSON, CSV or binary over HTTP. We query it via HTTP GET and receive XML, JSON, CSV or binary results.

Jersey framework is more than JAX-RS Reference Implementation. Jersey provides its own API that extends JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so developers may extend Jersey to suit their needs.

Use Case

Let’s have a use case using Solr with Jersey REST API. The use case is to add and retrieve documents from the Solr search server using jersey REST API. This use case explains the usage of Solrj to connect with Solr and how to use jersey REST API to add and retrieve documents to/from Solr using Solrj.

What we need to do:

  • Create a dynamic web project using Jersey Jar’s.
  • Using SolrJ connect with the Solr Search Server to Add/Retrieve documents.

Solution

Before solving our use case, let’s get some pre-requisites satisfied.

Pre-requisites:

  • The essential project structure and libs to create the project are given below as a screenshot.

solr-image

  • Create a dynamic web project using jersey jar’s

We have used Eclipse to create dynamic web projects using the jersey jar and its dependent jar files. We have created two API’s namely,

  • adddocument- This API is used to add documents to Solr using Solrj.This API is implemented to support POST method. We need a form to submit the input which will be added as a solr fields in the document.
  • Create an html file to support POST method API.

2

Once we submit the form, we can see the following response:
{“response”:”Added Successfully”}

To check if the document is added in server use the solr query given below,
http://localhost:8983/solr/select/?q=xxxx&version=2.2&start=0&rows=10&indent=on

1 2If the document process is done correctly, we can see the log given below in the solr server.

solr a

  • getdocuments -  This API is used to get documents from Solr using Solrj.

Example:  http://localhost:8080/solr/getdocuments?search=xxxx
Input Parameter: a search based on this input parameter solr query will be formed to obtain the search result.

  • Output response:

    {“doc-1″:{“timestamp”:”Sat Apr 12 15:11:46 IST 2014″,”id”:”100″,”price”:10000,”name”:”XXXX”,”sku”:”100″,”spell”:”XXXX”,”popularity”:10},”numFound”:1}

 

 Using SolrJ connect with Solr

    • The source code given below explains clearly about connecting Solr server using SolrJ.
      • Steps:
      1. Create a war file.
      2. Use Geronimo or tomcat server to deploy war file.
      3. Start Solr server using “start.jar”.
      4. Execute API.

      Use “java –jar start.jar” to start solr server.

      solr c

Once server is  started using the url given below, we can access the solr server,
http://localhost:8983/solr/admin/

Challenge:

  • Unable to deploy:

Untitled

Solution:
This is error occurs because the jar “jersery-servlet1.6.jar” has been missed. If we add that jar in the class path, the problem will be solved.

Conclusion

This is a demo application to demonstrate the use of Jersey rest API, method to connect SolrJ to with Solr server to get and add documents from/to Solr server. Usually Solr allows REST API to add and get the documents but this demo explains the other REST API like jersey to connect with SOLR.

Reference

 

 

12509 Views 3 Views Today