public void LuceneObjectsProjectionWithIterate() { IFullTextSession s = Search.CreateFullTextSession(this.OpenSession()); this.PrepEmployeeIndex(s); s.Clear(); ITransaction tx = s.BeginTransaction(); QueryParser parser = new QueryParser(NHibernate.Search.Environment.LuceneVersion, "Dept", new StandardAnalyzer(NHibernate.Search.Environment.LuceneVersion)); Query query = parser.Parse("Dept:ITech"); IFullTextQuery hibQuery = s.CreateFullTextQuery(query, typeof(Employee)); hibQuery.SetProjection( "Id", "Lastname", "Dept", ProjectionConstants.THIS, ProjectionConstants.SCORE, ProjectionConstants.BOOST, ProjectionConstants.DOCUMENT, ProjectionConstants.ID); int counter = 0; foreach (object[] projection in hibQuery.Enumerable()) { Assert.IsNotNull(projection); counter++; Assert.AreEqual("ITech", projection[2], "dept incorrect"); Assert.AreEqual(projection[3], s.Get <Employee>(projection[0]), "THIS incorrect"); Assert.AreEqual(1.0F, projection[4], "SCORE incorrect"); Assert.AreEqual(1.0F, projection[5], "BOOST incorrect"); Assert.IsTrue(projection[6] is Document, "DOCUMENT incorrect"); Assert.AreEqual(4, ((Document)projection[6]).GetFields().Count, "DOCUMENT size incorrect"); } Assert.AreEqual(4, counter, "incorrect number of results returned"); // cleanup s.Delete("from System.Object"); tx.Commit(); s.Close(); }
public void ClassBridgesWithProjection() { ISession s = this.OpenSession(); ITransaction tx = s.BeginTransaction(); s.Save(this.getDepts1()); s.Save(this.getDepts2()); s.Save(this.getDepts3()); s.Save(this.getDepts4()); s.Flush(); tx.Commit(); tx = s.BeginTransaction(); IFullTextSession session = Search.CreateFullTextSession(s); // The equipment field is the manufacturer field in the // Departments entity after being massaged by passing it // through the EquipmentType class. This field is in // the Lucene document but not in the Department entity itself. QueryParser parser = new QueryParser("equipment", new SimpleAnalyzer()); // Check the second ClassBridge annotation Query query = parser.Parse("equiptype:Cisco"); IFullTextQuery hibQuery = session.CreateFullTextQuery(query, typeof(Departments)); hibQuery.SetProjection(ProjectionConstants.THIS, ProjectionConstants.DOCUMENT); IEnumerator projections = hibQuery.Enumerable().GetEnumerator(); Assert.IsNotNull(projections); projections.MoveNext(); object[] projection = (object[])projections.Current; Assert.AreEqual(typeof(Departments), projection[0].GetType(), "DOCUMENT incorrect"); // Note: This assertion fails when run with other tests because the id is assigned by the database, and previous tests have already used this value //Assert.AreEqual(1, ((Departments)projection[0]).Id, "id incorrect"); Assert.IsTrue(projection[1] is Document, "DOCUMENT incorrect"); Assert.AreEqual(8, ((Document)projection[1]).GetFields().Count, "DOCUMENT size incorrect"); Assert.IsNotNull(((Document)projection[1]).GetField("equiptype"), "equiptype is null"); Assert.AreEqual( "Cisco", ((Document)projection[1]).GetField("equiptype").StringValue(), "equiptype incorrect"); Assert.IsNotNull(((Document)projection[1]).GetField("branchnetwork"), "branchnetwork is null"); Assert.AreEqual( "Salt Lake City 1A", ((Document)projection[1]).GetField("branchnetwork").StringValue(), "branchnetwork incorrect"); projections.MoveNext(); projection = (object[])projections.Current; Assert.AreEqual(typeof(Departments), projection[0].GetType(), "DOCUMENT incorrect"); // NB This assertion causes the test to break when run with other tests - some leakage? //Assert.AreEqual(4, ((Departments)projection[0]).Id, "id incorrect"); Assert.IsTrue(projection[1] is Document, "DOCUMENT incorrect"); Assert.AreEqual(8, ((Document)projection[1]).GetFields().Count, "DOCUMENT size incorrect"); Assert.IsNotNull(((Document)projection[1]).GetField("equiptype"), "equiptype is null"); Assert.AreEqual( "Cisco", ((Document)projection[1]).GetField("equiptype").StringValue(), "equiptype incorrect"); Assert.IsNotNull(((Document)projection[1]).GetField("branchnetwork"), "branchnetwork is null"); Assert.AreEqual( "St. George 1D", ((Document)projection[1]).GetField("branchnetwork").StringValue(), "branchnetwork incorrect"); Assert.AreEqual(false, projections.MoveNext(), "incorrect result count returned"); //cleanup foreach (object element in s.CreateQuery("from " + typeof(Departments).FullName).List()) { s.Delete(element); } tx.Commit(); s.Close(); }