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 ""

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) { = id; = name;
this.yearsExperience = yearsExperience;
this.income = income;

public Long getId() {
return id;

public Float getIncome() {
return income;

public String getName() {
return name;

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();;


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.term("name", "jp"))

for(CompassHit hit : hits) {
User usr = (User);


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();

Compass compass = cfg.buildCompass();



Output is
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.

Thursday, December 3, 2009

javascript submitting form twice

I have form with hidden fields. When i do change the country select box, taking the value of select box  & updating  the same in hidden field. When i hit the search button to search countries, i am submitting the form using javascript.

My code:

<a href="#" onclick="submitMyForm();">Search</a>

I was wondering this javascript submitted this form twice. First time with the value in the hidden box and second time with out.

We found out the problem with my javascript code. 

Correct code:

<a href="#" onclick="submitMyForm();return false;">Search</a>

So "return false;" did trick for me.

Have fun