Beispiel #1
0
  public void testLazyLoading() throws Exception {
    Categorie cat = new Categorie("Livre");
    Entite ent = new Entite("Le temple des songes", cat);
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(cat);
    s.persist(ent);
    tx.commit();
    s.close();

    s = getSessionWithAutoCommit();
    FullTextSession session = Search.getFullTextSession(s);
    Query luceneQuery = new TermQuery(new Term("categorie.nom", "livre"));
    List result = session.createFullTextQuery(luceneQuery, Entite.class).list();
    assertEquals(1, result.size());
    s.close();

    s = getSessionWithAutoCommit();
    ent = (Entite) s.get(Entite.class, ent.getId());
    session = Search.getFullTextSession(s);
    session.index(ent);
    s.close();

    s = getSessionWithAutoCommit();
    session = Search.getFullTextSession(s);
    luceneQuery = new TermQuery(new Term("categorie.nom", "livre"));
    result = session.createFullTextQuery(luceneQuery, Entite.class).list();
    assertEquals("test lazy loading and indexing", 1, result.size());
    s.close();

    s = getSessionWithAutoCommit();
    Iterator it =
        s.createQuery("from Entite where id = :id").setParameter("id", ent.getId()).iterate();
    session = Search.getFullTextSession(s);
    while (it.hasNext()) {
      ent = (Entite) it.next();
      session.index(ent);
    }
    s.close();

    s = getSessionWithAutoCommit();
    session = Search.getFullTextSession(s);
    luceneQuery = new TermQuery(new Term("categorie.nom", "livre"));
    result = session.createFullTextQuery(luceneQuery, Entite.class).list();
    assertEquals("test lazy loading and indexing", 1, result.size());
    s.close();
  }
  @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");
    }
  }
Beispiel #3
0
  public void testBatchSize() throws Exception {
    FullTextSession s = Search.getFullTextSession(openSession());
    Transaction tx = s.beginTransaction();
    final int loop = 14;
    s.doWork(
        new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
            for (int i = 0; i < loop; i++) {
              Statement statmt = connection.createStatement();
              statmt.executeUpdate(
                  "insert into Domain(id, name) values( + " + (i + 1) + ", 'sponge" + i + "')");
              statmt.executeUpdate(
                  "insert into Email(id, title, body, header, domain_id) values( + "
                      + (i + 1)
                      + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope', "
                      + (i + 1)
                      + ")");
              statmt.close();
            }
          }
        });
    tx.commit();
    s.close();

    // check non created object does get found!!1
    s = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    ScrollableResults results = s.createCriteria(Email.class).scroll(ScrollMode.FORWARD_ONLY);
    int index = 0;
    while (results.next()) {
      index++;
      s.index(results.get(0));
      if (index % 5 == 0) {
        s.clear();
      }
    }
    tx
        .commit(); // if you get a LazyInitializationException, that's because we clear() the
                   // session in the loop.. it only works with a batch size of 5 (the point of the
                   // test)
    s.clear();
    tx = s.beginTransaction();
    QueryParser parser =
        new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer);
    List result = s.createFullTextQuery(parser.parse("body:create")).list();
    assertEquals(14, result.size());
    for (Object object : result) {
      s.delete(object);
    }
    tx.commit();
    s.close();
  }
  @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;
  }
Beispiel #5
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();
    }
  }
 public void indexEntity() {
   final List<T> results = getSession().createCriteria(this.type).list();
   final FullTextSession searchSession = getSearchSession();
   searchSession.flush();
   for (final T entity : results) searchSession.index(entity);
 }
Beispiel #7
0
  public List<Opportunity> findOpportunities(
      String position,
      String location,
      String skills,
      String additionalKeyword,
      String additionalSkillNames) {

    List OpportunityList = new ArrayList();
    SessionFactory sessionFactory = getHibernateTemplate().getSessionFactory();
    Session session = sessionFactory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session);
    Transaction tx1 = fullTextSession.beginTransaction();
    List<Opportunity> opportunities = session.createQuery("from Opportunity").list();
    for (Opportunity opportunity1 : opportunities) {
      fullTextSession.index(opportunity1);
    }

    MultiFieldQueryParser parser =
        new MultiFieldQueryParser(
            new String[] {"title", "skillsNeeded", "str_location"}, new StandardAnalyzer());

    org.apache.lucene.search.Query q = null;

    if (!position.equals("")
        && !skills.equals("")
        && !location.equals("")) { // when all the values are present
      try {
        q =
            parser.parse(
                new String[] {position, location, skills},
                new String[] {"title", "str_location", "skillsNeeded"},
                new StandardAnalyzer());
      } catch (ParseException e) {
        e.printStackTrace();
      }
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
      List result = hibQuery.list();
      OpportunityList = result;
    } else if (!position.equals("")
        && !location.equals("")
        && skills.equals("")) { // when only skill value is not present

      try {
        q =
            parser.parse(
                new String[] {position, location},
                new String[] {"title", "str_location"},
                new StandardAnalyzer());

      } catch (ParseException e) {
        e.printStackTrace();
      }

      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
      List result = hibQuery.list();
      OpportunityList = result;

    } else if (!position.equals("") && location.equals("") && !skills.equals("")) {
      // search wrt position and skills

      try {
        q =
            parser.parse(
                new String[] {position, skills},
                new String[] {"title", "skillsNeeded"},
                new StandardAnalyzer());

      } catch (ParseException e) {
        e.printStackTrace();
      }
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
      List result = hibQuery.list();
      OpportunityList = result;
    } else if (position.equals("")
        && !location.equals("")
        && !skills.equals("")) { // search wrt location and skills

      try {
        q =
            parser.parse(
                new String[] {location, skills},
                new String[] {"str_location", "skillsNeeded"},
                new StandardAnalyzer());

      } catch (ParseException e) {
        e.printStackTrace();
      }
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
      List result = hibQuery.list();
      OpportunityList = result;

    } else if (!position.equals("")
        && location.equals("")
        && skills.equals("")) { // search for position

      try {
        // q = parser.parse(new String[] { position },new String[] { "title" }, new
        // StandardAnalyzer());
        String titles = position + "*";
        q = parser.parse("title:" + titles);

      } catch (ParseException e) {
        e.printStackTrace();
      }
      if (additionalKeyword.equals("")) {
        org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
        List result = hibQuery.list();
        OpportunityList = result;
      } else {

        try {
          FullTextQuery hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
          List list = hibQuery.list();

          hibQuery
              .enableFullTextFilter("keyword")
              .setParameter("advancedKeyword", additionalKeyword);
          List filterList = hibQuery.list();

          if (list != null) {
            list.removeAll(filterList);
            filterList.addAll(list);
            list = filterList;
          } else {
            list = filterList;
          }

          OpportunityList = list;

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else if (position.equals("")
        && !location.equals("")
        && skills.equals("")) { // search for location

      try {
        q =
            parser.parse(
                new String[] {location}, new String[] {"str_location"}, new StandardAnalyzer());

      } catch (ParseException e) {
        e.printStackTrace();
      }
      org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
      List result = hibQuery.list();
      OpportunityList = result;

    } else if (position.equals("") && location.equals("") && !skills.equals("")) {

      List result = new ArrayList();
      String strSkill = null;

      if (skills.contains(",")) {

        strSkill = skills.replaceAll(",", " ");

      } else {
        strSkill = skills;
      }

      try {
        q = parser.parse("skillsNeeded:" + strSkill);

      } catch (ParseException e) {

        e.printStackTrace();
      }

      try {

        if (additionalSkillNames.equals("")) {

          FullTextQuery hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
          List list = hibQuery.list();
          result.addAll(list);
        } else {
          FullTextQuery hibQuery = fullTextSession.createFullTextQuery(q, Opportunity.class);
          List list = hibQuery.list();
          hibQuery
              .enableFullTextFilter("skills")
              .setParameter("advancedSkills", additionalSkillNames);
          List filterList = hibQuery.list();
          List<Opportunity> result1 = list;
          if (list != null) {
            list.removeAll(filterList);
            filterList.addAll(list);
            list = filterList;
          } else {
            list = filterList;
          }

          result.addAll(list);
        }

        OpportunityList = result;
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    tx1.commit();
    session.close();

    return OpportunityList;
  }
Beispiel #8
0
  public void testTransactional() throws Exception {
    FullTextSession s = Search.getFullTextSession(openSession());
    Transaction tx = s.beginTransaction();
    final int loop = 4;
    for (int i = 0; i < loop; i++) {
      Email email = new Email();
      email.setId((long) i + 1);
      email.setTitle("JBoss World Berlin");
      email.setBody("Meet the guys who wrote the software");
      s.persist(email);
    }
    tx.commit();
    s.close();

    // check non created object does get found!!1
    s = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    QueryParser parser =
        new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer);
    List result = s.createFullTextQuery(parser.parse("body:create")).list();
    assertEquals(0, result.size());
    tx.commit();
    s.close();

    s = Search.getFullTextSession(openSession());
    s.getTransaction().begin();
    s.doWork(
        new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
            Statement stmt = connection.createStatement();
            stmt.executeUpdate("update Email set body='Meet the guys who write the software'");
            stmt.close();
          }
        });
    s.doWork(
        new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
            // insert an object never indexed
            Statement stmt = connection.createStatement();
            stmt.executeUpdate(
                "insert into Email(id, title, body, header) values( + "
                    + (loop + 1)
                    + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope')");
            stmt.close();
          }
        });

    s.getTransaction().commit();
    s.close();

    s = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    parser =
        new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer);
    result = s.createFullTextQuery(parser.parse("body:write")).list();
    assertEquals(0, result.size());
    result = s.createCriteria(Email.class).list();
    for (int i = 0; i < loop / 2; i++) {
      s.index(result.get(i));
    }
    tx.commit(); // do the process
    s.index(result.get(loop / 2)); // do the process out of tx
    tx = s.beginTransaction();
    for (int i = loop / 2 + 1; i < loop; i++) {
      s.index(result.get(i));
    }
    tx.commit(); // do the process
    s.close();

    s = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    // object never indexed
    Email email = (Email) s.get(Email.class, Long.valueOf(loop + 1));
    s.index(email);
    tx.commit();
    s.close();

    // check non indexed object get indexed by s.index
    s = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    result = s.createFullTextQuery(parser.parse("body:create")).list();
    assertEquals(1, result.size());
    tx.commit();
    s.close();
  }