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 GenerateIndexes() { ISessionFactory applicationFactory = NhibernateSessionFactory.GetSessionFactory(NhibernateSessionFactory.SessionFactoryConfiguration.Application); using (ISession session = applicationFactory.OpenSession()) using (IFullTextSession fullTextSession = Search.CreateFullTextSession(session)) { using (ITransaction transaction = fullTextSession.BeginTransaction()) { fullTextSession.PurgeAll(typeof(TaskMainDAO)); transaction.Commit(); } using (ITransaction transaction = fullTextSession.BeginTransaction()) { foreach (object entity in fullTextSession.CreateCriteria(typeof(TaskMainDAO)).List()) { fullTextSession.Index(entity); } transaction.Commit(); } } }
private static void Main(string[] args) { XmlConfigurator.Configure(new FileInfo("nhprof.log4net.config")); Configuration cfg = new Configuration() .Configure("nhibernate.cfg.xml"); cfg.SetProperty("hibernate.search.default.directory_provider", typeof(FSDirectoryProvider).AssemblyQualifiedName); cfg.SetProperty(Environment.AnalyzerClass, typeof(StopAnalyzer).AssemblyQualifiedName); cfg.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener()); cfg.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener()); cfg.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener()); using (new ConsoleColorer("nhibernate")) new SchemaExport(cfg).Execute(true, true, false, true); ISessionFactory factory = cfg.BuildSessionFactory(); using (IFullTextSession s = Search .CreateFullTextSession(factory.OpenSession())) using (ITransaction tx = s.BeginTransaction()) { s.PurgeAll(typeof(Employee)); s.PurgeAll(typeof(Salary)); var salary = new Salary { Name = "MinPay", HourlyRate = 22m }; var emp = new Employee { Name = "ayende", Salary = salary }; s.Save(salary); s.Save(emp); tx.Commit(); } Thread.Sleep(1500); Console.Clear(); using (IFullTextSession s = Search.CreateFullTextSession(factory.OpenSession())) using (ITransaction tx = s.BeginTransaction()) { var employees = s.CreateFullTextQuery <Employee>("Name", "a*") .List <Employee>(); foreach (Employee employee in employees) { Console.WriteLine("Employee: " + employee.Name); Console.WriteLine("Salary: {0} - {1:C}", employee.Salary.Name, employee.Salary.HourlyRate); } var salaries = s.CreateFullTextQuery <Salary>("HourlyRate:[20 TO 25]") .List <Salary>(); foreach (var salary in salaries) { Console.WriteLine("Salaray: {0} - {1:C}", salary.Name, salary.HourlyRate); } tx.Commit(); } }