0

我在我的应用程序中实现了 Hibernate Search,即基于 Lucene。每当我索引数据库时,lucene 索引的大小都会增加。但是,查询的结果每次都返回相同的结果。

如果我每次索引相同的数据,为什么 lucene 的大小每次都会增加?

FullTextSession fullTextSession = Search.getFullTextSession(getSession());
    org.hibernate.Transaction tx = fullTextSession.beginTransaction();

    Criteria criteria = fullTextSession.createCriteria(getPersistentClass())
    .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY)
    .setCacheMode(CacheMode.IGNORE)
    .setFetchSize(pageSize)
    .setFlushMode(FlushMode.MANUAL);


    int i = 0;
    List<ProdAttrAssociationVO> results = null;
    do {
      criteria = criteria.setFirstResult(i)
        .setMaxResults(pageSize);
      results = criteria.list();

      for (ProdAttrAssociationVO entity : results) {
        fullTextSession.delete(entity);
        fullTextSession.index(entity);
      }

      // flush the index changes to disk so we don't hold until a commit
      if (i % batchSize == 0) {
        fullTextSession.flushToIndexes();
        fullTextSession.clear();
      }

      i += pageSize;
    } while (results.size() > 0);


    System.out.println("ProdAttrAssociation Indexing Completed");
    tx.commit();
4

1 回答 1

6

我对 Hibernate 一无所知,但通常在 Lucene 中,删除的文档会保留在索引上,直到它被优化。这可以解释为什么你看到指数只会增长。

尝试在索引上运行 optimize()。不知道你是如何从 Hibernate 做到的(我看到它是SearchFactory的一种方法)。

希望这可以帮助。

于 2009-06-21T09:08:14.563 回答