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 LuceneObjectsProjectionWithList() { 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:Accounting"); IFullTextQuery hibQuery = s.CreateFullTextQuery(query, typeof(Employee)); hibQuery.SetProjection( "Id", "Lastname", "Dept", ProjectionConstants.THIS, ProjectionConstants.SCORE, ProjectionConstants.BOOST, ProjectionConstants.DOCUMENT, ProjectionConstants.ID, ProjectionConstants.DOCUMENT_ID); IList result = hibQuery.List(); Assert.IsNotNull(result); object[] projection = (Object[])result[0]; Assert.IsNotNull(projection); Assert.AreEqual(1001, projection[0], "id incorrect"); Assert.AreEqual("Jackson", projection[1], "last name incorrect"); Assert.AreEqual("Accounting", projection[2], "dept incorrect"); Assert.AreEqual("Jackson", ((Employee)projection[3]).Lastname, "THIS 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(1001, projection[7], "ID incorrect"); Assert.IsNotNull(projection[8], "Lucene internal doc id"); // Change the projection order and null one hibQuery.SetProjection( ProjectionConstants.DOCUMENT, ProjectionConstants.THIS, ProjectionConstants.SCORE, null, ProjectionConstants.ID, "Id", "Lastname", "Dept", ProjectionConstants.DOCUMENT_ID); result = hibQuery.List(); Assert.IsNotNull(result); projection = (object[])result[0]; Assert.IsNotNull(projection); Assert.IsTrue(projection[0] is Document, "DOCUMENT incorrect"); Assert.AreEqual(4, ((Document)projection[0]).GetFields().Count, "DOCUMENT size incorrect"); Assert.AreEqual(projection[1], s.Get <Employee>(projection[4]), "THIS incorrect"); //Assert.AreEqual(1.0F, projection[2], "SCORE incorrect"); Assert.IsNull(projection[3], "BOOST not removed"); Assert.AreEqual(1001, projection[4], "ID incorrect"); Assert.AreEqual(1001, projection[5], "id incorrect"); Assert.AreEqual("Jackson", projection[6], "last name incorrect"); Assert.AreEqual("Accounting", projection[7], "dept incorrect"); Assert.IsNotNull(projection[8], "Lucene internal doc id"); // cleanup s.Delete("from System.Object"); tx.Commit(); s.Close(); }