Beispiel #1
0
        private static void FillDb()
        {
            using (IFullTextSession s = Search.CreateFullTextSession(sf.OpenSession())) {
                using (ITransaction tx = s.BeginTransaction()){
                    Book b1 = new Book(1,
                                       "Eric Evans",
                                       "Domain-Driven Design: Tackling Complexity in the Heart of Software",
                                       "This book provides a broad framework for making design decisions and a technical vocabulary for discussing domain design. It is a synthesis of widely accepted best practices along with the author's own insights and experiences."
                                       );

                    s.Save(b1);

                    Book b2 = new Book(2,
                                       "Pierre Kuate",
                                       "NHibernate in Action",
                                       "In the classic style of Manning's 'In Action' series, NHibernate in Action introduces .NET developers to the NHibernate Object/Relational Mapping tool. As NHibernate is a port of Hibernate from Java to .NET.");
                    s.Save(b2);

                    Book b3 = new Book(3,
                                       "John Doe",
                                       "Foo book NHibernate",
                                       "Foo series book");
                    s.Save(b3);

                    s.Flush();
                    tx.Commit();
                }
            }
        }
        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();
        }
        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();
        }
        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();
        }
        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();
        }