【问题标题】:NHibernate.Search + Lucene.NET duplicate entriesNHibernate.Search + Lucene.NET 重复条目
【发布时间】:2011-01-21 16:55:44
【问题描述】:

当我从现有数据库构建初始索引时,Lucene 中的条目类型被索引为 MuestraProxy, DynamicProxyGenAssembly2>

但是,当使用 NHibernate 添加新实体时,它会被索引为 Muestra, RALVet>},这会在 Lucene 中创建重复的条目,一个是真实类,另一个是一个作为它的代理。

我在构建索引时尝试使用 NHibernateUtil.GetClass(),但它仍然返回代理。

这是构建初始索引的代码:

private static void CreateIndex<T>()
    {
        var fullTextSession =
            NHibernate.Search.Search.CreateFullTextSession(BootStrapper.SessionFactory.OpenSession());
        using(var tran = fullTextSession.BeginTransaction())
        {
            var query = fullTextSession.CreateQuery(string.Concat("from ", typeof (T).Name));

            foreach (var doc in query.List())
            {
                fullTextSession.Index(doc);
            //  fullTextSession.Index(NHibernateUtil.GetClass(doc));
            }
            tran.Commit();
        }
        fullTextSession.Close();
    }

【问题讨论】:

    标签: .net lucene.net nhibernate.search


    【解决方案1】:

    编辑 2:好吧,我在 SO 上进行了另一次搜索,我认为您应该看看 this answer。我无法重现您的问题,所以要么这里有其他东西(您的配置或域模型?),要么我完全错过了重点。祝你好运!


    我将在这里尝试猜测原因(我不使用 C# 或 hql,所以如果它在这方面有什么东西,我可能会不走运)。我认为您看到的是因为您使用了.List() 方法的无类型版本。您应该尝试使用 .List&lt;T&gt;() 来指定您在列出实体时所期望的类型。

    我认为

    foreach (var doc in query.List&lt;T&gt;()) { fullTextSession.Index(doc); }

    应该可以解决问题。


    编辑:好的,显然它不适用于添加的&lt;T&gt;(显然代码 sn-p 吃了括号,所以如果你这样做了,请确保你复制粘贴了正确的版本)。

    FWIW,我们正在做的工作如下。我们使用 UnitOfWork 模式,因此 UnitOfWork 启动当前的 Nhibernate 配置和会话。我使用 Reflector 从 Vb.net 转到 c#

                DocumentBuilder db = SearchFactoryImpl.GetSearchFactory(UnitOfWork.Configuration).GetDocumentBuilder(typeof(T));
                IList<T> results = null;
                PropertyInfo pi = typeof(T).GetProperty("Id");
                // an internal method that pages the data from the DB, returning true while there's more to process
                while (this.InnerPageThrough<T>(ref results, pageNumber, itemsPerPage))
                {
                    IndexWriter iw = new IndexWriter(this.CheminIndexation + @"\" + typeof(T).Name, new StandardAnalyzer(), false);
                    iw.SetMaxMergeDocs(0x186a0);
                    iw.SetMergeFactor(0x3e8);
                    iw.SetMaxBufferedDocs(0x2710);
                    iw.SetUseCompoundFile(false);
                    using (Timer.Start("indexing + Conversions.ToString(results.Count) + " objects " + typeof(T).Name))
                    {
                        // Sorry, looks like crap through the translation
                        IEnumerator<T> VB$t_ref$L3;
                        try
                        {
                            VB$t_ref$L3 = results.GetEnumerator();
                            while (VB$t_ref$L3.MoveNext())
                            {
                                T Entity = VB$t_ref$L3.Current;
                                object EntityId = RuntimeHelpers.GetObjectValue(pi.GetValue(Entity, null));
                                iw.AddDocument(db.GetDocument(Entity, RuntimeHelpers.GetObjectValue(EntityId), typeof(T)));
                            }
                        }
                        finally
                        {
                            if (VB$t_ref$L3 != null)
                            {
                                VB$t_ref$L3.Dispose();
                            }
                        }
                    }
                    iw.Flush(true, true, true);
                    iw.Close();
                    UnitOfWork.CurrentSession.Clear();
                    pageNumber++;
                }
                if (Optimize)
                {
                    using (Timer.Start("optimising index"))
                    {
                        IndexWriter iw = new IndexWriter(this.CheminIndexation + @"\" + typeof(T).Name, new StandardAnalyzer(), false);
                        iw.Optimize();
                        iw.Close();
                    }
                }
            }
        }
    }
    

    我将尝试以更精简的方式重现您的问题

    【讨论】:

    • 谢谢 samy,不幸的是它不能解决问题。
    • Sammy,感谢您的努力,我真的很感激。我一直在尝试你的代码并调查这个问题。在配置中,我设置了一个拦截器(帮助我对实体进行数据绑定),它导致了问题。如果没有设置拦截器,我不会得到重复的条目。所以这似乎是问题所在。当我确信问题已经消失时,我会更新帖子。再次感谢。
    • 没问题,看到这是外部问题,而不是奇怪的错误,我松了一口气
    【解决方案2】:

    好的,我终于让它完美地工作了。

    问题是我使用拦截器在我的实体中注入 INotifyPropertyChanged 实现。移除拦截器使 NH.Search 和 Lucene.net 可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多