Package org.hibernate.search

Examples of org.hibernate.search.FullTextSession


    assertNotNull( result );
    assertEquals( "Wrong number of hits. There should be one elephant and one shark.", count, result.size() );
  }

  private void createTestData() {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();

    Fish shark = new Fish();
    shark.setName( "White Pointer" );
    shark.setNumberOfDorsalFins( 2 );
    shark.setWeight( 1500 );
    s.save( shark );

    Mammal elephant = new Mammal();
    elephant.setName( "Elephant" );
    elephant.setHasSweatGlands( false );
    elephant.setWeight( 4500 );
    s.save( elephant );

    Mammal chimp = new Mammal();
    chimp.setName( "Chimpanzee" );
    chimp.setHasSweatGlands( true );
    chimp.setWeight( 50 );
    s.save( chimp );

    Bird dove = new Bird();
    dove.setName( "Dove" );
    dove.setNumberOfEggs( 2 );
    s.save( dove );

    Eagle eagle = new Eagle();
    eagle.setName( "Bald Eagle" );
    eagle.setNumberOfEggs( 2 );
    eagle.setWingYype( Eagle.WingType.BROAD );
    s.save( eagle );

    tx.commit();
    s.clear();
  }
View Full Code Here


    tx.commit();
    s.close();
  }

  public void testListEmptyHits() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    prepEmployeeIndex( s );

    s.clear();
    Transaction tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );

    Query query = parser.parse( "dept:XXX" );
    org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
    List result = hibQuery.list();
    assertEquals( 0, result.size() );

    hibQuery = s.createFullTextQuery( query, Employee.class ).setFirstResult( 10 ).setMaxResults( 20 );
    result = hibQuery.list();
    assertEquals( 0, result.size() );

    //cleanup
    for (Object element : s.createQuery( "from " + Employee.class.getName() ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

    tx.commit();
    s.close();
  }

  public void testIterateEmptyHits() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    prepEmployeeIndex( s );

    s.clear();
    Transaction tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );

    Query query = parser.parse( "dept:XXX" );
    org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
    Iterator iter = hibQuery.iterate();
    assertFalse( iter.hasNext() );

    hibQuery = s.createFullTextQuery( query, Employee.class ).setFirstResult( 10 ).setMaxResults( 20 );
    iter = hibQuery.iterate();
    assertFalse( iter.hasNext() );

    //cleanup
    for (Object element : s.createQuery( "from " + Employee.class.getName() ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

* @author Emmanuel Bernard
*/
public class MassIndexTest extends SearchTestCase {

  public void testBatchSize() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    int loop = 14;
    for (int i = 0; i < loop; i++) {
      Statement statmt = s.connection().createStatement();
      statmt.executeUpdate( "insert into Domain(id, name) values( + "
          + ( i + 1 ) + ", 'sponge" + i + "')" );
      statmt.executeUpdate( "insert into Email(id, title, body, header, domain_id) values( + "
          + ( i + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope', " + ( i + 1 ) +")" );
      statmt.close();
    }
    tx.commit();
    s.close();

    //check non created object does get found!!1
    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    ScrollableResults results = s.createCriteria( Email.class ).scroll( ScrollMode.FORWARD_ONLY );
    int index = 0;
    while ( results.next() ) {
      index++;
      s.index( results.get( 0 ) );
      if ( index % 5 == 0 ) s.clear();
    }
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
    List result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
    assertEquals( 14, result.size() );
    for (Object object : result) {
      s.delete( object );
    }
    tx.commit();
    s.close();
  }
View Full Code Here

    s.close();
  }


  public void testTransactional() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    int loop = 4;
    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();

    //check non created object does get found!!1
    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
    List result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
    assertEquals( 0, result.size() );
    tx.commit();
    s.close();

    s = new FullTextSessionImpl( openSession() );
    s.getTransaction().begin();
    Statement stmt = s.connection().createStatement();
    stmt.executeUpdate( "update Email set body='Meet the guys who write the software'" );
    stmt.close();
    //insert an object never indexed
    stmt = s.connection().createStatement();
    stmt.executeUpdate( "insert into Email(id, title, body, header) values( + "
        + ( loop + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope')" );
    stmt.close();
    s.getTransaction().commit();
    s.close();

    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    parser = new QueryParser( "id", new StopAnalyzer() );
    result = s.createFullTextQuery( parser.parse( "body:write" ) ).list();
    assertEquals( 0, result.size() );
    result = s.createCriteria( Email.class ).list();
    for (int i = 0; i < loop / 2; i++)
      s.index( result.get( i ) );
    tx.commit(); //do the process
    s.index( result.get( loop / 2 ) ); //do the process out of tx
    tx = s.beginTransaction();
    for (int i = loop / 2 + 1; i < loop; i++)
      s.index( result.get( i ) );
    tx.commit(); //do the process
    s.close();

    s = Search.getFullTextSession( openSession() );
    tx = s.beginTransaction();
    //object never indexed
    Email email = (Email) s.get( Email.class, Long.valueOf( loop + 1 ) );
    s.index( email );
    tx.commit();
    s.close();

    //check non indexed object get indexed by s.index
    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
    assertEquals( 1, result.size() );
    tx.commit();
    s.close();
  }
View Full Code Here

    s.persist( ent );
    tx.commit();
    s.close();

    s = openSession();
    FullTextSession session = Search.getFullTextSession( s );
    Query luceneQuery = new TermQuery( new Term( "categorie.nom", "livre" ) );
    List result = session.createFullTextQuery( luceneQuery, Entite.class ).list();
    assertEquals( 1, result.size() );
    s.close();

    s = openSession();
    ent = (Entite) s.get( Entite.class, ent.getId() );
    session = Search.getFullTextSession( s );
    session.index( ent );
    s.close();

    s = openSession();
    session = Search.getFullTextSession( s );
    luceneQuery = new TermQuery( new Term( "categorie.nom", "livre" ) );
    result = session.createFullTextQuery( luceneQuery, Entite.class ).list();
    assertEquals( "test lazy loading and indexing", 1, result.size() );
    s.close();

    s = openSession();
    Iterator it = s.createQuery( "from Entite where id = :id").setParameter( "id", ent.getId() ).iterate();
    session = Search.getFullTextSession( s );
    while ( it.hasNext() ) {
      ent = (Entite) it.next();
      session.index( ent );
    }
    s.close();

    s = openSession();
    session = Search.getFullTextSession( s );
    luceneQuery = new TermQuery( new Term( "categorie.nom", "livre" ) );
    result = session.createFullTextQuery( luceneQuery, Entite.class ).list();
    assertEquals( "test lazy loading and indexing", 1, result.size() );
    ent = (Entite) result.get( 0 );
    cat = ent.getCategorie();
    ent.setCategorie( null );
    session.delete( cat );
    session.delete( ent );
    s.close();
  }
View Full Code Here

/**
* @author Emmanuel Bernard
*/
public class MassIndexUsingManualFlushTest extends SearchTestCase {
  public void testManualIndexFlush() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    int loop = 14;
    for (int i = 0; i < loop; i++) {
      Statement statmt = s.connection().createStatement();
      statmt.executeUpdate( "insert into Domain(id, name) values( + "
          + ( i + 1 ) + ", 'sponge" + i + "')" );
      statmt.executeUpdate( "insert into Email(id, title, body, header, domain_id) values( + "
          + ( i + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope', " + ( i + 1 ) +")" );
      statmt.close();
    }
    tx.commit();
    s.close();

    //check non created object does get found!!1
    s = new FullTextSessionImpl( openSession() );
    tx = s.beginTransaction();
    ScrollableResults results = s.createCriteria( Email.class ).scroll( ScrollMode.FORWARD_ONLY );
    int index = 0;
    while ( results.next() ) {
      index++;
      final Email o = (Email) results.get( 0 );
      s.index( o );
      if ( index % 5 == 0 ) {
        s.flushToIndexes();
        s.clear();
      }
    }
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
    List result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
    assertEquals( 14, result.size() );
    for (Object object : result) {
      s.delete( object );
    }
    tx.commit();
    s.close();
  }
View Full Code Here

   * HSEARCH-296
   *
   * @throws Exception in case the test fails.
   */
  public void testClassProjection() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    prepEmployeeIndex( s );

    s.clear();
    Transaction tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );
    Query query = parser.parse( "dept:ITech" );
    org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
    hibQuery.setProjection( FullTextQuery.OBJECT_CLASS );

    List result = hibQuery.list();
    assertNotNull( result );

    Object[] projection = ( Object[] ) result.get( 0 );
    assertNotNull( projection );
    assertEquals( "Wrong projected class", Employee.class, projection[0] );

    tx.commit();
    s.close();
  }
View Full Code Here

    tx.commit();
    s.close();
  }

  public void testLuceneObjectsProjectionWithScroll() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    prepEmployeeIndex( s );

    Transaction tx;
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );

    Query query = parser.parse( "dept:ITech" );
    org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
    // Is the 'FullTextQuery.ID' value correct here? Do we want the Lucene internal document number?
    hibQuery.setProjection(
        "id",
        "lastname",
        "dept",
        FullTextQuery.THIS,
        FullTextQuery.SCORE,
        FullTextQuery.DOCUMENT,
        FullTextQuery.ID
    );

    ScrollableResults projections = hibQuery.scroll();

    // There are a lot of methods to check in ScrollableResultsImpl
    // so, we'll use methods to check each projection as needed.

    projections.beforeFirst();
    projections.next();
    Object[] projection = projections.get();
    checkProjectionFirst( projection, s );
    assertTrue( projections.isFirst() );

    projections.last();
    projection = projections.get();
    checkProjectionLast( projection, s );
    assertTrue( projections.isLast() );

    projections.next();
    projection = projections.get();
    assertNull( projection );

    projections.previous();
    projection = projections.get();
    checkProjectionLast( projection, s );

    projections.first();
    projection = projections.get();
    checkProjectionFirst( projection, s );

    projections.scroll( 2 );
    projection = projections.get();
    checkProjection2( projection, s );

    projections.scroll( -5 );
    projection = projections.get();
    assertNull( projection );

    //cleanup
    for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
      s.delete( element );
    }
    tx.commit();
    s.close();
  }
View Full Code Here

    tx.commit();
    s.close();
  }

  public void testResultTransformToDelimString() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    prepEmployeeIndex( s );

    Transaction tx;
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );

    Query query = parser.parse( "dept:ITech" );
    org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
    hibQuery.setProjection( "id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.ID );
    hibQuery.setResultTransformer( new ProjectionToDelimStringResultTransformer() );

    @SuppressWarnings("unchecked")
    List<String> result = hibQuery.list();
    assertTrue( "incorrect transformation", result.get( 0 ).startsWith( "1000, Griffin, ITech" ) );
    assertTrue( "incorrect transformation", result.get( 1 ).startsWith( "1002, Jimenez, ITech" ) );

    //cleanup
    for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
      s.delete( element );
    }
    tx.commit();
    s.close();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.search.FullTextSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.