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("Title", new StopAnalyzer()); 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(); }
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(); }
public void Cache() { 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)); 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(); } finally { DeleteData(); } }
public void StraightFilters() { 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(); DeleteData(); }
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 ParameterizedFilter() { 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("security").SetParameter("Login", "andre"); Assert.AreEqual(1, ftQuery.ResultSize, "Should filter to limit to Emmanuel"); s.Transaction.Commit(); s.Close(); } finally { DeleteData(); } }
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(); }
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(); }
public void ClassBridge() { ISession s = this.OpenSession(); ITransaction tx = s.BeginTransaction(); s.Save(this.getDept1()); s.Save(this.getDept2()); s.Save(this.getDept3()); s.Flush(); tx.Commit(); tx = s.BeginTransaction(); IFullTextSession session = Search.CreateFullTextSession(s); // The branchnetwork field is the concatenation of both // the branch field and the network field of the Department // class. This is in the Lucene document but not in the // Department entity itself. QueryParser parser = new QueryParser("branchnetwork", new SimpleAnalyzer()); Query query = parser.Parse("branchnetwork:layton 2B"); IFullTextQuery hibQuery = session.CreateFullTextQuery(query, typeof(Department)); IList result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual("2B", ((Department)result[0]).Network, "incorrect entity returned, wrong network"); Assert.AreEqual("Layton", ((Department)result[0]).Branch, "incorrect entity returned, wrong branch"); Assert.AreEqual(1, result.Count, "incorrect number of results returned"); // Partial match. query = parser.Parse("branchnetwork:3c"); hibQuery = session.CreateFullTextQuery(query, typeof(Department)); result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual("3C", ((Department)result[0]).Network, "incorrect entity returned, wrong network"); Assert.AreEqual("West Valley", ((Department)result[0]).Branch, "incorrect entity returned, wrong branch"); Assert.AreEqual(1, result.Count, "incorrect number of results returned"); // No data cross-ups . query = parser.Parse("branchnetwork:Kent Lewin"); hibQuery = session.CreateFullTextQuery(query, typeof(Department)); result = hibQuery.List(); Assert.IsNotNull(result); Assert.IsTrue(result.Count == 0, "problem with field cross-ups"); // Non-ClassBridge field. parser = new QueryParser("BranchHead", new SimpleAnalyzer()); query = parser.Parse("BranchHead:Kent Lewin"); hibQuery = session.CreateFullTextQuery(query, typeof(Department)); result = hibQuery.List(); Assert.IsNotNull(result); Assert.IsTrue(result.Count == 1, "incorrect entity returned, wrong branch head"); Assert.AreEqual("Kent Lewin", ((Department)result[0]).BranchHead, "incorrect entity returned"); // Cleanup foreach (object element in s.CreateQuery("from " + typeof(Department).FullName).List()) { s.Delete(element); } tx.Commit(); s.Close(); }
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(); }
public void ClassBridges() { 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)); IList result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual(2, result.Count, "incorrect number of results returned"); foreach (Departments d in result) { Assert.AreEqual("C", d.Manufacturer, "incorrect manufacturer"); } // No data cross-ups. query = parser.Parse("branchnetwork:Kent Lewin"); hibQuery = session.CreateFullTextQuery(query, typeof(Departments)); result = hibQuery.List(); Assert.IsNotNull(result); Assert.IsTrue(result.Count == 0, "problem with field cross-ups"); // Non-ClassBridge field. parser = new QueryParser("BranchHead", new SimpleAnalyzer()); query = parser.Parse("BranchHead:Kent Lewin"); hibQuery = session.CreateFullTextQuery(query, typeof(Departments)); result = hibQuery.List(); Assert.IsNotNull(result); Assert.IsTrue(result.Count == 1, "incorrect entity returned, wrong branch head"); Assert.AreEqual("Kent Lewin", ((Departments)result[0]).BranchHead, "incorrect entity returned"); // Check other ClassBridge annotation. parser = new QueryParser("branchnetwork", new SimpleAnalyzer()); query = parser.Parse("branchnetwork:st. george 1D"); hibQuery = session.CreateFullTextQuery(query, typeof(Departments)); result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual("1D", ((Departments)result[0]).Network, "incorrect entity returned, wrong network"); Assert.AreEqual("St. George", ((Departments)result[0]).Branch, "incorrect entity returned, wrong branch"); Assert.AreEqual(1, result.Count, "incorrect number of results returned"); // Cleanup foreach (object element in s.CreateQuery("from " + typeof(Departments).FullName).List()) { s.Delete(element); } tx.Commit(); s.Close(); }
public void TestList() { IFullTextSession s = Search.CreateFullTextSession(OpenSession()); CreateTestBooks(s); ITransaction tx = s.BeginTransaction(); QueryParser parser = new QueryParser(Environment.LuceneVersion, "Summary", new StopAnalyzer(Environment.LuceneVersion)); Lucene.Net.Search.Query query = parser.Parse("Summary:lucene"); IFullTextQuery hibQuery = s.CreateFullTextQuery(query, typeof(Book)); IList result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual(3, result.Count, "Wrong number of test results."); int id = 1; // Ignoring the default sort order as this does not appear to be the same between Lucene.Net and Lucene // This also beats against KW's change to queue processing order - see /* * // Make sure that the order is according to in which order the books got inserted into the index. * foreach (Book b in result) * { * Assert.AreEqual(id, b.Id, "Expected another id"); * id++; * } */ // now the same query, but with a lucene sort specified. query = parser.Parse("Summary:lucene"); hibQuery = s.CreateFullTextQuery(query, typeof(Book)); Sort sort = new Sort(new SortField("Id", SortField.INT, true)); hibQuery.SetSort(sort); result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual(3, result.Count, "Wrong number of test results."); id = 3; foreach (Book b in result) { Assert.AreEqual(id, b.Id, "Expected another id"); id--; } // order by summary query = parser.Parse("Summary:lucene OR Summary:action"); hibQuery = s.CreateFullTextQuery(query, typeof(Book)); sort = new Sort(new SortField("summary_forSort", SortField.STRING, false)); //ASC hibQuery.SetSort(sort); result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual(4, result.Count, "Wrong number of test results."); Assert.AreEqual("Groovy in Action", ((Book)result[0]).Summary); // order by summary backwards query = parser.Parse("Summary:lucene OR Summary:action"); hibQuery = s.CreateFullTextQuery(query, typeof(Book)); sort = new Sort(new SortField("summary_forSort", SortField.STRING, true)); //DESC hibQuery.SetSort(sort); result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual(4, result.Count, "Wrong number of test results."); Assert.AreEqual("Hibernate & Lucene", ((Book)result[0]).Summary); // order by date backwards query = parser.Parse("Summary:lucene OR Summary:action"); hibQuery = s.CreateFullTextQuery(query, typeof(Book)); sort = new Sort(new SortField("PublicationDate", SortField.STRING, true)); //DESC hibQuery.SetSort(sort); result = hibQuery.List(); Assert.IsNotNull(result); Assert.AreEqual(4, result.Count, "Wrong number of test results."); foreach (Book book in result) { //System.out.println(book.getSummary() + " : " + book.getPublicationDate()); } Assert.AreEqual("Groovy in Action", ((Book)result[0]).Summary); tx.Commit(); DeleteTestBooks(s); s.Close(); }