Package org.hibernate.search

Examples of org.hibernate.search.FullTextSession


   *
   * @throws Exception in case the test fails.
   */
  @SuppressWarnings( "unchecked" )
  public void testAddWorkGetReplacedByDeleteWork() throws Exception {
    FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession( openSession() );
    SearchFactoryImpl searchFactory = ( SearchFactoryImpl ) fullTextSession.getSearchFactory();
    DocumentBuilderIndexedEntity builder = searchFactory.getDocumentBuilderIndexedEntity( SpecialPerson.class );

    // create test entity
    SpecialPerson person = new SpecialPerson();
    person.setName( "Joe Smith" );

    EmailAddress emailAddress = new EmailAddress();
    emailAddress.setAddress( "foo@foobar.com" );
    emailAddress.setDefaultAddress(true);

    person.addEmailAddress( emailAddress );

    List<LuceneWork> queue = new ArrayList<LuceneWork>();

    builder.addWorkToQueue( SpecialPerson.class, person, 1, WorkType.ADD, queue, searchFactory );

    assertEquals("There should only be one job in the queue", 1, queue.size());
    assertTrue("Wrong job type", queue.get(0) instanceof AddLuceneWork );

    builder.addWorkToQueue( SpecialPerson.class, person, 1, WorkType.DELETE, queue, searchFactory );

    assertEquals("There should only be one job in the queue", 1, queue.size());
    assertTrue("Wrong job type. Add job should have been replaced by delete.", queue.get(0) instanceof DeleteLuceneWork );

    fullTextSession.close();
 
View Full Code Here


* @author John Griffin
*/
public class LuceneQueryTest extends SearchTestCase {

  public void testList() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Clock clock = new Clock( 1, "Seiko" );
    s.save( clock );
    clock = new Clock( 2, "Festina" );
    s.save( clock );
    Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
    s.save( book );
    book = new Book( 2, "La gloire de mon p�re", "Les deboires de mon p�re en v�lo" );
    s.save( book );
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:noword" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( 0, result.size() );

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query with explicit class filter", 2, result.size() );

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query, Clock.class );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query with one class filter", 1, result.size() );

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query with no class filter", 2, result.size() );
    for (Object element : result) {
      assertTrue( Hibernate.isInitialized( element ) );
      s.delete( element );
    }
    s.flush();
    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query with delete objects", 0, result.size() );

    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

        s.persist(cloud);
        s.flush();
        tx.commit();

        tx = s.beginTransaction();
        FullTextSession session = Search.getFullTextSession(s);
        QueryParser parser = new QueryParser("id", new StandardAnalyzer() );
        Query query;
        List result;

        query = parser.parse("double2:[2.1 TO 2.1] AND float2:[2.1 TO 2.1] " +
        "AND integerv2:[2 TO 2.1] AND long2:[2 TO 2.1] AND type:\"dog\" AND storm:false");

    result = session.createFullTextQuery(query).list();
        assertEquals( "find primitives and do not fail on null", 1, result.size() );

        query = parser.parse("double1:[2.1 TO 2.1] OR float1:[2.1 TO 2.1] OR integerv1:[2 TO 2.1] OR long1:[2 TO 2.1]");
        result = session.createFullTextQuery(query).list();
        assertEquals( "null elements should not be stored", 0, result.size() ); //the query is dumb because restrictive

    query = parser.parse("type:dog");
        result = session.createFullTextQuery(query).setProjection( "type" ).list();
        assertEquals( "Enum projection works", 1, result.size() ); //the query is dumb because restrictive

    query = new TermQuery( new Term("clazz", Cloud.class.getName() ) );
        result = session.createFullTextQuery(query).setProjection( "clazz" ).list();
        assertEquals( "Clazz projection works", 1, result.size() );
    assertEquals( "Clazz projection works", Cloud.class.getName(), ( (Class) ((Object[])result.get(0))[0] ).getName() );

    BooleanQuery bQuery = new BooleanQuery();
    bQuery.add( new TermQuery( new Term("uri", "http://www.hibernate.org" ) ), BooleanClause.Occur.MUST );
    bQuery.add( new TermQuery( new Term("url", "http://www.hibernate.org" ) ), BooleanClause.Occur.MUST );

    result = session.createFullTextQuery(bQuery).setProjection( "clazz" ).list();
        assertEquals( "Clazz projection works", 1, result.size() );

    s.delete( s.get( Cloud.class, cloud.getId() ) );
        tx.commit();
        s.close();
View Full Code Here

        s.persist(cloud);
        s.flush();
        tx.commit();

        tx = s.beginTransaction();
        FullTextSession session = Search.getFullTextSession(s);
        QueryParser parser = new QueryParser("id", new SimpleAnalyzer() );
        Query query;
        List result;

        query = parser.parse("customFieldBridge:This AND customStringBridge:This");
        result = session.createFullTextQuery(query).list();
        assertEquals( "Properties not mapped", 1, result.size() );

        query = parser.parse("customFieldBridge:by AND customStringBridge:is");
        result = session.createFullTextQuery(query).list();
        assertEquals( "Custom types not taken into account", 0, result.size() );

        s.delete( s.get( Cloud.class, cloud.getId() ) );
        tx.commit();
        s.close();
View Full Code Here

        s.persist(cloud);
        s.flush();
        tx.commit();

        tx = s.beginTransaction();
        FullTextSession session = Search.getFullTextSession(s);
        QueryParser parser = new QueryParser("id", new StandardAnalyzer() );
        Query query;
        List result;

        query = parser.parse("myDate:[19900101 TO 20060101]"
                + " AND dateDay:[20001214 TO 2000121501]"
                + " AND dateMonth:[200012 TO 20001201]"
                + " AND dateYear:[2000 TO 200001]"
                + " AND dateHour:[20001214 TO 2000121503]"
                + " AND dateMinute:[20001214 TO 200012150343]"
                + " AND dateSecond:[20001214 TO 20001215034302]"
                + " AND dateMillisecond:[20001214 TO 20001215034302005]"
        );
        result = session.createFullTextQuery(query).list();
        assertEquals( "Date not found or not property truncated", 1, result.size() );

        s.delete( s.get( Cloud.class, cloud.getId() ) );
        tx.commit();
        s.close();
View Full Code Here

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

  public void testResultSize() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Clock clock = new Clock( 1, "Seiko" );
    s.save( clock );
    clock = new Clock( 2, "Festina" );
    s.save( clock );
    Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
    s.save( book );
    book = new Book( 2, "La gloire de mon p�re", "Les deboires de mon p�re en v�lo" );
    s.save( book );
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:Festina Or brand:Seiko" );
    Statistics stats = s.getSessionFactory().getStatistics();
    stats.clear();
    boolean enabled = stats.isStatisticsEnabled();
    if ( !enabled ) stats.setStatisticsEnabled( true );
    FullTextQuery hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    assertEquals( "Exection of getResultSize without actual results", 2, hibQuery.getResultSize() );
    assertEquals( "No entity should be loaded", 0, stats.getEntityLoadCount() );

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "2 entities should be loaded", 2, stats.getEntityLoadCount() );
    if ( !enabled ) stats.setStatisticsEnabled( false );
    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

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

  public void testFirstMax() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Clock clock = new Clock( 1, "Seiko" );
    s.save( clock );
    clock = new Clock( 2, "Festina" );
    s.save( clock );
    Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
    s.save( book );
    book = new Book( 2, "La gloire de mon p�re", "Les deboires de mon p�re en v�lo" );
    s.save( book );
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:Festina Or brand:Seiko" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    hibQuery.setFirstResult( 1 );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "first result no max result", 1, result.size() );

    hibQuery.setFirstResult( 0 );
    hibQuery.setMaxResults( 1 );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "max result set", 1, result.size() );

    hibQuery.setFirstResult( 0 );
    hibQuery.setMaxResults( 3 );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "max result out of limit", 2, result.size() );

    hibQuery.setFirstResult( 2 );
    hibQuery.setMaxResults( 3 );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "first result out of limit", 0, result.size() );

    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

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

  public void testIterator() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Clock clock = new Clock( 1, "Seiko" );
    s.save( clock );
    clock = new Clock( 2, "Festina" );
    s.save( clock );
    Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
    s.save( book );
    book = new Book( 2, "La gloire de mon p�re", "Les deboires de mon p�re en v�lo" );
    s.save( book );
    tx.commit();//post commit events for lucene
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:noword" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    Iterator result = hibQuery.iterate();
    assertNotNull( result );
    assertFalse( result.hasNext() );

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    result = hibQuery.iterate();
    assertNotNull( result );
    int index = 0;
    while ( result.hasNext() ) {
      index++;
      s.delete( result.next() );
    }
    assertEquals( 2, index );

    s.flush();

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    result = hibQuery.iterate();
    assertNotNull( result );
    assertFalse( result.hasNext() );

    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

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

  public void testScrollableResultSet() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Clock clock = new Clock( 1, "Seiko" );
    s.save( clock );
    clock = new Clock( 2, "Festina" );
    s.save( clock );
    Book book = new Book( 1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah" );
    s.save( book );
    book = new Book( 2, "La gloire de mon p�re", "Les deboires de mon p�re en v�lo" );
    s.save( book );
    tx.commit();//post commit events for lucene
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:noword" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    ScrollableResults result = hibQuery.scroll();
    assertNotNull( result );
    assertEquals( -1, result.getRowNumber() );
    assertEquals( false, result.next() );
    result.close();

    query = parser.parse( "summary:Festina Or brand:Seiko" );
    hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    result = hibQuery.scroll();
    assertEquals( -1, result.getRowNumber() );
    result.beforeFirst();
    assertEquals( true, result.next() );
    assertTrue( result.isFirst() );
    assertTrue( result.scroll( 1 ) );
    assertTrue( result.isLast() );
    assertFalse( result.scroll( 1 ) );
    result.beforeFirst();
    while ( result.next() ) {
      s.delete( result.get()[0] );
    }
    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

*/
public class LazyCollectionsUpdatingTest extends SearchTestCase {
 
  public void testUpdatingInTransaction() {
    assertFindsByRoadName( "buonarroti" );
    FullTextSession fullTextSession = Search.getFullTextSession( sessions.openSession() );
    try {
      Transaction tx = fullTextSession.beginTransaction();
      BusStop busStop = (BusStop) fullTextSession.get( BusStop.class, 1L );
      busStop.setRoadName( "new road" );
      tx.commit();
    }
    catch (org.hibernate.AssertionFailure ass) {
      fail( ass.getMessage() );
    }
    finally {
      fullTextSession.close();
    }
    assertFindsByRoadName( "new" );
  }
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.