Package org.hibernate.search

Examples of org.hibernate.search.FullTextSession


    indexTestData();

    // Search
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    FullTextSession fullTextSession = Search.getFullTextSession( session );

    MultiFieldQueryParser parser = new MultiFieldQueryParser(
        new String[] { "kurztext" }, new StandardAnalyzer()
    );
    Query query = parser.parse( "combi OR sport" );

    Criteria criteria = session.createCriteria( AbstractCar.class );
    criteria.add( Restrictions.eq( "hasColor", Boolean.FALSE ) );

    org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, AbstractCar.class )
        .setCriteriaQuery( criteria );
    List result = hibQuery.list();
    assertEquals( 2, result.size() );
    tx.commit();
    session.close();
View Full Code Here


    indexTestData();

    // Search
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    FullTextSession fullTextSession = Search.getFullTextSession( session );

    MultiFieldQueryParser parser = new MultiFieldQueryParser(
        new String[] { "kurztext" }, new StandardAnalyzer()
    );
    Query query = parser.parse( "combi OR sport" );

    Criteria criteria = session.createCriteria( AbstractCar.class );
    criteria.add( Restrictions.eq( "hasColor", Boolean.FALSE ) );

    org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query )
        .setCriteriaQuery( criteria );
    List result = hibQuery.list();
    assertEquals( 2, result.size() );
    tx.commit();
    session.close();
View Full Code Here

    indexTestData();

    // Search
    Session session = openSession();
    Transaction tx = session.beginTransaction();
    FullTextSession fullTextSession = Search.getFullTextSession( session );

    MultiFieldQueryParser parser = new MultiFieldQueryParser(
        new String[] { "kurztext" }, new StandardAnalyzer()
    );
    Query query = parser.parse( "combi OR sport" );

    Criteria criteria = session.createCriteria( AbstractCar.class );
    criteria.add( Restrictions.eq( "hasColor", Boolean.FALSE ) );

    try {
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery( query, AbstractCar.class, Bike.class )
          .setCriteriaQuery( criteria );
      hibQuery.list();
      fail();
    }
    catch ( SearchException se ) {
View Full Code Here

* @author John Griffin
*/
public class TermVectorTest extends SearchTestCase {
  
   public void testPositionOffsets() throws Exception {
      FullTextSession s = Search.getFullTextSession(openSession());
      createIndex(s);

      s.clear();
      Transaction tx = s.beginTransaction();

      // Here's how to get a reader from a FullTextSession
      SearchFactory searchFactory = s.getSearchFactory();
      DirectoryProvider provider = searchFactory.getDirectoryProviders(ElectricalProperties.class)[0];
      ReaderProvider readerProvider = searchFactory.getReaderProvider();
      IndexReader reader = readerProvider.openReader(provider);

      /**
       * Since there are so many combinations of results here, we are only going
       * to assert a few. - J.G.
       */
      int x = 0;
      TermPositionVector vector = (TermPositionVector) reader.getTermFreqVector(x, "content");
      assertNotNull(vector);
      String[] terms = vector.getTerms();
      int[] freqs = vector.getTermFrequencies();

      assertEquals("electrical", terms[x]);
      assertEquals(2, freqs[x]);

      TermVectorOffsetInfo[] offsets = vector.getOffsets(x);
      assertEquals(0, offsets[x].getStartOffset());
      assertEquals(10, offsets[x].getEndOffset());

      int[] termPositions = vector.getTermPositions(0);
      assertEquals(0, termPositions[0]);
      assertEquals(3, termPositions[1]);

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

    sendMessage( queue );

    // need to sleep to give JMS processing and indexing time
    Thread.sleep( 1000 );

    FullTextSession ftSess = Search.getFullTextSession( openSession() );
    ftSess.getTransaction().begin();
    QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
    Query luceneQuery = parser.parse( "logo:jboss" );
    org.hibernate.Query query = ftSess.createFullTextQuery( luceneQuery );
    List result = query.list();
    assertEquals( 1, result.size() );
    ftSess.delete( result.get( 0 ) );
    ftSess.getTransaction().commit();
    ftSess.close();
  }
View Full Code Here

      s.close();
   }


   public void testNoTermVector() throws Exception {
      FullTextSession s = Search.getFullTextSession(openSession());
      Transaction tx = s.beginTransaction();

      Employee e1 = new Employee(1000, "Griffin", "ITech");
      s.save(e1);
      tx.commit();
      s.clear();

      tx = s.beginTransaction();

      // Here's how to get a reader from a FullTextSession
      SearchFactory searchFactory = s.getSearchFactory();
      DirectoryProvider provider = searchFactory.getDirectoryProviders(Employee.class)[0];
      ReaderProvider readerProvider = searchFactory.getReaderProvider();
      IndexReader reader = readerProvider.openReader(provider);

      TermPositionVector vector = (TermPositionVector) reader.getTermFreqVector(0, "dept");
      assertNull("should not find a term position vector", vector);

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

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

  public void testBoostedGetDesc() throws Exception {
    FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
    buildBoostedGetIndex( fullTextSession );

    fullTextSession.clear();
    Transaction tx = fullTextSession.beginTransaction();

    QueryParser authorParser = new QueryParser( "author", new StandardAnalyzer() );
    QueryParser descParser = new QueryParser( "description", new StandardAnalyzer() );
    Query author = authorParser.parse( "Wells" );
    Query desc = descParser.parse( "martians" );

    BooleanQuery query = new BooleanQuery();
    query.add( author, BooleanClause.Occur.SHOULD );
    query.add( desc, BooleanClause.Occur.SHOULD );
    //System.out.println( query.toString() );

    org.hibernate.search.FullTextQuery hibQuery =
        fullTextSession.createFullTextQuery( query, BoostedGetDescriptionLibrary.class );
    List results = hibQuery.list();

    //System.out.println( hibQuery.explain( 0 ) );
    //System.out.println( hibQuery.explain( 1 ) );

    assertTrue(
        "incorrect document returned",
        ( ( BoostedGetDescriptionLibrary ) results.get( 0 ) ).getDescription().startsWith( "Martians" )
    );

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

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

  public void testBoostedFieldDesc() throws Exception {
    FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
    buildBoostedFieldIndex( fullTextSession );

    fullTextSession.clear();
    Transaction tx = fullTextSession.beginTransaction();

    QueryParser authorParser = new QueryParser( "author", new StandardAnalyzer() );
    QueryParser descParser = new QueryParser( "description", new StandardAnalyzer() );
    Query author = authorParser.parse( "Wells" );
    Query desc = descParser.parse( "martians" );

    BooleanQuery query = new BooleanQuery();
    query.add( author, BooleanClause.Occur.SHOULD );
    query.add( desc, BooleanClause.Occur.SHOULD );
    //System.out.println( query.toString() );

    org.hibernate.search.FullTextQuery hibQuery =
        fullTextSession.createFullTextQuery( query, BoostedFieldDescriptionLibrary.class );
    List results = hibQuery.list();

    assertTrue(
        "incorrect document boost",
        ( ( BoostedFieldDescriptionLibrary ) results.get( 0 ) ).getDescription().startsWith( "Martians" )
    );

    //System.out.println( hibQuery.explain( 0 ) );
    //System.out.println( hibQuery.explain( 1 ) );

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

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

  public void testBoostedDesc() throws Exception {
    FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
    buildBoostedDescIndex( fullTextSession );

    fullTextSession.clear();
    Transaction tx = fullTextSession.beginTransaction();

    QueryParser authorParser = new QueryParser( "author", new StandardAnalyzer() );
    QueryParser descParser = new QueryParser( "description", new StandardAnalyzer() );
    Query author = authorParser.parse( "Wells" );
    Query desc = descParser.parse( "martians" );

    BooleanQuery query = new BooleanQuery();
    query.add( author, BooleanClause.Occur.SHOULD );
    query.add( desc, BooleanClause.Occur.SHOULD );
    //System.out.println( query.toString() );

    org.hibernate.search.FullTextQuery hibQuery =
        fullTextSession.createFullTextQuery( query, BoostedDescriptionLibrary.class );
    List results = hibQuery.list();

    //System.out.println( hibQuery.explain( 0 ) );
    //System.out.println( hibQuery.explain( 1 ) );

    assertTrue(
        "incorrect document returned",
        ( ( BoostedDescriptionLibrary ) results.get( 0 ) ).getDescription().startsWith( "Martians" )
    );

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

   *
   * @throws Exception in case the test fails.
   */
  public void testNoWorkDuplication() throws Exception {

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

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

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

    person.addEmailAddress( emailAddress );

    // persist the customer
    s.persist( person );
    tx.commit();

    // search if the record made it into the index
    tx = s.beginTransaction();
    String searchQuery = "Joe";
    QueryParser parser = new QueryParser( "Content", new StandardAnalyzer() );
    Query luceneQuery = parser.parse( searchQuery );
    FullTextQuery query = s.createFullTextQuery( luceneQuery );
    List results = query.list();
    assertTrue( "We should have a hit", results.size() == 1 );
    tx.commit();

    // Now try to delete
    tx = s.beginTransaction();
    int id = person.getId();
    person = ( SpecialPerson ) s.get( SpecialPerson.class, id );
    s.delete( person );
    tx.commit();

    // Search and the record via Lucene directly
    tx = s.beginTransaction();

    DirectoryProvider directoryProvider = s.getSearchFactory().getDirectoryProviders( SpecialPerson.class )[0];
    ReaderProvider readerProvider = s.getSearchFactory().getReaderProvider();
    IndexReader reader = readerProvider.openReader( directoryProvider );
    IndexSearcher searcher = new IndexSearcher( reader );

    try {
      // we have to test using Lucene directly since query loaders will ignore hits for which there is no
      // database entry
      TopDocs topDocs = searcher.search( luceneQuery, null, 1 );
      assertTrue( "We should have no hit", topDocs.totalHits == 0 );
    }
    finally {
      readerProvider.closeReader( reader );
    }
    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.