Пример #1
0
        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();
        }
Пример #2
0
        public void LuceneObjectsProjectionWithList()
        {
            IFullTextSession s = Search.CreateFullTextSession(this.OpenSession());

            this.PrepEmployeeIndex(s);

            s.Clear();
            ITransaction tx     = s.BeginTransaction();
            QueryParser  parser = new QueryParser(LuceneVersion.LUCENE_48, "Dept", new StandardAnalyzer(LuceneVersion.LUCENE_48));

            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]).Fields.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]).Fields.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();
        }