RUN@cloud » Websolr Service

Websolr Service

Last modified by Raffaele Guidi on 2012/04/30 16:19

Websolr provides Apache Solr as a service. You can build search and indexing functionality into your application by using Websolr service.

If you haven't already, create an account at CloudBees, and then subscribe to Websolr Base subscription plan.

Once subscribed, you can open the Websolr service (from the Services page) to create an Index.

websolr-resource.png
 

Let's take a book store application as an example. This application lets you add books and later search based on the book title. We're going to use Apache Solrj library.

Solr Index Schema

Book model object in this example has 4 fields title, author, publisher and isbn, all of type String. First, go to the Websolr Index resource page. Then, click the Show config link and copy the websolr server URL. You will need this URL to connect to the Solr server from your client.

websolr-resource-view.png

Click on View to go to Websolr Index configuration page where you can customize the Index schema as appropriate for your application.

websolr-index.png

Once on the Websolr Index configuration page, click Advanced Configuration and paste the book specific field entries inside <fields>  tag.

<schema name="Sunspot 1.2.x" version="1.0">
 ...
 <fields>
    ...
   <field name="author" stored="true" type="string" multiValued="false" indexed="true"/>
   <field name="title" stored="true" type="string" multiValued="false" indexed="true"/>
   <field name="publisher" stored="true" type="string" multiValued="false" indexed="true"/>
   <field name="isbn" stored="true" type="string" multiValued="false" indexed="true"/>
    ...
 </fields>
  ...
</schema>

There is predefined field id which is a unique key that must be added along with other fields to the Index.

Connect to Websolr Server

    websolrServer = new CommonsHttpSolrServer(websolrUrl);

You can find the correct value for websolrUrl on the Websolr Index resource page.

Populate Index

To populate an index, you have two options: either annotate your POJO or model class with @Field annotation or use SolrInputDocument. See Solrj wiki for details.

In this sample we will go with @Field annotation. Here is Book model annotated with @Field annotation.

@Entity
public class Book extends GenericModel {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Field
    public String id;

    @Required
    @Field
    public String title;
   
    @Required
    @Field
    public String author;
   
    @Required
    @Field
    public String isbn;
   
    @Required
    @Field
    public String publisher;
}

Here is how you can add a book object to Websolr Index:

    websolrServer.addBean(book);
    websolrServer.commit();

Perform Search

The code snippet below performs a search by title and lists the results in ascending order:

public List<Book> search(String query){
        List<Book> books = null;
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery( "title:"+query );
        solrQuery.addSortField("title", SolrQuery.ORDER.asc);
       try {
            QueryResponse resp = websolrServer.query( solrQuery );
            books = resp.getBeans(Book.class);
        } catch (SolrServerException e) {
            Logger.error("Failed to execute query: %s", query, e);
        }
       if(books == null){
            books = Collections.EMPTY_LIST;
        }
       return books;
    }

Check out the complete code sample and follow README to run it on your local machine or deploy it on CloudBees RUN@cloud.

  $ git clone git@github.com:vivek/websolr-play-sample.git
Tags: ecosystem
Created by Vivek Pandey on 2011/11/15 23:41