代码之家  ›  专栏  ›  技术社区  ›  Batakj

如果索引相同的数据,为什么Lucene索引的大小会增加?

  •  0
  • Batakj  · 技术社区  · 17 年前

    我在我的应用程序中实现了Hibernate搜索,即基于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();
    
    1 回复  |  直到 11 年前
        1
  •  6
  •   itsadok    17 年前

    我对Hibernate一无所知,但在Lucene中,删除的文档在优化前都会保留在索引上。这可以解释为什么指数只在增长。

    尝试对索引运行optimize()。不知道你从Hibernate怎么做(我看到这是一个方法 SearchFactory )

    希望这有帮助。

    推荐文章