Exemplo n.º 1
0
        public void CombinedFilters()
        {
            try
            {
                CreateData();
                IFullTextSession s = Search.CreateFullTextSession(OpenSession());
                s.Transaction.Begin();
                BooleanQuery query = new BooleanQuery();
                query.Add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD);
                query.Add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD);
                query.Add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD);

                IFullTextQuery ftQuery = s.CreateFullTextQuery(query, typeof(Driver));
                ftQuery.EnableFullTextFilter("bestDriver");
                ftQuery.EnableFullTextFilter("security").SetParameter("Login", "andre");
                Assert.AreEqual(1, ftQuery.ResultSize, "Should filter to limit to Emmanuel");

                ftQuery = s.CreateFullTextQuery(query, typeof(Driver));
                ftQuery.EnableFullTextFilter("bestDriver");
                ftQuery.EnableFullTextFilter("security").SetParameter("login", "andre");
                ftQuery.DisableFullTextFilter("security");
                ftQuery.DisableFullTextFilter("bestDriver");
                Assert.AreEqual(3, ftQuery.ResultSize, "Should not filter anymore");

                s.Transaction.Commit();
                s.Close();
            }
            finally
            {
                DeleteData();
            }
        }
Exemplo n.º 2
0
        public void CustomBridges()
        {
            Cloud cloud = new Cloud();

            cloud.CustomFieldBridge  = ("This is divided by 2");
            cloud.CustomStringBridge = ("This is div by 4");
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

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

            tx = s.BeginTransaction();
            IFullTextSession session = Search.CreateFullTextSession(s);
            QueryParser      parser  = new QueryParser("id", new SimpleAnalyzer());

            Lucene.Net.Search.Query query = parser.Parse("CustomFieldBridge:This AND CustomStringBridge:This");
            IList result = session.CreateFullTextQuery(query).List();

            Assert.AreEqual(1, result.Count, "Properties not mapped");

            query  = parser.Parse("CustomFieldBridge:by AND CustomStringBridge:is");
            result = session.CreateFullTextQuery(query).List();
            Assert.AreEqual(0, result.Count, "Custom types not taken into account");

            s.Delete(s.Get(typeof(Cloud), cloud.Id));
            tx.Commit();
            s.Close();
        }
Exemplo n.º 3
0
        public void Cache()
        {
            CreateData();
            IFullTextSession s = Search.CreateFullTextSession(OpenSession());

            s.Transaction.Begin();
            BooleanQuery query = new BooleanQuery();

            query.Add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD);
            query.Add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD);
            query.Add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD);

            IFullTextQuery ftQuery = s.CreateFullTextQuery(query, typeof(Driver));

            Assert.AreEqual(3, ftQuery.ResultSize, "No filter should happen");

            ftQuery = s.CreateFullTextQuery(query, typeof(Driver));
            ftQuery.EnableFullTextFilter("cachetest");
            Assert.AreEqual(0, ftQuery.ResultSize, "Should filter out all");

            ftQuery = s.CreateFullTextQuery(query, typeof(Driver));
            ftQuery.EnableFullTextFilter("cachetest");
            try
            {
                int i = ftQuery.ResultSize;
            }
            catch (NotSupportedException)
            {
                Assert.Fail("Cache does not work");
            }

            s.Transaction.Commit();
            s.Close();
            DeleteData();
        }
Exemplo n.º 4
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();
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
        public void CanLookupEntityByValueOfEmbeddedDictionaryValue()
        {
            IFullTextSession session = Search.CreateFullTextSession(s);

            // PhraseQuery
            TermQuery query  = new TermQuery(new Term("orders.orderNumber", "ZERTYD"));
            IList     result = session.CreateFullTextQuery(query, typeof(Product)).List();

            Assert.AreEqual(1, result.Count, "collection of untokenized ignored");

            query  = new TermQuery(new Term("orders.orderNumber", "ACVBNM"));
            result = session.CreateFullTextQuery(query, typeof(Product)).List();
            Assert.AreEqual(1, result.Count, "collection of untokenized ignored");
        }
Exemplo n.º 8
0
        public void StraightFilters()
        {
            try
            {
                CreateData();
                IFullTextSession s = Search.CreateFullTextSession(OpenSession());
                s.Transaction.Begin();
                BooleanQuery query = new BooleanQuery();
                query.Add(new TermQuery(new Term("teacher", "andre")), BooleanClause.Occur.SHOULD);
                query.Add(new TermQuery(new Term("teacher", "max")), BooleanClause.Occur.SHOULD);
                query.Add(new TermQuery(new Term("teacher", "aaron")), BooleanClause.Occur.SHOULD);

                IFullTextQuery ftQuery = s.CreateFullTextQuery(query, typeof(Driver));
                ftQuery.EnableFullTextFilter("bestDriver");
                Lucene.Net.Search.Filter dateFilter = new RangeFilter("delivery", "2001", "2005", true, true);
                ftQuery.SetFilter(dateFilter);
                Assert.AreEqual(1, ftQuery.ResultSize, "Should select only liz");

                s.Transaction.Commit();
                s.Close();
            }
            finally
            {
                DeleteData();
            }
        }
        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();
        }
Exemplo n.º 10
0
        public IList <TaskMainDAO> Find(string textQuery)
        {
            if (textQuery == string.Empty)
            {
                return(null);
            }
            ISessionFactory applicationFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application);

            using (ISession session = applicationFactory.OpenSession())
                using (IFullTextSession fullTextSession = Search.CreateFullTextSession(session))
                    using (ITransaction transaction = fullTextSession.BeginTransaction())
                    {
                        try
                        {
                            IFullTextQuery      fullTextQuery = fullTextSession.CreateFullTextQuery <TaskMainDAO>(textQuery);
                            IList <TaskMainDAO> tasks         = fullTextQuery.List <TaskMainDAO>();
                            transaction.Commit();
                            return(tasks);
                        }
                        catch (Lucene.Net.QueryParsers.ParseException e)
                        {
                            //handle parsing failure. Display some indication like "Wrong search criteria"
                            transaction.Commit();
                            return(new TaskMainDAO[0]);
                        }
                    }
        }
Exemplo n.º 11
0
        public void DefaultAndNullBridges()
        {
            Cloud cloud = new Cloud();

            cloud.DateTime = null;
            cloud.Double1  = (null);
            cloud.Double2  = (2.1d);
            cloud.Int1     = (null);
            cloud.Int2     = (2);
            cloud.Float1   = (null);
            cloud.Float2   = (2.1f);
            cloud.Long1    = (null);
            cloud.Long2    = (2L);
            cloud.String1  = (null);
            cloud.Type     = (CloudType.Dog);
            cloud.Storm    = (false);
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

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

            tx = s.BeginTransaction();
            IFullTextSession session = Search.CreateFullTextSession(s);
            QueryParser      parser  = new QueryParser("id", new StandardAnalyzer());

            Lucene.Net.Search.Query query = parser.Parse("Double2:[2 TO 2.1] AND Float2:[2 TO 2.1] " +
                                                         "AND Int2:[2 TO 2.1] AND Long2:[2 TO 2.1] AND Type:\"Dog\" AND Storm:false");

            IList result = session.CreateFullTextQuery(query).List();

            Assert.AreEqual(1, result.Count, "find primitives and do not fail on null");

            query = parser.Parse("Double1:[2.1 TO 2.1] OR Float1:[2 TO 2.1] " +
                                 "OR Int1:[2 TO 2.1] OR Long1:[2 TO 2.1]");
            result = session.CreateFullTextQuery(query).List();
            Assert.AreEqual(0, result.Count, "null elements should not be stored"); //the query is dumb because restrictive

            query  = parser.Parse("Type:Dog");
            result = session.CreateFullTextQuery(query).SetProjection("Type").List();
            Assert.AreEqual(1, result.Count, "Enum projection works"); //the query is dumb because restrictive

            s.Delete(s.Get(typeof(Cloud), cloud.Id));
            tx.Commit();
            s.Close();
        }
Exemplo n.º 12
0
        public void StandardBehavior()
        {
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();
            Animal       a  = new Animal();

            a.Id   = 1;
            a.Name = "Elephant";
            s.Persist(a);
            a      = new Animal();
            a.Id   = 2;
            a.Name = "Bear";
            s.Persist(a);
            tx.Commit();

            s.Clear();

            tx     = s.BeginTransaction();
            a      = (Animal)s.Get(typeof(Animal), 1);
            a.Name = "Mouse";
            Furniture fur = new Furniture();

            fur.Color = "dark blue";
            s.Persist(fur);
            tx.Commit();

            s.Clear();

            tx = s.BeginTransaction();
            IFullTextSession fts = Search.CreateFullTextSession(s);
            var         version  = LuceneVersion.LUCENE_48;
            QueryParser parser   = new QueryParser(version, "id", new StopAnalyzer(version));

            IList results = fts.CreateFullTextQuery(parser.Parse("name:mouse OR name:bear")).List();

            Assert.AreEqual(2, results.Count, "Either double insert, single update, or query fails with shards");

            results = fts.CreateFullTextQuery(parser.Parse("name:mouse OR name:bear OR color:blue")).List();
            Assert.AreEqual(3, results.Count, "Mixing shared and non sharded properties fails");
            results = fts.CreateFullTextQuery(parser.Parse("name:mouse OR name:bear OR color:blue")).List();
            Assert.AreEqual(3, results.Count, "Mixing shared and non sharded properties fails with indexreader reuse");

            // cleanup
            s.Delete("from System.Object");
            tx.Commit();
            s.Close();
        }
Exemplo n.º 13
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 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();
        }
Exemplo n.º 15
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();
        }
Exemplo n.º 16
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();
        }
Exemplo n.º 17
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 CanLookupEntityByValueOfEmbeddedSetValues()
        {
            IFullTextSession session = Search.CreateFullTextSession(s);

            QueryParser parser = new MultiFieldQueryParser(Environment.LuceneVersion, new string[] { "name", "authors.name" }, new StandardAnalyzer(Environment.LuceneVersion));

            Lucene.Net.Search.Query query = parser.Parse("Hugo");
            IList result = session.CreateFullTextQuery(query).List();

            Assert.AreEqual(1, result.Count, "collection of embedded (set) ignored");
        }
Exemplo n.º 19
0
        public IList <T> QueryProperty(string field, string query)
        {
            using (var wu = SessionManager.WorkUnitFor(this, DbWorkUnitType.Read))
            {
                IFullTextSession fullTextSession = NHibernate.Search.Search.CreateFullTextSession(wu.Session);

                //QueryParser parser = new QueryParser(field, new StandardAnalyzer());
                //var luceneQuery = parser.Parse(query);
                var qq = fullTextSession.CreateFullTextQuery <T>(field, query);
                return(qq.List <T>());
            }
        }
Exemplo n.º 20
0
        private static void LuceneQueries()
        {
            using (IFullTextSession s = Search.CreateFullTextSession(sf.OpenSession())) {
                QueryParser qp = new QueryParser("id", new StopAnalyzer());

                IQuery NHQuery = s.CreateFullTextQuery(qp.Parse("Summary:series"), typeof(Book));

                IList result = NHQuery.List();

                Debug.Assert(result.Count == 2);
            }
        }
Exemplo n.º 21
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();
        }
Exemplo n.º 22
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();
        }
Exemplo n.º 23
0
 public IList <T> RawQuery <T>(string field, string query)
 {
     using (var wu = SessionManager.WorkUnitFor(this, DbWorkUnitType.Read))
     {
         IFullTextSession fullTextSession = NHibernate.Search.Search.CreateFullTextSession(wu.Session);
         //QueryParser parser = new QueryParser(field, new StandardAnalyzer());
         //var luceneQuery = parser.Parse(query);
         try
         {
             var qq = fullTextSession.CreateFullTextQuery <T>(field, query);
             return(qq.List <T>());
         }
         catch (ObjectNotFoundException ex)
         {
             return(new List <T>());
         }
     }
 }
Exemplo n.º 24
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();
        }
        public void CanLookupEntityByUpdatedValueInSet()
        {
            Product p = s.Get <Product>(p1.Id);

            p.Authors.Add(s.Get <Author>(a4.Id));
            tx.Commit();

            s.Clear();

            tx = s.BeginTransaction();

            IFullTextSession session = Search.CreateFullTextSession(s);
            QueryParser      parser  = new MultiFieldQueryParser(Environment.LuceneVersion, new string[] { "name", "authors.name" }, new StandardAnalyzer(Environment.LuceneVersion));
            Query            query   = parser.Parse("Proust");
            IList            result  = session.CreateFullTextQuery(query, typeof(Product)).List();

            // HSEARCH-56
            Assert.AreEqual(1, result.Count, "update of collection of embedded ignored");
        }
        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();
        }
Exemplo n.º 27
0
        public void DateTimeBridge()
        {
            Cloud cloud = new Cloud();

            DateTime date = new DateTime(2000, 12, 15, 3, 43, 2);

            cloud.DateTime            = (date); //5 millisecond
            cloud.DateTimeDay         = (date);
            cloud.DateTimeHour        = (date);
            cloud.DateTimeMillisecond = (date);
            cloud.DateTimeMinute      = (date);
            cloud.DateTimeMonth       = (date);
            cloud.DateTimeSecond      = (date);
            cloud.DateTimeYear        = (date);
            ISession     s  = OpenSession();
            ITransaction tx = s.BeginTransaction();

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

            tx = s.BeginTransaction();
            IFullTextSession session = Search.CreateFullTextSession(s);
            QueryParser      parser  = new QueryParser("id", new StandardAnalyzer());

            Lucene.Net.Search.Query query = parser.Parse("DateTime:[19900101 TO 20060101]"
                                                         + " AND DateTimeDay:[20001214 TO 2000121501]"
                                                         + " AND DateTimeMonth:[200012 TO 20001201]"
                                                         + " AND DateTimeYear:[2000 TO 200001]"
                                                         + " AND DateTimeHour:[20001214 TO 2000121503]"
                                                         + " AND DateTimeMinute:[20001214 TO 200012150343]"
                                                         + " AND DateTimeSecond:[20001214 TO 20001215034302]"
                                                         + " AND DateTimeMillisecond:[20001214 TO 20001215034302005]"
                                                         );
            IList result = session.CreateFullTextQuery(query).List();

            Assert.AreEqual(1, result.Count, "DateTime not found or not property truncated");

            s.Delete(s.Get(typeof(Cloud), cloud.Id));
            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();
        }
Exemplo n.º 29
0
        public static void Search()
        {
            HttpContext context        = HttpContext.Current;
            ISession    currentSession = context.Items[CurrentSessionKey] as ISession;

            if (currentSession == null)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;
            }

            IFullTextSession session = new FullTextSessionImpl(currentSession);

            if (!currentSession.IsOpen)
            {
                currentSession = sessionFactory.OpenSession();
                context.Items[CurrentSessionKey] = currentSession;

                if (currentSession.IsOpen)
                {
                    Debug.WriteLine("Session open");
                }
                else
                {
                    Debug.WriteLine("Session Closed");
                }
            }

            string   searchQuery = "Name:2";
            Analyzer std         = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);

            QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "Name", std);
            Query       query  = parser.Parse(searchQuery);

            IFullTextSession fullTextSession = NHibernate.Search.Search.CreateFullTextSession(NHibernateHelper.GetCurrentSession());
            IFullTextQuery   fullTextQuery   = fullTextSession.CreateFullTextQuery(query, typeof(Species));

            var employeeList = fullTextQuery.List();
        }
Exemplo n.º 30
0
        public void Fields()
        {
            Document doc = new Document(
                "Hibernate in Action", "Object/relational mapping with Hibernate", "blah blah blah");
            ISession     s  = this.OpenSession();
            ITransaction tx = s.BeginTransaction();

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

            s.Clear();

            IFullTextSession session = Search.CreateFullTextSession(s);

            tx = session.BeginTransaction();
            QueryParser p      = new QueryParser("id", new StandardAnalyzer());
            IList       result = session.CreateFullTextQuery(p.Parse("Abstract:Hibernate")).List();

            Assert.AreEqual(1, result.Count, "Query by field");
            s.Delete(result[0]);
            tx.Commit();
            s.Close();
        }