nhibernatelucene.netnhibernate.search

NHibernate Search N+1 Issue


I'm using NHibernate Search for NHibernate 3.0 GA.

I have this code in my product repository:

public IList<Product> Find(string term)
        {
            var productFields = new[] { "Name", "Description" };
            var importance = new Dictionary<String, float>(2) { { "Name", 4 }, { "Description", 1 } };
            var analyzer = new StandardAnalyzer();
            var parser = new MultiFieldQueryParser(
                productFields,
                analyzer,
                importance);

            var query = parser.Parse(term);

            var session = Search.CreateFullTextSession(NHibernateSession.Current);
            var tx = session.BeginTransaction();
            var fullTextQuery = session.CreateFullTextQuery(query);
            fullTextQuery.SetFirstResult(0).SetMaxResults(20);
            var results = fullTextQuery.List<Product>();
            tx.Commit();

            return results;
        }

In NH Profiler, I can see that a select statement is issued for every product found. What am I missing?

I found this thread from 2009 but presumably this bug has been fixed.

EDIT [06/06/2011]:

My property mappings as far as associations go are as follows:

mapping.HasManyToMany(c => c.Categories)
                .Table("CatalogHierarchy")
                .ParentKeyColumn("child_oid")
                .ChildKeyColumn("oid")
                .Cascade.None()
                .Inverse()
                .LazyLoad()
                .AsSet();

            mapping.HasMany(c => c.Variants)
                .Table("CatalogProducts")
                .Where("i_ClassType=2")
                .KeyColumn("ParentOID")
                .Cascade.SaveUpdate()
                .AsSet();

I don't really want to eager fetch all categories.


Solution

  • I solved this in the end. I realised I had not wrapped the whole thing into a transaction. Once I did, the N+1 issue was gone. Schoolboy error, but hey, you learn by your mistakes.