Package org.hibernate.search

Examples of org.hibernate.search.FullTextSession


    /**
     * {@inheritDoc}
     */
    public List<T> search(String searchTerm) throws SearchException {
        Session sess = getSession();
        FullTextSession txtSession = Search.getFullTextSession(sess);

        org.apache.lucene.search.Query qry;
        try {
            qry = HibernateSearchTools.generateQuery(searchTerm, this.persistentClass, sess, defaultAnalyzer);
        } catch (ParseException ex) {
            throw new SearchException(ex);
        }
        org.hibernate.search.FullTextQuery hibQuery = txtSession.createFullTextQuery(qry,
                this.persistentClass);
        return hibQuery.list();
    }
View Full Code Here


    /**
     * Flush search indexes, to be done after a reindex() or reindexAll() operation
     */
    public void flushSearchIndexes() {
        Session currentSession = sessionFactory.getCurrentSession();
        final FullTextSession fullTextSession = Search.getFullTextSession(currentSession);
        fullTextSession.flushToIndexes();
    }
View Full Code Here

    }
    return ftSession;
  }

  public FullTextQuery createFullTextQuery(org.apache.lucene.search.Query luceneQuery, Class<?>... entities) {
    FullTextSession ftSession = getFullTextSession();
    return new FullTextQueryImpl( ftSession.createFullTextQuery( luceneQuery, entities ), ftSession );
  }
View Full Code Here

  public void index(String entity) {
    Class<?> clazz = getEntityClass( entity );

    SessionFactory factory = getSessionFactory();
    Session session = factory.openSession();
    FullTextSession fulltextSession = Search.getFullTextSession( session );
    try {
      fulltextSession.createIndexer( clazz )
          .batchSizeToLoadObjects( batchSize )
          .cacheMode( CacheMode.NORMAL )
          .threadsToLoadObjects( numberOfObjectLoadingThreads )
          .threadsForSubsequentFetching( numberOfFetchingThreads )
          .startAndWait();
View Full Code Here

  public void optimize(String entity) {
    Class<?> clazz = getEntityClass( entity );

    SessionFactory factory = getSessionFactory();
    Session session = factory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession( session );
    fullTextSession.beginTransaction();
    fullTextSession.getSearchFactory().optimize( clazz );
    fullTextSession.getTransaction().commit();
    session.close();
  }
View Full Code Here

  public void purge(String entity) {
    Class<?> clazz = getEntityClass( entity );

    SessionFactory factory = getSessionFactory();
    Session session = factory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession( session );
    fullTextSession.beginTransaction();
    fullTextSession.purgeAll( clazz );
    fullTextSession.getTransaction().commit();
    session.close();
  }
View Full Code Here

    // searchFactoryImplementor = lookupSearchFactory( registry );
  }

  @Override
  public Query getParsedQueryExecutor(OgmSession session, String queryString, Map<String, Object> namedParameters) {
    FullTextSession fullTextSession = Search.getFullTextSession( session );

    LuceneQueryParsingResult parsingResult = new QueryParser().parseQuery( queryString,
        createProcessingChain( session, unwrap( namedParameters ), fullTextSession ) );

    log.createdQuery( queryString, parsingResult.getQuery() );

    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( parsingResult.getQuery(), parsingResult.getTargetEntity() );
    if ( requiresProjections( parsingResult.getProjections() ) ) {
      fullTextQuery.setProjection( parsingResult.getProjections().toArray( new String[parsingResult.getProjections().size()] ) );
    }

    fullTextQuery.setSort( parsingResult.getSort() );
View Full Code Here

*/
public class QueryUnindexedEntityTest extends SearchTestCase {

  public void testQueryOnAllEntities() throws Exception {

    FullTextSession s = Search.getFullTextSession( openSession() );

    Transaction tx = s.beginTransaction();
    Person person = new Person();
    person.setName( "Jon Doe" );
    s.save( person );
    tx.commit();

    tx = s.beginTransaction();
    QueryParser parser = new QueryParser( "name", new StandardAnalyzer() );
    Query query = parser.parse( "name:foo" );
    FullTextQuery hibQuery = s.createFullTextQuery( query );
    try {
      hibQuery.list();
      fail();
    }
    catch ( HibernateException e ) {
      assertTrue( "Wrong message", e.getMessage().startsWith( "There are no mapped entities" ) );
    }

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

   * Test that we can change the default sort order of the lucene search result.
   *
   * @throws Exception in case the test fails.
   */
  public void testList() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    createTestBooks(s);
    Transaction tx = s.beginTransaction();
    QueryParser parser = new QueryParser("title", new StopAnalyzer() );

    Query query = parser.parse( "summary:lucene" );
    FullTextQuery hibQuery = s.createFullTextQuery( query, Book.class );
    List<Book> result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 3, result.size() );
    // make sure that the order is according to in which order the books got inserted
    // into the index.
    int id = 1;
    for(Book b : result) {
      assertEquals( "Expected another id", Integer.valueOf( id ), b.getId() );
      id++;
    }

    // now the same query, but with a lucene sort specified.
    query = parser.parse( "summary:lucene" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    Sort sort = new Sort(new SortField("id", true));
    hibQuery.setSort(sort);
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 3, result.size() );
    id = 3;
    for(Book b : result) {
      assertEquals("Expected another id", Integer.valueOf( id ), b.getId());
      id--;
    }

    // order by summary
    query = parser.parse( "summary:lucene OR summary:action" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    sort = new Sort( new SortField( "summary_forSort", false ) ); //ASC
    hibQuery.setSort( sort );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 4, result.size() );
    assertEquals( "Groovy in Action", result.get( 0 ).getSummary() );

    // order by summary backwards
    query = parser.parse( "summary:lucene OR summary:action" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    sort = new Sort( new SortField( "summary_forSort", true ) ); //DESC
    hibQuery.setSort( sort );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 4, result.size() );
    assertEquals( "Hibernate & Lucene", result.get( 0 ).getSummary() );

    // order by date backwards
    query = parser.parse( "summary:lucene OR summary:action" );
    hibQuery = s.createFullTextQuery( query, Book.class );
    sort = new Sort( new SortField( "publicationDate", SortField.STRING, true ) ); //DESC
    hibQuery.setSort( sort );
    result = hibQuery.list();
    assertNotNull( result );
    assertEquals( "Wrong number of test results.", 4, result.size() );
    for (Book book : result) {
      System.out.println(book.getSummary() + " : " + book.getPublicationDate() );
    }
    assertEquals( "Groovy in Action", result.get( 0 ).getSummary() );

    tx.commit();

    deleteTestBooks(s);
    s.close();
  }
View Full Code Here

/**
* @author Emmanuel Bernard
*/
public class ExplanationTest extends SearchTestCase {
  public void testExplanation() throws Exception {
    FullTextSession s = Search.getFullTextSession( openSession() );
    Transaction tx = s.beginTransaction();
    Dvd dvd = new Dvd("The dark knight", "Batman returns with it best enomy the Jocker. The dark side of this movies shows up pretty quickly");
    s.persist( dvd );
    dvd = new Dvd("Wall-e", "The tiny little robot comes to Eartch after the dark times and tries to clean it");
    s.persist( dvd );
    tx.commit();
    s.clear();

    tx = s.beginTransaction();
    Map<String, Float> boosts = new HashMap<String, Float>(2);
    boosts.put( "title", new Float(4) );
    boosts.put( "description", new Float(1) );
    MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {"title", "description"}, new StandardAnalyzer(), boosts);
    Query luceneQuery = parser.parse( "dark" );
    FullTextQuery ftQuery = s.createFullTextQuery( luceneQuery, Dvd.class )
        .setProjection( FullTextQuery.DOCUMENT_ID, FullTextQuery.EXPLANATION, FullTextQuery.THIS );
    @SuppressWarnings("unchecked") List<Object[]> results = ftQuery.list();
    assertEquals( 2, results.size() );
    for (Object[] result : results) {
      assertEquals( ftQuery.explain( (Integer) result[0] ).toString(), result[1].toString() );
      s.delete( result[2] );
    }
    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.