Package org.hibernate.search

Examples of org.hibernate.search.FullTextSession


            log.debug("deleting indexed documents of entity: " + entityClass.getName());
            userTx = (UserTransaction)org.jboss.seam.Component.getInstance("org.jboss.seam.transaction.transaction");
            userTx.begin();

            EntityManager em = (EntityManager) Component.getInstance("entityManager");
            FullTextSession ftSession = (FullTextSession)em.getDelegate();

            // Delete all documents with "_hibernate_class" term of the selected entity
            DirectoryProvider dirProvider = ftSession.getSearchFactory().getDirectoryProviders(entityClass)[0];
            IndexReader reader = IndexReader.open(dirProvider.getDirectory());

            // TODO: This is using an internal term of HSearch
            reader.deleteDocuments(new Term("_hibernate_class", entityClass.getName()));
            reader.close();

            // Optimize index
            progress.setStatus("Optimizing index");
            log.debug("optimizing index (merging segments)");
            ftSession.getSearchFactory().optimize(entityClass);

            userTx.commit();

            progress.setStatus("Building index");
            log.debug("indexing documents in batches of: " + batchSize);

            // Now re-index with HSearch
            em = (EntityManager) Component.getInstance("entityManager");
            ftSession = (FullTextSession)em.getDelegate();

            // TODO: Let's run this in auto-commit mode, assuming we have READ COMMITTED isolation anyway and non-repeatable reads
            //userTx.setTransactionTimeout(3600);
            //userTx.begin();

            // Use HQL instead of Criteria to eager fetch lazy properties
            String query = "select o from " + entityClass.getName() + " o fetch all properties";
            if (WikiNode.class.isAssignableFrom(entityClass)) {
                // If it's a WikiNode, fetch the associated User instances, avoiding N+1 selects
                query = "select o from " + entityClass.getName() + " o inner join fetch o.createdBy left join fetch o.lastModifiedBy fetch all properties";
            }
            ScrollableResults cursor = ftSession.createQuery(query).scroll();

            cursor.last();
            int count = cursor.getRowNumber() + 1;
            log.debug("total documents in database: " + count);

            if (count > 0) {
                cursor.first(); // Reset to first result row
                int i = 0;
                while (true) {
                    i++;
                    Object o = cursor.get(0);
                    log.debug("indexing instance " + i + ": " + o);
                    ftSession.index(o);
                    if (i % batchSize == 0) {
                        log.debug("ending batch, beginning new batch");
                        ftSession.clear(); // Clear persistence context for each batch
                    }
                    progress.setPercentComplete( new Double(100d/count*i).intValue() );
                    log.debug(progress);

                    if (cursor.isLast())
View Full Code Here


   }

   @SuppressWarnings("unchecked")
   private void indexProducts()
   {
       FullTextSession fullTextSession = getFullTextSession();
       List results = fullTextSession.createCriteria(Product.class)
            .setFetchMode("actors", FetchMode.JOIN)
            .setFetchMode("categories", FetchMode.JOIN)
            .list();
      for (Object obj : results)
      {
         fullTextSession.index(obj);
      }
   }
View Full Code Here

   }

   @SuppressWarnings("unchecked")
   private void indexAllClasses(Class... entityTypes)
   {
      FullTextSession fullTextSession = getFullTextSession();
      for (Class entityType : entityTypes)
      {
         for (Object obj : fullTextSession.createCriteria(entityType).list())
         {
            fullTextSession.index(obj);
         }
      }
   }
View Full Code Here

  @Test
  public void testHibernateSearchJPAAPIUsage() throws Exception {
    final Session session = openSession();
    Transaction transaction = session.beginTransaction();
    final FullTextSession fts = Search.getFullTextSession( session );
    final Insurance insurance = new Insurance();
    insurance.setName( "Macif" );
    fts.persist( insurance );
    transaction.commit();

    fts.clear();

    transaction = fts.beginTransaction();
    final QueryBuilder b = fts.getSearchFactory()
        .buildQueryBuilder()
        .forEntity( Insurance.class )
        .get();
    final Query lq = b.keyword().onField( "name" ).matching( "Macif" ).createQuery();
    final FullTextQuery ftQuery = fts.createFullTextQuery( lq, Insurance.class );
    ftQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );
    final List<Insurance> resultList = ftQuery.list();
    assertThat( resultList ).hasSize( 1 );
    for ( Object e : resultList ) {
      fts.delete( e );
    }
    transaction.commit();
    fts.close();
  }
View Full Code Here

  @Test
  public void testHibernateSearchNativeAPIUsage() throws Exception {
    getTransactionManager().begin();
    final EntityManager entityManager = getFactory().createEntityManager();
    final FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession( entityManager.unwrap( Session.class ) );
    final Insurance insurance = new Insurance();
    insurance.setName( "Macif" );
    ftSession.persist( insurance );
    getTransactionManager().commit();

    ftSession.clear();

    getTransactionManager().begin();
    final QueryBuilder b = ftSession.getSearchFactory()
        .buildQueryBuilder()
        .forEntity( Insurance.class )
        .get();
    final Query lq = b.keyword().onField( "name" ).matching( "Macif" ).createQuery();
    final org.hibernate.search.FullTextQuery ftQuery = ftSession.createFullTextQuery( lq, Insurance.class );
    ftQuery.initializeObjectsWith( ObjectLookupMethod.SKIP, DatabaseRetrievalMethod.FIND_BY_ID );
    final List<Insurance> resultList = ftQuery.list();
    assertThat( getFactory().getPersistenceUnitUtil().isLoaded( resultList.get( 0 ) ) ).isTrue();
    assertThat( resultList ).hasSize( 1 );
    for ( Object e : resultList ) {
      ftSession.delete( e );
    }
    getTransactionManager().commit();
    entityManager.close();
  }
View Full Code Here

    {
      purgeAll( Insurance.class );
      startAndWaitMassIndexing( Insurance.class );
    }
    {
      FullTextSession session = Search.getFullTextSession( openSession() );
      QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( Insurance.class ).get();
      Query luceneQuery = queryBuilder.keyword().wildcard().onField( "name" ).matching( "ins*" ).createQuery();
      Transaction transaction = session.beginTransaction();
      @SuppressWarnings("unchecked")
      List<Insurance> list = session.createFullTextQuery( luceneQuery ).list();
      assertThat( list ).hasSize( 1 );
      assertThat( list.get( 0 ).getName() ).isEqualTo( "Insurance Corporation" );
      transaction.commit();
      session.clear();
      session.close();
    }
  }
View Full Code Here

      purgeAll( IndexedNews.class );
      startAndWaitMassIndexing( IndexedNews.class );
    }
    {
      // Assert index creation
      FullTextSession session = Search.getFullTextSession( openSession() );
      QueryBuilder queryBuilder = session.getSearchFactory().buildQueryBuilder().forEntity( IndexedNews.class ).get();
      Query luceneQuery = queryBuilder.keyword().wildcard().onField( "newsId" ).ignoreFieldBridge().matching( "tit*" ).createQuery();
      Transaction transaction = session.beginTransaction();
      @SuppressWarnings("unchecked")
      List<IndexedNews> list = session.createFullTextQuery( luceneQuery ).list();
      assertThat( list ).hasSize( 1 );
      assertThat( list.get( 0 ).getContent() ).isEqualTo( "content" );
      assertThat( list.get( 0 ).getNewsId().getTitle() ).isEqualTo( "title" );
      assertThat( list.get( 0 ).getNewsId().getAuthor() ).isEqualTo( "author" );
      transaction.commit();
      session.clear();
      session.close();
    }
  }
View Full Code Here

  public static void deleteIndexDir() throws Exception {
    FileHelper.delete( indexDir );
  };

  private void startAndWaitMassIndexing(Class<?> entityType) throws InterruptedException {
    FullTextSession session = Search.getFullTextSession( openSession() );
    session.createIndexer( entityType ).purgeAllOnStart( true ).startAndWait();
    int numDocs = session.getSearchFactory().getIndexReaderAccessor().open( entityType ).numDocs();
    session.close();
    assertThat( numDocs ).isGreaterThan( 0 );
  }
View Full Code Here

    session.close();
    assertThat( numDocs ).isGreaterThan( 0 );
  }

  private void purgeAll(Class<?> entityType) {
    FullTextSession session = Search.getFullTextSession( openSession() );
    session.purgeAll( entityType );
    session.flushToIndexes();
    int numDocs = session.getSearchFactory().getIndexReaderAccessor().open( entityType ).numDocs();
    session.close();
    assertThat( numDocs ).isEqualTo( 0 );
  }
View Full Code Here

  public void doCompile(Map replacements, boolean shallow) throws QueryException, MappingException {
  }

  @Override
  public List<?> list(SessionImplementor session, QueryParameters queryParameters) throws HibernateException {
    FullTextSession fullTextSession = Search.getFullTextSession( (Session) session );

    LuceneQueryParsingResult parsingResult = getLuceneQuery( queryParameters, fullTextSession );

    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( parsingResult.getQuery(), parsingResult.getTargetEntity() );

    if ( requiresProjections( parsingResult.getProjections() ) ) {
      fullTextQuery.setProjection( parsingResult.getProjections().toArray( new String[parsingResult.getProjections().size()] ) );
    }
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.