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]