我正在尝试将数据库中已经存在的大约21,000个实体添加到nhibernate-search Lucene索引中。完成后,索引大小约为12MB。我认为时间可能会有很大的不同,但它总是非常慢。在我的最后一次运行(使用调试器运行)中,索引数据花费了超过12分钟。
private void IndexProducts(ISessionFactory sessionFactory)
{
using (var hibernateSession = sessionFactory.GetCurrentSession())
using (var luceneSession = Search.CreateFullTextSession(hibernateSession))
{
var tx = luceneSession.BeginTransaction();
foreach (var prod in hibernateSession.Query<Product>())
{
luceneSession.Index(prod);
hibernateSession.Evict(prod);
}
hibernateSession.Clear();
tx.Commit();
}
}大部分时间都花在tx.Commit()上。从我读到的Hibernate search来看,这是意料之中的。我遇到了相当多的方法来提供帮助,比如MassIndexer、flushToIndexes、批处理模式等,但据我所知,这些都是Java特有的选项。
会话清除和驱逐只是我不顾一切的举动-我还没有看到它们以这样或那样的方式有所不同。
有没有人成功地快速索引了大量现有数据?
发布于 2012-01-28 00:48:17
我已经能够通过组合使用批处理和事务来显著提高索引速度。
我的初始代码花了大约30分钟来索引大约20.000个实体。使用下面的代码,我已经将时间缩短到了大约4分钟。
private void IndexEntities<TEntity>(IFullTextSession session) where TEntity : class
{
var currentIndex = 0;
const int batchSize = 500;
while (true)
{
var entities = session
.CreateCriteria<TEntity>()
.SetFirstResult(currentIndex)
.SetMaxResults(batchSize)
.List();
using (var tx = session.BeginTransaction())
{
foreach (var entity in entities)
{
session.Index(entity);
}
currentIndex += batchSize;
session.Flush();
tx.Commit();
session.Clear();
}
if (entities.Count < batchSize)
break;
}
}发布于 2011-03-04 06:45:37
这取决于您可以设置的lucene选项。查看this page并检查nhibernate-search是否为这些选项提供了包装器。如果没有,请修改它的源代码。
https://stackoverflow.com/questions/5187490
复制相似问题