Package org.hibernate.search.jpa

Examples of org.hibernate.search.jpa.FullTextEntityManager


    @Override
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public <T extends DomainEntity> List<T> search(Class<T> clazz, String field, String query, int offset, int number) {
        FullTextEntityManager fullTextEntityManager = Search.createFullTextEntityManager(getEntityManager());
        MultiFieldQueryParser parser = new MultiFieldQueryParser(getAllIndexedFields(clazz, field), new StandardAnalyzer());
        try {
            org.apache.lucene.search.Query parsedquery = parser.parse(query.trim().replaceAll(" ", "* ") + "*");
            FullTextQuery hq = fullTextEntityManager.createFullTextQuery(parsedquery, clazz);
            hq.setMaxResults(number > 0 ? number : MAX_RESULTS);
            hq.setFirstResult(offset > 0 ? offset : 0);
            return hq.getResultList();
        } catch (Exception ex) {
            throw new IWebMvcException("Could not perform search", ex);
View Full Code Here


    @Transactional(readOnly = true)
    @Override public final <T extends DomainEntity> List<T> search(Class<T> clazz, String query, int offset, int number) {
      Assert.notNull(clazz);
      Assert.hasText(query);
        try {
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
            List<String> indexedFields = getAllIndexedFields(clazz, "");
            MultiFieldQueryParser parser = new MultiFieldQueryParser(indexedFields.toArray(new String[indexedFields.size()]), new StandardAnalyzer());
            FullTextQuery hq = fullTextEntityManager.createFullTextQuery(parser.parse(query.trim().replaceAll(" ", "* ") + "*"), clazz);
            hq.setMaxResults(number > 0 ? number : MAX_RESULTS);
            hq.setFirstResult(offset > 0 ? offset : 0);
            return hq.getResultList();
        } catch (Exception ex) {
            throw new IWebMvcException("Could not perform search", ex);
View Full Code Here

            // Search in all indexed fields

            IndexReaderAccessor readerAccessor = null;
            IndexReader reader = null;
            try {
                FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);

                // obtain analyzer to parse the query:
                Analyzer analyzer;
                if (searchedEntity == null) {
                    analyzer = defaultAnalyzer;
                } else {
                    analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer(searchedEntity);
                }

                // search on all indexed fields: generate field list, removing internal hibernate search field name: _hibernate_class
                // TODO: possible improvement: cache the fields of each entity
                SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
                readerAccessor = searchFactory.getIndexReaderAccessor();
                reader = readerAccessor.open(searchedEntity);
                Collection<String> fieldNames = new HashSet<>();
                for (FieldInfo fieldInfo : ReaderUtil.getMergedFieldInfos(reader)) {
                    if (fieldInfo.isIndexed) {
View Full Code Here

     *
     * @param clazz the class
     * @param entityManager the entity manager
     */
    public static void reindex(Class clazz, EntityManager entityManager) {
        FullTextEntityManager txtentityManager = Search.getFullTextEntityManager(entityManager);
        MassIndexer massIndexer = txtentityManager.createIndexer(clazz);
        try {
            massIndexer.startAndWait();
        } catch (InterruptedException e) {
            log.error("mass reindexing interrupted: " + e.getMessage());
        } finally {
            txtentityManager.flushToIndexes();
        }
    }
View Full Code Here

     *
     * @param async true if the reindexing will be done as a background thread
     * @param entityManager the entity manager
     */
    public static void reindexAll(boolean async, EntityManager entityManager) {
        FullTextEntityManager txtentityManager = Search.getFullTextEntityManager(entityManager);
        MassIndexer massIndexer = txtentityManager.createIndexer();
        massIndexer.purgeAllOnStart(true);
        try {
            if (!async) {
                massIndexer.startAndWait();
            } else {
                massIndexer.start();
            }
        } catch (InterruptedException e) {
            log.error("mass reindexing interrupted: " + e.getMessage());
        } finally {
            txtentityManager.flushToIndexes();
        }
    }
View Full Code Here

public class HibernateSearchAtopOgmTest extends JpaTestCase {

  @Test
  public void testHibernateSearchJPAAPIUsage() throws Exception {
    getTransactionManager().begin();
    final FullTextEntityManager ftem = Search.getFullTextEntityManager( getFactory().createEntityManager() );
    final Insurance insurance = new Insurance();
    insurance.setName( "Macif" );
    ftem.persist( insurance );
    getTransactionManager().commit();

    ftem.clear();

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

    }
  }

  private void assertEntityHasBeenIndexed() throws NotSupportedException, SystemException, Exception {
    boolean operationSuccessful = false;
    FullTextEntityManager fullTextEm = null;
    try {
      getTransactionManager().begin();
      fullTextEm = Search.getFullTextEntityManager( createEntityManager() );
      QueryBuilder queryBuilder = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity( IndexedNews.class ).get();
      Query luceneQuery = queryBuilder.keyword().wildcard().onField( "newsId" ).ignoreFieldBridge().matching( "tit*" ).createQuery();
      @SuppressWarnings("unchecked")
      List<IndexedNews> list = fullTextEm.createFullTextQuery( luceneQuery ).getResultList();
      assertThat( list ).hasSize( 1 );

      List<IndexedLabel> labels = list.get( 0 ).getLabels();
      assertThat( labels ).hasSize( 2 );
      assertThat( contains( labels, "massindex" ) ).isTrue();
View Full Code Here

  }

  @SuppressWarnings("unchecked")
  private void assertAssociatedElementsHaveBeenIndexed() throws NotSupportedException, SystemException, Exception {
    boolean operationSuccessful = false;
    FullTextEntityManager fullTextEm = null;
    try {
      getTransactionManager().begin();
      fullTextEm = Search.getFullTextEntityManager( createEntityManager() );
      QueryBuilder b = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity( IndexedLabel.class ).get();
      {
        Query luceneQuery = b.keyword().wildcard().onField( "name" ).matching( "tes*" ).createQuery();
        List<IndexedLabel> labels = fullTextEm.createFullTextQuery( luceneQuery ).getResultList();
        assertThat( labels ).hasSize( 1 );
        assertThat( contains( labels, "test" ) ).isTrue();
      }
      {
        Query luceneQuery = b.keyword().wildcard().onField( "name" ).matching( "mas*" ).createQuery();
        List<IndexedLabel> labels = fullTextEm.createFullTextQuery( luceneQuery ).getResultList();
        assertThat( labels ).hasSize( 1 );
        assertThat( contains( labels, "massindex" ) ).isTrue();
      }
      operationSuccessful = true;
    }
View Full Code Here

  private EntityManager createEntityManager() {
    return getFactory().createEntityManager();
  }

  private void startAndWaitMassIndexing(Class<?>... entityTypes) throws InterruptedException {
    FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( createEntityManager() );
    fullTextEm.createIndexer( entityTypes ).purgeAllOnStart( true ).startAndWait();
    int numDocs = fullTextEm.getSearchFactory().getIndexReaderAccessor().open( entityTypes ).numDocs();
    close( fullTextEm );
    assertThat( numDocs ).isGreaterThan( 0 );
  }
View Full Code Here

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

  private void purgeAll(Class<?>... entityTypes) throws Exception {
    FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( createEntityManager() );
    for ( Class<?> entityType : entityTypes ) {
      fullTextEm.purgeAll( entityType );
      fullTextEm.flushToIndexes();
    }
    int numDocs = fullTextEm.getSearchFactory().getIndexReaderAccessor().open( entityTypes ).numDocs();
    close( fullTextEm );
    assertThat( numDocs ).isEqualTo( 0 );
  }
View Full Code Here

TOP

Related Classes of org.hibernate.search.jpa.FullTextEntityManager

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.