/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat, Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.search.test.session;
import java.io.File;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.queryParser.QueryParser;
import org.hibernate.Transaction;
import org.hibernate.search.Environment;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.impl.FullTextSessionImpl;
import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.test.TestConstants;
/**
* @author Emmanuel Bernard
*/
public class OptimizeTest extends SearchTestCase {
public void testOptimize() throws Exception {
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
int loop = 2000;
for ( int i = 0; i < loop; i++ ) {
Email email = new Email();
email.setId( ( long ) i + 1 );
email.setTitle( "JBoss World Berlin" );
email.setBody( "Meet the guys who wrote the software" );
s.persist( email );
}
tx.commit();
s.close();
s = Search.getFullTextSession( openSession() );
tx = s.beginTransaction();
s.getSearchFactory().optimize( Email.class );
tx.commit();
s.close();
//check non indexed object get indexed by s.index
s = new FullTextSessionImpl( openSession() );
tx = s.beginTransaction();
QueryParser parser = new QueryParser( TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer );
int result = s.createFullTextQuery( parser.parse( "body:wrote" ) ).getResultSize();
assertEquals( 2000, result );
s.createQuery( "delete " + Email.class.getName() ).executeUpdate();
tx.commit();
s.close();
}
protected void configure(org.hibernate.cfg.Configuration cfg) {
super.configure( cfg );
File sub = getBaseIndexDir();
cfg.setProperty( "hibernate.search.default.indexBase", sub.getAbsolutePath() );
cfg.setProperty( "hibernate.search.default.directory_provider", "filesystem" );
cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
}
@SuppressWarnings("unchecked")
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Email.class,
Domain.class
};
}
}