Package org.hibernate.search

Examples of org.hibernate.search.FullTextSession


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

  public void testCurrent() 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( "id", "lastname", "dept" );



    ScrollableResults results = hibQuery.scroll();
    results.beforeFirst();
    results.next();
    assertTrue("beforeFirst() pointer incorrect", results.isFirst());

    results.afterLast();
    results.previous();
    assertTrue("afterLast() pointer incorrect", results.isLast());

    // Let's see if a bad reverse scroll screws things up
    results.scroll( -8 );
    results.next();
    assertTrue("large negative scroll() pointer incorrect", results.isFirst());

    // And test a bad forward scroll.
    results.scroll( 10 );
    results.previous();
    assertTrue("large positive scroll() pointer incorrect", results.isLast());

    // Finally, let's test a REAL screwup.
    hibQuery.setFirstResult( 3 );
    hibQuery.setMaxResults( 1 );

    results = hibQuery.scroll();
    results.first();
    Object[] result = results.get();
    assertEquals(1004, result[0]);

    results.last();
    result = results.get();
    assertEquals(1004, result[0]);

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


    createTestData();

    QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
    Query query = parser.parse( "Elephant" );

    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    try {
      org.hibernate.Query hibQuery = s.createFullTextQuery( query, String.class );
      hibQuery.list();
      tx.commit();
      fail();
    }
    catch ( IllegalArgumentException iae ) {
      log.debug( "success" );
    }

    tx = s.beginTransaction();
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
    assertItsTheElephant( hibQuery.list() );
    tx.commit();

    s.close();
  }
View Full Code Here

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

  public void testMultipleEntityPerIndex() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Clock clock = new Clock( 1, "Seiko" );
    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 );
    AlternateBook alternateBook = new AlternateBook( 1, "La chute de la petite reine a travers les yeux de Festina" );
    s.save( alternateBook );
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:Festina" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query with explicit class filter", 1, result.size() );

    query = parser.parse( "summary:Festina" );
    hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    Iterator it = hibQuery.iterate();
    assertTrue( it.hasNext() );
    assertNotNull( it.next() );
    assertFalse( it.hasNext() );

    query = parser.parse( "summary:Festina" );
    hibQuery = s.createFullTextQuery( query, Clock.class, Book.class );
    ScrollableResults sr = hibQuery.scroll();
    assertTrue( sr.first() );
    assertNotNull( sr.get() );
    assertFalse( sr.next() );
    sr.close();

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

    query = parser.parse( "summary:Festina" );
    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 );
    }
    for (Object element : s.createQuery( "from java.lang.Object" ).list()) s.delete( element );
    tx.commit();
    s.close();
  }
View Full Code Here

  }

  public void testInheritance() throws Exception {
    createTestData();

    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();

    QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
    Query query = parser.parse( "Elephant" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
    assertItsTheElephant( hibQuery.list() );

    query = parser.parse( "Elephant" );
    hibQuery = s.createFullTextQuery( query);
    assertItsTheElephant( hibQuery.list() );

    query = parser.parse( "hasSweatGlands:false" );
    hibQuery = s.createFullTextQuery( query, Animal.class, Mammal.class );
    assertItsTheElephant( hibQuery.list() );

    query = parser.parse( "Elephant OR White Pointer" );
    hibQuery = s.createFullTextQuery( query, Being.class );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query filtering on superclass return mapped subclasses", 2, result.size() );

    query = new RangeQuery( new Term( "weight", "04000" ), new Term( "weight", "05000" ), true );
    hibQuery = s.createFullTextQuery( query, Animal.class );
    assertItsTheElephant( hibQuery.list() );

    query = parser.parse( "Elephant" );
    hibQuery = s.createFullTextQuery( query, Being.class );
    assertItsTheElephant( hibQuery.list() );

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


  public void testPolymorphicQueries() throws Exception {
    createTestData();

    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "name", new StopAnalyzer() );
    Query query = parser.parse( "Elephant" );

    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Mammal.class );
    assertItsTheElephant( hibQuery.list() );

    hibQuery = s.createFullTextQuery( query, Animal.class );
    assertItsTheElephant( hibQuery.list() );

    hibQuery = s.createFullTextQuery( query, Being.class );
    assertItsTheElephant( hibQuery.list() );

    hibQuery = s.createFullTextQuery( query, Object.class );
    assertItsTheElephant( hibQuery.list() );

    hibQuery = s.createFullTextQuery( query, Serializable.class );
    assertItsTheElephant( hibQuery.list() );

    hibQuery = s.createFullTextQuery(
        query, Mammal.class, Animal.class, Being.class, Object.class, Serializable.class
    );
    assertItsTheElephant( hibQuery.list() );

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

  }

  public void testSubclassInclusion() throws Exception {
    createTestData();

    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();

    Query query = new TermQuery( new Term( "numberOfEggs", "2" ) );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Eagle.class );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of hits. There should be two birds.", 1, result.size() );

    query = new TermQuery( new Term( "numberOfEggs", "2" ) );
    hibQuery = s.createFullTextQuery( query, Bird.class );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of hits. There should be two birds.", 2, result.size() );

    query = new TermQuery( new Term( "numberOfEggs", "2" ) );
    hibQuery = s.createFullTextQuery( query, Mammal.class );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of hits. There should be two birds.", 0, result.size() );

    try {
      query = new TermQuery( new Term( "numberOfEggs", "2" ) );
      hibQuery = s.createFullTextQuery( query, String.class );
      hibQuery.list();
      fail();
    }
    catch ( IllegalArgumentException iae ) {
      log.debug( "success" );
    }

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

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

  public void testCriteria() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    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 );
    Author emmanuel = new Author();
    emmanuel.setName( "Emmanuel" );
    s.save( emmanuel );
    book.getAuthors().add( emmanuel );
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "title", new StopAnalyzer() );

    Query query = parser.parse( "summary:Festina" );
    org.hibernate.Query hibQuery = s.createFullTextQuery( query, Book.class );
    List result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Query with no explicit criteria", 1, result.size() );
    book = (Book) result.get( 0 );
    assertFalse( "Association should not be inintialized", Hibernate.isInitialized( book.getAuthors() ) );

    result = s.createFullTextQuery( query ).setCriteriaQuery(
        s.createCriteria( Book.class ).setFetchMode( "authors", FetchMode.JOIN ) ).list();
    assertNotNull( result );
    assertEquals( "Query with explicit criteria", 1, result.size() );
    book = (Book) result.get( 0 );
    assertTrue( "Association should be inintialized", Hibernate.isInitialized( book.getAuthors() ) );
    assertEquals( 1, book.getAuthors().size() );

    //cleanup
    Author author = book.getAuthors().iterator().next();
    book.getAuthors().remove( author );

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

   *
   * @throws Exception in case the test fails.
   */
  public void testPurgeIndex() throws Exception {
    createTestData();
    FullTextSession s = Search.getFullTextSession( openSession() );

    Transaction tx = s.beginTransaction();
    assertNumberOfAnimals( s, 5 );
    tx.commit();

    tx = s.beginTransaction();
    s.purgeAll( Serializable.class );
    tx.commit();

    tx = s.beginTransaction();
    assertNumberOfAnimals( s, 3 );
    tx.commit();

    tx = s.beginTransaction();
    s.purgeAll( Bird.class );
    tx.commit();

    tx = s.beginTransaction();
    assertNumberOfAnimals( s, 1 );
    tx.commit();

    tx = s.beginTransaction();
    s.purgeAll( Object.class );
    tx.commit();

    tx = s.beginTransaction();
    assertNumberOfAnimals( s, 0 );
    tx.commit();

    s.close();
  }
View Full Code Here

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

  public void testScrollEmptyHits() 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 );

    ScrollableResults projections = hibQuery.scroll();
    projections.beforeFirst();
    projections.next();
    Object[] projection = projections.get();
    assertNull( projection );

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

    projections = hibQuery.scroll();
    projections.beforeFirst();
    projections.next();
    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

   *
   * @throws Exception in case the test fails.
   */
  public void testPurgeUnIndexClass() throws Exception {
    createTestData();
    FullTextSession s = Search.getFullTextSession( openSession() );

    Transaction tx = s.beginTransaction();
    assertNumberOfAnimals( s, 5 );
    tx.commit();

    tx = s.beginTransaction();
    try {
      s.purgeAll( String.class );
      tx.commit();
      fail();
    }
    catch ( IllegalArgumentException iae ) {
      log.debug( "Success" );
    }
    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.