@SuppressWarnings("unchecked")
  @Override
  public List<Item> findItems(String searchString) {
    List<Item> resultList = new ArrayList<Item>();
    EntityManagerFactory factory = emf;
    EntityManager em = factory.createEntityManager();
    FullTextEntityManager fullTextEntityManager =
        org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
    Session session = (Session) fullTextEntityManager.getDelegate();
    FullTextSession fullTextSession = Search.getFullTextSession(session);

    //		MassIndexer massIndexer = fullTextSession.createIndexer();
    //		try {
    //			fullTextSession.createIndexer().start();
    /*		} catch (InterruptedException e) {
    	e.printStackTrace();
    }*/
    Transaction transaction = fullTextSession.beginTransaction();
    fullTextSession.setFlushMode(FlushMode.MANUAL);
    fullTextSession.setCacheMode(CacheMode.IGNORE);
    ScrollableResults results =
        fullTextSession.createCriteria(Item.class).setFetchSize(20).scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
      index++;
      fullTextSession.index(results.get(0)); // index each element
      if (index % 20 == 0) {
        fullTextSession.flushToIndexes(); // apply changes to indexes
        fullTextSession.clear(); // free memory since the queue is processed
      }
    }
    transaction.commit();

    em.getTransaction().begin();

    QueryBuilder qb =
        fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Item.class).get();
    Query query =
        qb.keyword()
            .fuzzy()
            .onFields("alias", "tagName")
            .ignoreFieldBridge()
            .matching(searchString)
            .createQuery();
    FullTextQuery ftq = fullTextEntityManager.createFullTextQuery(query, Item.class);
    resultList = ftq.getResultList();
    em.getTransaction().commit();
    em.close();

    return resultList;
  }
Пример #2
0
  private void reindex(Class<?> clazz) {
    log.info("Re-indexing {0}", clazz);

    ScrollableResults results = null;
    try {
      session.purgeAll(clazz);

      // TODO try this, see how it affects reindexing time:
      // session.flushToIndexes();
      // session.getSearchFactory().optimize(clazz);

      session.setFlushMode(FlushMode.MANUAL);
      session.setCacheMode(CacheMode.IGNORE);

      results =
          session.createCriteria(clazz).setFetchSize(BATCH_SIZE).scroll(ScrollMode.FORWARD_ONLY);

      int index = 0;
      while (results.next()) {
        objectProgress++;
        index++;
        session.index(results.get(0)); // index each element
        if (index % BATCH_SIZE == 0) {
          session.flushToIndexes(); // apply changes to indexes
          session.clear(); // clear since the queue is processed
        }
      }
      session.flushToIndexes(); // apply changes to indexes
      // TODO try this too, see how it affects reindexing time:
      // session.getSearchFactory().optimize(clazz);
      session.clear(); // clear since the queue is processed
    } catch (Exception e) {
      log.warn("Unable to index objects of type {0}", e, clazz.getName());
      hasError = true;
    } finally {
      if (results != null) results.close();
    }
  }