Ejemplo n.º 1
0
        public void List()
        {
            IFullTextSession s     = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            clock = new Clock(2, "Festina");
            s.Save(clock);
            Book book =
                new Book(1, "La chute de la petite reine a travers les yeux de Festina",
                         "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
            s.Save(book);
            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "title", new StopAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query = parser.Parse("Summary:noword");
            IQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            IList  result   = hibQuery.List();

            Assert.AreEqual(0, result.Count);

            query    = parser.Parse("Summary:Festina Or Brand:Seiko");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            result   = hibQuery.List();
            Assert.AreEqual(2, result.Count, "Query with explicit class filter");

            query    = parser.Parse("Summary:Festina Or Brand:Seiko");
            hibQuery = s.CreateFullTextQuery(query);
            result   = hibQuery.List();
            Assert.AreEqual(2, result.Count, "Query with no class filter");
            foreach (Object element in result)
            {
                Assert.IsTrue(NHibernateUtil.IsInitialized(element));
                s.Delete(element);
            }
            s.Flush();
            tx.Commit();

            tx       = s.BeginTransaction();
            query    = parser.Parse("Summary:Festina Or Brand:Seiko");
            hibQuery = s.CreateFullTextQuery(query);
            result   = hibQuery.List();
            Assert.AreEqual(0, result.Count, "Query with delete objects");

            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 2
0
        public void MultipleEntityPerIndex()
        {
            IFullTextSession s     = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            Book book =
                new Book(1, "La chute de la petite reine a travers les yeux de Festina",
                         "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            AlternateBook alternateBook =
                new AlternateBook(1, "La chute de la petite reine a travers les yeux de Festina");

            s.Save(alternateBook);
            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "Title", new StopAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query = parser.Parse("Summary:Festina");
            IQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            IList  result   = hibQuery.List();

            Assert.AreEqual(1, result.Count, "Query with explicit class filter");

            query    = parser.Parse("Summary:Festina");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            IEnumerator it = hibQuery.Enumerable().GetEnumerator();

            Assert.IsTrue(it.MoveNext());
            Assert.IsNotNull(it.Current);
            Assert.IsFalse(it.MoveNext());

            query    = parser.Parse("Summary:Festina OR Brand:seiko");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            hibQuery.SetMaxResults(2);
            result = hibQuery.List();
            Assert.AreEqual(2, result.Count, "Query with explicit class filter and limit");

            query    = parser.Parse("Summary:Festina");
            hibQuery = s.CreateFullTextQuery(query);
            result   = hibQuery.List();
            Assert.AreEqual(2, result.Count, "Query with no class filter");
            foreach (Object element in result)
            {
                Assert.IsTrue(NHibernateUtil.IsInitialized(element));
                s.Delete(element);
            }
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 3
0
        public void Iterator()
        {
            IFullTextSession s     = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            clock = new Clock(2, "Festina");
            s.Save(clock);
            Book book =
                new Book(1, "La chute de la petite reine a travers les yeux de Festina",
                         "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
            s.Save(book);
            tx.Commit(); //post Commit events for lucene
            s.Clear();
            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "title", new StopAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query = parser.Parse("Summary:noword");
            IQuery      hibQuery          = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            IEnumerator result            = hibQuery.Enumerable().GetEnumerator();

            Assert.IsFalse(result.MoveNext());

            query    = parser.Parse("Summary:Festina Or Brand:Seiko");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            result   = hibQuery.Enumerable().GetEnumerator();
            int index = 0;

            while (result.MoveNext())
            {
                index++;
                s.Delete(result.Current);
            }
            Assert.AreEqual(2, index);

            tx.Commit();

            tx       = s.BeginTransaction();
            query    = parser.Parse("Summary:Festina Or Brand:Seiko");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            result   = hibQuery.Enumerable().GetEnumerator();

            Assert.IsFalse(result.MoveNext());
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
        public void ResultTransformToDelimString()
        {
            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.ID);
            hibQuery.SetResultTransformer(new ProjectionToDelimStringResultTransformer());

            IList result = hibQuery.List();

            Assert.IsTrue(((string)result[0]).StartsWith("1000, Griffin, ITech"), "incorrect transformation");
            Assert.IsTrue(((string)result[1]).StartsWith("1002, Jimenez, ITech"), "incorrect transformation");

            // cleanup
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="s"></param>
        private void DeleteTestBooks(IFullTextSession s)
        {
            ITransaction tx = s.BeginTransaction();

            s.Delete("from System.Object");
            tx.Commit();
            s.Clear();
        }
        public void DoubleInsert()
        {
            Address address = new Address();

            address.Address1      = "TEST1";
            address.Address2      = "N/A";
            address.Town          = "TEST TOWN";
            address.County        = "TEST COUNTY";
            address.Country       = "UK";
            address.Postcode      = "XXXXXXX";
            address.Active        = true;
            address.CreatedOn     = DateTime.Now;
            address.LastUpdatedOn = DateTime.Now;

            Phone phone = new Phone();

            phone.Number        = "01273234122";
            phone.Type          = "HOME";
            phone.CreatedOn     = DateTime.Now;
            phone.LastUpdatedOn = DateTime.Now;

            PersonalContact contact = new PersonalContact();

            contact.Firstname   = "Amin";
            contact.Surname     = "Mohammed-Coleman";
            contact.Email       = "*****@*****.**";
            contact.DateOfBirth = DateTime.Now;

            // contact.NotifyBirthDay( false );
            contact.CreatedOn     = DateTime.Now;
            contact.LastUpdatedOn = DateTime.Now;
            contact.Notes         = "TEST";
            contact.AddAddressToContact(address);
            contact.AddPhoneToContact(phone);

            IFullTextSession s = Search.CreateFullTextSession(OpenSession());
            var tx             = s.BeginTransaction();

            s.Save(contact);
            tx.Commit();

            s.Close();

            s  = Search.CreateFullTextSession(OpenSession());
            tx = s.BeginTransaction();
            Term      term      = new Term("county", "county");
            TermQuery termQuery = new TermQuery(term);
            IList     results   = s.CreateFullTextQuery(termQuery).List();

            Assert.AreEqual(1, results.Count);
            s.Flush();
            s.Clear();

            s.Delete("from System.Object");
            tx.Commit();

            s.Close();
        }
Ejemplo n.º 7
0
        public void FirstMax()
        {
            ISession sess = OpenSession();

            Assert.AreEqual(0, sess.CreateCriteria(typeof(Clock)).List().Count);

            IFullTextSession s     = Search.CreateFullTextSession(sess);
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            clock = new Clock(2, "Festina");
            s.Save(clock);
            Book book =
                new Book(1, "La chute de la petite reine a travers les yeux de Festina",
                         "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
            s.Save(book);
            tx.Commit();
            s.Clear();
            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "title", new StopAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query = parser.Parse("Summary:Festina Or Brand:Seiko");
            IQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));

            hibQuery.SetFirstResult(1);
            IList result = hibQuery.List();

            Assert.AreEqual(1, result.Count, "first result no max result");

            hibQuery.SetFirstResult(0);
            hibQuery.SetMaxResults(1);
            result = hibQuery.List();
            Assert.AreEqual(1, result.Count, "max result set");

            hibQuery.SetFirstResult(0);
            hibQuery.SetMaxResults(3);
            result = hibQuery.List();
            Assert.AreEqual(2, result.Count, "max result out of limit");

            hibQuery.SetFirstResult(2);
            hibQuery.SetMaxResults(3);
            result = hibQuery.List();
            Assert.AreEqual(0, result.Count, "first result out of limit");

            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 8
0
        public void PurgeAll()
        {
            IFullTextSession s     = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            clock = new Clock(2, "Festina");
            s.Save(clock);
            clock = new Clock(3, "Longine");
            s.Save(clock);
            clock = new Clock(4, "Rolex");
            s.Save(clock);
            Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
            s.Save(book);
            tx.Commit();
            s.Clear();

            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "Brand", new StopAnalyzer(Environment.LuceneVersion));

            tx = s.BeginTransaction();
            s.PurgeAll(typeof(Clock));

            tx.Commit();

            tx = s.BeginTransaction();

            Lucene.Net.Search.Query query = parser.Parse("Brand:Festina or Brand:Seiko or Brand:Longine or Brand:Rolex");
            IQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            IList  results  = hibQuery.List();

            Assert.AreEqual(0, results.Count, "class not completely purged");

            query    = parser.Parse("Summary:Festina or Summary:gloire");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            results  = hibQuery.List();
            Assert.AreEqual(2, results.Count, "incorrect class purged");

            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
        public void ProxyObjectInheritance()
        {
            // This will test subclassed proxy objects and make sure that they are index correctly.
            IFullTextSession s  = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx = s.BeginTransaction();

            // Create an object in db
            Mammal temp = new Mammal();

            temp.NumberOfLegs = (4);
            temp.Name         = ("Some Mammal Name Here");
            s.Save(temp);

            tx.Commit(); //post commit events for lucene

            // Clear object from cache by clearing session
            s.Clear();

            // This should return a proxied object
            Mammal mammal = s.Load <Mammal>(temp.Id);

            // Index the proxied object
            s.Index(mammal);

            // Build an index reader
            var reader = s.SearchFactory.ReaderProvider.OpenReader(s.SearchFactory.GetDirectoryProviders(typeof(Mammal)));

            // Get the last document indexed
            var Document = reader.Document(reader.MaxDoc() - 1);

            // get the class name field from the document
            string classTypeThatWasIndex = Document.Get(NHibernate.Search.Engine.DocumentBuilder.CLASS_FIELDNAME);

            // get the expected lucene type name (this should be equivilent to
            // the static method of NHibernate.Search.Util.TypeHelper.LuceneTypeName
            string expectedLuceneTypeName = typeof(Mammal).FullName + ", " + typeof(Mammal).Assembly.GetName().Name;

            Assert.AreEqual(expectedLuceneTypeName, classTypeThatWasIndex);

            // Tidyup
            tx = s.BeginTransaction();
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
        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();
        }
Ejemplo n.º 11
0
        public void Inheritance()
        {
            IFullTextSession s  = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx = s.BeginTransaction();
            Animal           a  = new Animal();

            a.Name = ("Shark Jr");
            s.Save(a);
            Mammal m = new Mammal();

            m.NumberOfLegs = (4);
            m.Name         = ("Elephant Jr");
            s.Save(m);
            tx.Commit(); //post commit events for lucene
            s.Clear();
            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "Name", new StopAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query = parser.Parse("Elephant");
            IQuery hibQuery = s.CreateFullTextQuery(query, typeof(Mammal));
            IList  result   = hibQuery.List();

            Assert.IsNotEmpty(result);
            Assert.AreEqual(1, result.Count, "Query subclass by superclass attribute");

            query    = parser.Parse("NumberOfLegs:[4 TO 4]");
            hibQuery = s.CreateFullTextQuery(query, typeof(Animal), typeof(Mammal));
            result   = hibQuery.List();
            Assert.IsNotEmpty(result);
            Assert.AreEqual(1, result.Count, "Query subclass by subclass attribute");

            query    = parser.Parse("Jr");
            hibQuery = s.CreateFullTextQuery(query, typeof(Animal));
            result   = hibQuery.List();
            Assert.IsNotEmpty(result);
            Assert.AreEqual(2, result.Count, "Query filtering on superclass return mapped subclasses");
            foreach (Object managedEntity in result)
            {
                s.Delete(managedEntity);
            }
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 12
0
        public void TestScopedAnalyzers()
        {
            MyEntity en = new MyEntity();

            en.Entity    = "Entity";
            en.Field     = "Field";
            en.Property  = "Property";
            en.Component = new MyComponent();
            en.Component.ComponentProperty = "component property";

            IFullTextSession s  = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx = s.BeginTransaction();

            s.Save(en);
            s.Flush();
            tx.Commit();

            tx = s.BeginTransaction();

            QueryParser parser = new QueryParser("id", new StandardAnalyzer());

            Lucene.Net.Search.Query luceneQuery = parser.Parse("entity:alarm");
            IFullTextQuery          query       = s.CreateFullTextQuery(luceneQuery, typeof(MyEntity));

            Assert.AreEqual(1, query.ResultSize, "Entity query");

            luceneQuery = parser.Parse("property:cat");
            query       = s.CreateFullTextQuery(luceneQuery, typeof(MyEntity));
            Assert.AreEqual(1, query.ResultSize, "Property query");

            luceneQuery = parser.Parse("field:energy");
            query       = s.CreateFullTextQuery(luceneQuery, typeof(MyEntity));
            Assert.AreEqual(1, query.ResultSize, "Field query");

            luceneQuery = parser.Parse("component.componentProperty:noise");
            query       = s.CreateFullTextQuery(luceneQuery);
            Assert.AreEqual(1, query.ResultSize, "Component query");

            s.Delete(query.UniqueResult());
            tx.Commit();

            s.Close();
        }
Ejemplo n.º 13
0
        public void UsingCriteriaApi()
        {
            IFullTextSession s     = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            tx.Commit();

            IList list = s.CreateFullTextQuery <Clock>("Brand:seiko")
                         .SetCriteriaQuery(s.CreateCriteria(typeof(Clock)).Add(Restrictions.IdEq(1)))
                         .List();

            Assert.AreEqual(1, list.Count, "should get result back from query");

            s.Delete(clock);
            s.Flush();
            s.Close();
        }
Ejemplo n.º 14
0
        public void Purge()
        {
            IFullTextSession s     = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx    = s.BeginTransaction();
            Clock            clock = new Clock(1, "Seiko");

            s.Save(clock);
            clock = new Clock(2, "Festina");
            s.Save(clock);
            Book book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            book = new Book(2, "La gloire de mon père", "Les deboires de mon père en vélo");
            s.Save(book);
            tx.Commit();
            s.Clear();

            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser("Brand", new StopAnalyzer());

            Lucene.Net.Search.Query query = parser.Parse("Brand:Seiko");
            IQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            IList  results  = hibQuery.List();

            Assert.AreEqual(1, results.Count, "incorrect test record");
            Assert.AreEqual(1, ((Clock)results[0]).Id, "incorrect test record");

            s.Purge(typeof(Clock), ((Clock)results[0]).Id);

            tx.Commit();

            tx = s.BeginTransaction();

            query    = parser.Parse("Brand:Festina or Brand:Seiko");
            hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book));
            results  = hibQuery.List();
            Assert.AreEqual(1, results.Count, "incorrect test record count");
            Assert.AreEqual(2, ((Clock)results[0]).Id, "incorrect test record");

            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
        public void ResultTransformMap()
        {
            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);
            hibQuery.SetResultTransformer(new ProjectionToMapResultTransformer());

            IList transforms = hibQuery.List();
            Dictionary <string, object> map = (Dictionary <string, object>)transforms[1];

            Assert.AreEqual("ITech", map["Dept"], "incorrect transformation");
            Assert.AreEqual(1002, map["Id"], "incorrect transformation");
            Assert.IsTrue(map[ProjectionConstants.DOCUMENT] is Document, "incorrect transformation");
            Assert.AreEqual(
                "1002",
                ((Document)map[ProjectionConstants.DOCUMENT]).GetField("Id").StringValue,
                "incorrect transformation");

            // cleanup
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 16
0
        public void Criteria()
        {
            IFullTextSession s    = Search.CreateFullTextSession(OpenSession());
            ITransaction     tx   = s.BeginTransaction();
            Book             book = new Book(1, "La chute de la petite reine a travers les yeux de Festina", "La chute de la petite reine a travers les yeux de Festina, blahblah");

            s.Save(book);
            Author emmanuel = new Author();

            emmanuel.Name = "Emmanuel";
            s.Save(emmanuel);
            book.Authors.Add(emmanuel);
            tx.Commit();
            s.Clear();

            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "Title", new StopAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query    = parser.Parse("Summary:Festina");
            IFullTextQuery          hibQuery = s.CreateFullTextQuery(query, typeof(Book));
            IList result = hibQuery.List();

            Assert.NotNull(result);
            Assert.AreEqual(1, result.Count, "Query with no explicit criteria");
            book = (Book)result[0];
            //Assert.IsFalse(NHibernate.IsInitialized(book.Authors), "Association should not be initialized");

            result = s.CreateFullTextQuery(query).SetCriteriaQuery(s.CreateCriteria(typeof(Book)).SetFetchMode("Authors", FetchMode.Join)).List();
            Assert.NotNull(result);
            Assert.AreEqual(1, result.Count, "Query with no explicit criteria");
            book = (Book)result[0];
            //Assert.IsTrue(NHibernate.IsInitialized(book.Authors), "Association should be initialized");
            Assert.AreEqual(1, book.Authors.Count);

            // cleanup
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Ejemplo n.º 17
0
        public void ObjectNotFound()
        {
            ISession     sess = OpenSession();
            ITransaction tx   = sess.BeginTransaction();

            Author author = new Author();

            author.Name = "Moo Cow";
            sess.Persist(author);

            tx.Commit();
            sess.Clear();
            IDbCommand statement = sess.Connection.CreateCommand();

            statement.CommandText = "DELETE FROM Author";
            statement.ExecuteNonQuery();

            IFullTextSession s = Search.CreateFullTextSession(sess);

            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(LuceneVersion.LUCENE_48, "title", new KeywordAnalyzer());

            Lucene.Net.Search.Query query    = parser.Parse("name:moo");
            IFullTextQuery          hibQuery = s.CreateFullTextQuery(query, typeof(Author), typeof(Music));
            IList result = hibQuery.List();

            Assert.AreEqual(0, result.Count, "Should have returned no author");

            foreach (object o in result)
            {
                s.Delete(o);
            }

            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();
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="s"></param>
 private void DeleteTestBooks(IFullTextSession s)
 {
     ITransaction tx = s.BeginTransaction();
     s.Delete("from System.Object");
     tx.Commit();
     s.Clear();
 }
Ejemplo n.º 20
0
        public void EagerCollectionLoad()
        {
            ISession     sess = this.OpenSession();
            ITransaction tx   = sess.BeginTransaction();

            Music music = new Music();

            music.Title = "Moo Goes The Cow";
            Author author = new Author();

            author.Name = "Moo Cow";
            music.Authors.Add(author);
            sess.Persist(author);

            author      = new Author();
            author.Name = "Another Moo Cow";
            music.Authors.Add(author);
            sess.Persist(author);

            author      = new Author();
            author.Name = "A Third Moo Cow";
            music.Authors.Add(author);
            sess.Persist(author);

            author      = new Author();
            author.Name = "Random Moo Cow";
            music.Authors.Add(author);
            sess.Persist(author);
            sess.Save(music);

            Music music2 = new Music();

            music2.Title = "The Cow Goes Moo";
            author       = new Author();
            author.Name  = "Moo Cow The First";
            music2.Authors.Add(author);
            sess.Persist(author);

            author      = new Author();
            author.Name = "Moo Cow The Second";
            music2.Authors.Add(author);
            sess.Persist(author);

            author      = new Author();
            author.Name = "Moo Cow The Third";
            music2.Authors.Add(author);
            sess.Persist(author);

            author      = new Author();
            author.Name = "Moo Cow The Fourth";
            music2.Authors.Add(author);
            sess.Persist(author);
            sess.Save(music2);
            tx.Commit();
            sess.Clear();

            IFullTextSession s = Search.CreateFullTextSession(sess);

            tx = s.BeginTransaction();
            QueryParser parser = new QueryParser(Environment.LuceneVersion, "title", new KeywordAnalyzer());

            Lucene.Net.Search.Query query    = parser.Parse("title:moo");
            IFullTextQuery          hibQuery = s.CreateFullTextQuery(query, typeof(Music));
            IList result = hibQuery.List();

            Assert.AreEqual(2, result.Count, "Should have returned 2 Music");
            music = (Music)result[0];
            Assert.AreEqual(4, music.Authors.Count, "Music 1 should have 4 authors");
            music = (Music)result[1];
            Assert.AreEqual(4, music.Authors.Count, "Music 2 should have 4 authors");

            // cleanup
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }