private void purgeAll(Class<?> entityType, String tenantId) {
   FullTextSession session = Search.getFullTextSession(openSessionWithTenantId(tenantId));
   session.purgeAll(entityType);
   session.flushToIndexes();
   int numDocs = session.getSearchFactory().getIndexReaderAccessor().open(entityType).numDocs();
   session.close();
   assertThat(numDocs).isEqualTo(0);
 }
  @Test
  public void indexingTest() {
    POI poi = new POI(1, "Test", 24.0d, 32.0d, "");

    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session);

    try {
      session.save(poi);
      fullTextSession.index(poi);
      fullTextSession.flushToIndexes();
    } catch (Exception e) {
      e.printStackTrace();
      Assert.fail("Exception thrown when index point");
    }

    try {
      Point center = Point.fromDegrees(24, 31.5); // 50.79 km fromBoundingCircle 24.32

      org.apache.lucene.search.Query luceneQuery =
          SpatialQueryBuilder.buildGridQuery(center, 50, "location");
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery, POI.class);
      List results = hibQuery.list();
      Assert.assertEquals(1, results.size());

      org.apache.lucene.search.Query luceneQuery2 =
          SpatialQueryBuilder.buildGridQuery(center, 1, "location");
      org.hibernate.Query hibQuery2 = fullTextSession.createFullTextQuery(luceneQuery2, POI.class);
      List results2 = hibQuery2.list();
      Assert.assertEquals(0, results2.size());

      org.apache.lucene.search.Query luceneQuery3 =
          SpatialQueryBuilder.buildDistanceQuery(center, 50, "location");
      org.hibernate.Query hibQuery3 = fullTextSession.createFullTextQuery(luceneQuery3, POI.class);
      List results3 = hibQuery3.list();
      Assert.assertEquals(0, results3.size());

      org.apache.lucene.search.Query luceneQuery4 =
          SpatialQueryBuilder.buildSpatialQuery(center, 50, "location");
      org.hibernate.Query hibQuery4 = fullTextSession.createFullTextQuery(luceneQuery4, POI.class);
      List results4 = hibQuery4.list();
      Assert.assertEquals(0, results4.size());

      org.apache.lucene.search.Query luceneQuery5 =
          SpatialQueryBuilder.buildSpatialQuery(center, 51, "location");
      org.hibernate.Query hibQuery5 = fullTextSession.createFullTextQuery(luceneQuery5, POI.class);
      List results5 = hibQuery5.list();
      Assert.assertEquals(1, results5.size());

    } catch (Exception e) {
      e.printStackTrace();
      Assert.fail("Exception thrown when querying point");
    }
  }
  @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;
  }
Esempio n. 4
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();
    }
  }
  protected void performCleanUp(SessionFactory sf) {
    log("starting clean up phase");

    FullTextSession s = Search.getFullTextSession(sf.openSession());
    try {
      Transaction tx = s.beginTransaction();
      s.createSQLQuery("delete from book_author where book_id < :id")
          .setParameter("id", initialOffset)
          .executeUpdate();
      s.createSQLQuery("delete from book where id < :id")
          .setParameter("id", initialOffset)
          .executeUpdate();
      s.createSQLQuery("delete from author where id < :id")
          .setParameter("id", initialOffset)
          .executeUpdate();

      s.purgeAll(Book.class);
      s.flush();
      s.flushToIndexes();
      tx.commit();
    } finally {
      s.close();
    }
  }
  public static void LoadGeonames() {
    Session session = null;
    FullTextSession fullTextSession = null;
    BufferedReader buffRead = null;
    SessionFactory sessionFactory = null;
    try {
      sessionFactory =
          new Configuration().configure(hibernateConfigurationFile).buildSessionFactory();

      session = sessionFactory.openSession();
      session.createSQLQuery("delete from MemberLevelTestPoI").executeUpdate();
      session.beginTransaction();
      fullTextSession = Search.getFullTextSession(session);

      File geonamesFile = new File(geonamesDataFile);
      buffRead = new BufferedReader(new FileReader(geonamesFile));
      String line = null;

      int line_number = 0;
      while ((line = buffRead.readLine()) != null) {
        String[] data = line.split("\t");
        POI current =
            new POI(
                Integer.parseInt(data[0]),
                data[1],
                Double.parseDouble(data[4]),
                Double.parseDouble(data[5]),
                data[7]);
        session.save(current);
        if ((line_number % 10000) == 0) {
          fullTextSession.flushToIndexes();
          session.getTransaction().commit();
          session.close();
          session = sessionFactory.openSession();
          fullTextSession = Search.getFullTextSession(session);
          session.beginTransaction();
          System.out.println(Integer.toString(line_number));
        }
        line_number++;
      }
      session.getTransaction().commit();
      fullTextSession.close();
      sessionFactory.close();
      buffRead.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fullTextSession != null && fullTextSession.isOpen()) {
        Transaction transaction = fullTextSession.getTransaction();
        if (transaction != null && transaction.isActive()) {
          transaction.rollback();
        }
        fullTextSession.close();
      }
      if (sessionFactory != null && !sessionFactory.isClosed()) {
        sessionFactory.close();
      }
      try {
        if (buffRead != null) {
          buffRead.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
Esempio n. 7
0
 /** 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();
 }