Sunday, December 6, 2009

Search engine framework "compass"

Now a days search in web application brings you good user experience & save time.
Compass is a powerful, transactional Java Search Engine framework. Java bean mostly used in all enterprise application. Compass allow us to map our java bean to underlying search engine, sync data between compass index and datasource[persistence].

The following is the implementation of compass search in core java. Compass supports for some ORM like hibernate, iBatis.

1) Create java project & add compass jar to your lib.

2) Create a bean called "User.java"

User.java:

@Searchable(alias="user")
public class User {

private Long id;
private String name;
private Integer yearsExperience;
private Float income;

public User() {
}

public User(Long id, String name, Integer yearsExperience, Float income) {
this.id = id;
this.name = name;
this.yearsExperience = yearsExperience;
this.income = income;
}

@SearchableId
public Long getId() {
return id;
}

@SearchableProperty()
public Float getIncome() {
return income;
}

@SearchableProperty()
public String getName() {
return name;
}

@SearchableProperty()
public Integer getYearsExperience() {
return yearsExperience;
}
}


@Searchable() annotation, tells to compass that User object to be indexed.
@SearchableId() annotation, to mention unique id to refer the object.
@SearchableProperty() annotation, helps to what are the property to be index

3) indexUser(Compass) method used to index the user object.

public static void index(Compass compass) {

CompassSession session = compass.openSession();

User user1 = new User(2l, "jp" , 100, 140000.0f);

CompassTransaction tx = session.beginTransaction();

session.save(user1);


tx.commit();
session.close();
}


This method cache the user1 object to memory.

4) searchUser(Compass) method to search user1 object.

public static void searchUser(Compass compass) {
CompassSession session = compass.openSession();
CompassTransaction tx = session.beginLocalTransaction();

CompassQueryBuilder cqb = session.queryBuilder();

CompassHits hits = cqb.bool()
.addMust(cqb.alias("user"))
.addMust(cqb.term("name", "jp"))
.toQuery().hits();

for(CompassHit hit : hits) {
User usr = (User)hit.data();
System.out.println(usr.getName()+"-"+usr.getYearsExperience+"-"+usr.getIncome);
System.out.println("hits="+hit.getScore());
}

tx.commit();
session.close();
}

we need to create compass session, transaction & query builder. Then get the hits from query builder.
.addMust(cqb.alias("user") line get the user object with help of alias. If we have relational bean this alias help us to search particular object like user, address, customer.

5) Finally main() to index the user object and search the same


public static void main( String[] args ) {
CompassConfiguration cfg = new CompassConfiguration();
cfg.configure();

Compass compass = cfg.buildCompass();

index(compass);
searchUser(compass);

}

Output is
jp-100-140000.0
hits= 1
Hits is 1, because our criteria matched for only one object.
Here user1 object index to the memory and searched back from the same. As data grows we can't keep it in memory. In that case we can index objects to file system.


0 comments:

Post a Comment