-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Search and Lucene
PostPosted: Fri Sep 12, 2008 5:01 pm 
Newbie

Joined: Thu Sep 11, 2008 5:22 pm
Posts: 3
Hello,

I am working on a Seam application that uses Hibernate Search to build and search Lucene indexes. I have put Hibernate Search annotations all over my JPA Entities, and the Hibernate Search is building searchable indexes. Perfect.

However, I need to create an index that I do not believe can be created via Hibernate Search. Let me give an example. Consider the following object:

Code:
@Entity
@Indexed
public class Person implements HasFavoriteNumber {

  @Field(index = Index.UN_TOKENIZED ) private String id;
  @Field(index = Index.TOKENIZED ) private String name;
  @Field(index = Index.UN_TOKENIZED ) private int favoriteNumber; // a number from 1 to 10

  //getters and setters follow..
}


Let's pretend that I would like to create a Lucene index that stores the spelling of the person's favorite number: i.e. if the person's favorite number is 7, I want an index called favoriteNumberInEnglish that would contain the value "seven" for this Person. I have created a class (FavoriteNumberInEnglishListener) that implements PostInsertEventListener, PostUpdateEventListener PostDeleteEventListener. Hibernate calls my class after update. However, it appears that Hibernate Search has not indexed the Person -- when I create Query object and search by ID, I get zero hits:

Code:
//omitting insert and delete, since they're very similar to update, but they are there in my actual code

public class FavoriteNumberInEnglishListener implements PostUpdateListener {

  public void onPostUpdate(PostUpdateEvent event) {
    Object o = event.getEntity();
    if( o instanceof HasFavoriteNumber ) {
      HasFavoriteNumber hasFavoriteNumber = ( HasFavoriteNumber ) o;
      try {
        Directory d = FSDirectory.getDirectory(DIRECTORY);
        IndexReader ir = indexReader.open(d);
        QueryParser parser = new QueryParser("id", new StandardAnalyzer());
        Query q = parser.parse(hasFavoriteNumber.getId());
        Hits hits = new IndexSearcher(ir).search(q); // this always has zero results
        if( hits.length() == 1 ) { // never returns true, as hits.length() is always zero
          Document doc = hits.doc(0);
          doc.add( new Field("favoriteNumberInEnglish", hasFavoriteNumber.getFavoriteNumber(), Field.Store.YES, Field.Index.UN_TOKENIZED));
           // and then write the document using an IndexWriter
        }
      }
      catch( Exception e ) {}
    }
  }


I thought that perhaps the problem was that Hibernate was calling my Listener before it was calling FullTextIndexEventListener, so I made my listener subclass FullTextIndexEventListener and put a call to the superclass as the first line in onPostUpdate, but I still found zero hits.

Any ideas what I'm doing wrong? I get the feeling my approach may be completely off -- if there is a better way for me to do this, I can change my approach.

Thank you in advance for any assistance.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 13, 2008 8:39 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I don't understand why you are implementing an eventlistener.
If you only want to code the favorite number from int 7 to String "seven"
you need a StringBridge and let Search do the work for you.

http://www.hibernate.org/hib_docs/search/reference/en/html_single/#d0e1622

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Lucene
PostPosted: Mon Sep 15, 2008 1:36 am 
Newbie

Joined: Thu Sep 11, 2008 5:22 pm
Posts: 3
Ah, that makes sense. That will work well.

One other question: let's assume that I have made the following changes:

Code:

@Entity
@Indexed
public class PersonAttribute {
  @Field private String attributeName;
  @Field private String attributeValue;
}

@Entity
@Indexed
public class Person {
  @Field(index = Index.UN_TOKENIZED ) private String id;
  @Field(index = Index.TOKENIZED ) private String name;
  @Field(index = Index.UN_TOKENIZED )  public int favoriteNumber;
  private Set<PersonAttribute> attributes = new HashSet<PersonAttribute>();
}



We do something similar to this, to allow for flexibility in our model across multiple installations. How could I create indexes on the PersonAttribute objects? For example, if I create a person named Fred, whose attributes Collection contains one PersonAttribute with attributeName of "favoriteColor" and attribute value of "green", I would like to be able to write the query +favoriteColor:green and be returned Fred. Is there any way to do this? I don't think that @IndexEmbeded and @ContainedIn will work here, will it?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.