Saturday, March 20, 2010

Flexible Searching Using Compass

            Lucene is a very powerful search engine. Compass is written on top of lucene and provides lucene type search capabilities on top of domain model (jpa entity).In this blog I aim to look into use cases where compass search capabilities can be utilized.
           Compass provides ability to search goggle style on JPA entities. But the question is what are the use cases where somebody would like to have goggle style search on the JPA entities. For example a health insurance claim form contain information like claimant name (first name, last name), address etc. So how to provide search with search information like gaurav malhotra myaddress. The search information can possible comes from the following end points
 UI/Webservice
One can provide UI search screen based on the fixed parameter like
First Name = xxxx
Last Name = yyyyy
-- -- -- -- -- --
and its every easy to construct the SQL/JPQL/JPA2.0 search criteria. But if there is a limitation on the search information and now suppose we want to include the street name also in the search information, this will trigger the change in the UI search screen. Similar challenge exists in case of web services also.

One of nice things about compass is that it fits seamless with JPA (ORM products like EclipseLink). The compass specific search metadata can be directly specified on the JPA entity as shown in the JPA entity Claim.So a typical programming model using compass looks like 




The JPA entity Claim looks like
@Searchable
public class Claim {
    @SearchableId
    private int id;

    @SearchableProperty
    @SearchableMetaDatas({@SearchableMetaData(name = "firstname")})
    private String firstName;

     
    @SearchableProperty
    @SearchableMetaDatas({@SearchableMetaData(name = "lastname")})
    private String lastName;
     
    @SearchableProperty
    @SearchableMetaDatas({@SearchableMetaData(name = "streetname")})
    private String streetName;
     
      -- -- --- ---
  
}

Hence the search code looks like
> Person person = null;
   person = searchService.search(“firstname:gaurav lastname:malhotra streetname=xxx”);
   ** Search Person whose first name is gaurav , last name is malhotra and streetname is xxx
> Person person = null;
   person = searchService.search(“gaurav”);
   ** Search entity which has 'gaurav'

Groovy lovers can also use the search by injecting searchService into the groovy shell and calling search as
Person person = null;
Person = searchService.search(“gaurav malhotra”);  // the search framework automatically finds the Person entity.
Hence some of the import scenarios (EAI) where xml to domain model mapping is provided using groovy logic; adding compass search capabilities to it can be very powerful.

NOTE :- Compass search capability is very vast and support complex querying like inheritance support etc. Refer to Compass Search for more details

Conclusion
          Compass searching provides a very flexible and powerful way of searching capabilities, agnostic of underlying JPA entities, which is sometime referred to google style searching. In my subsequent article I will cover the scabaility of the compass by putting its indexes in the oracle coherence

No comments: