-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to recreate search index?
PostPosted: Thu May 15, 2008 5:31 am 
Newbie

Joined: Tue Jun 26, 2007 3:08 am
Posts: 10
Hi,
How can I force Hibernate Searche to drop index and recreate it from fresh data? I have situation that I use create in hibernate.hbm2ddl.auto, but the index is not droped (stay old), so I have several copies of old data?

my config for search in Spring considers only:
<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.FSDirectoryProvider</prop>
<prop key="hibernate.search.default.indexBase">${indexBase}</prop>

Can you help me?
Adr


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 9:35 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
the easiest solution is to delete your index directory, a new empty index will be created when you start the application.

Also the Hibernate Search documentation has code examples about batch indexing and mass purging (to remove old entitties from index) to achieve the same effect programmatically.

You should use the Search forum BTW.

regards,
Sanne

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 6:47 am 
Newbie

Joined: Tue Jun 26, 2007 3:08 am
Posts: 10
But I thing purgeAll only delete those entities which are exit in database, not entities which are left by create method. am I right?
Now I deploy my app as a Jetty maven task jetty:run when testing in development mode or deploying .war into Tomcat on production.
Problem is that on some deploying which needs to recreate database (eg when I use hibernate.hbm2ddl.auto=create) I also fill up database with sample data. Create hbm2dll task do not delete index so I have duplicates now in search, and with more and more creating duplicates grows.
I tried to add purgeAll as you assume, but without results (so I think that this only purges results for existing entities in db).
My code:
if(createDatabase.equals(HIBERNATE_CREATE)){
session = sessionFactory.openSession();
FullTextSession fullTextSession = Search.createFullTextSession(session);
Transaction transaction = fullTextSession.beginTransaction();
fullTextSession.purgeAll(AbstractCategory.class);
fullTextSession.purgeAll(Part.class);
fullTextSession.purgeAll(Message.class);
....
}
sessionFactory is Spring Session Factory bean
condition alows to skip creating database sample categories, parts, and messages.

Thx for answer, but I think problem still remains ...

Best regards,
Adr


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 7:13 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Thanks for rating.

Quote:
But I thing purgeAll only delete those entities which are exit in database, not entities which are left by create method. am I right?

No, I'm sure the purgeAll removes all references from the index, it doesn't even look at the database.

Are you sure you are not skpping the purgeAll lines? I mean verify that "if", put some logging.
Also, look inside your index using luke:
http://hibernate.org/446.html
So you verify your entities are removed from the index, maybe some other entities are remaining there you didn't list as a purgeAll argument.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 16, 2008 7:30 am 
Newbie

Joined: Tue Jun 26, 2007 3:08 am
Posts: 10
No, I don't skip them because further are things which makes duplicates.
For now I just clear index just before starting session factory:
so I make in Spring:
Code:
   <bean name="removeIndex" class="com.test.RemoveIndex" init-method="init">
      <property name="indexBase"><value>${indexBase}</value></property>
      <property name="createDatabase"><value>${hibernate.hbm2ddl.auto}</value></property>
   </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" depends-on="removeIndex">
</bean>


And delete directory in RemoveIndex class:
Code:
public class RemoveIndex {
   
   private static final String SVN = ".svn";

   private String indexBase;
   
   private static final String HIBERNATE_CREATE = "create";

   private String createDatabase;

   public void init() {
      if(createDatabase.equals(HIBERNATE_CREATE)){
         File[] files = new File(indexBase).listFiles();
         for(File file : files) {
            if(!file.getName().equals(SVN)) {
               deleteDirectory(file);
            }
         }
      }
   }
   
   public static boolean deleteDirectory(File path) {
       if( path.exists() ) {
         File[] files = path.listFiles();
         for(int i=0; i<files.length; i++) {
            if(files[i].isDirectory()) {
              deleteDirectory(files[i]);
            }
            else {
              files[i].delete();
            }
         }
       }
       return( path.delete() );
     }

   
   public String getIndexBase() {
      return indexBase;
   }
   public void setIndexBase(String indexBase) {
      this.indexBase = indexBase;
   }

   public String getCreateDatabase() {
      return createDatabase;
   }

   public void setCreateDatabase(String createDatabase) {
      this.createDatabase = createDatabase;
   }
}

Maybe someone can provide better solution but this works for me perfectly now, and maybe helps someone in future.

Best regards,
Adr


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.