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(); }
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]); } } }
private void Work(object state) { try { Random random = new Random(); QueryParser parser = new MultiFieldQueryParser(Environment.LuceneVersion, new string[] { "name", "physicalDescription", "suspectCharge" }, new Lucene.Net.Analysis.Standard.StandardAnalyzer(Environment.LuceneVersion)); using (ISession s = OpenSession()) { ITransaction tx = s.BeginTransaction(); IFullTextQuery query = GetQuery("John Doe", parser, s); Assert.IsTrue(query.ResultSize != 0); query = GetQuery("green", parser, s); random.Next(query.ResultSize - 15); query.SetFirstResult(random.Next(query.ResultSize - 15)); query.SetMaxResults(10); query.List(); tx.Commit(); } using (ISession s = OpenSession()) { ITransaction tx = s.BeginTransaction(); IFullTextQuery query = GetQuery("John Doe", parser, s); Assert.IsTrue(query.ResultSize != 0); query = GetQuery("thief", parser, s); int firstResult = random.Next(query.ResultSize - 15); query.SetFirstResult(firstResult); query.SetMaxResults(10); IList result = query.List(); object object_Renamed = result[0]; if (insert && object_Renamed is Detective) { Detective detective = (Detective)object_Renamed; detective.PhysicalDescription = detective.PhysicalDescription + " Eye" + firstResult; } else if (insert && object_Renamed is Suspect) { Suspect suspect = (Suspect)object_Renamed; suspect.PhysicalDescription = suspect.PhysicalDescription + " Eye" + firstResult; } tx.Commit(); } //System.Diagnostics.Debug.WriteLine("Interation " + worksCount + " completed on thread " + Thread.CurrentThread.ManagedThreadId); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); errorsCount++; } finally { worksCount++; } }
public void ResultSize() { 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"); IFullTextQuery hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book)); Assert.AreEqual(0, hibQuery.ResultSize); query = parser.Parse("Summary:Festina Or Brand:Seiko"); hibQuery = s.CreateFullTextQuery(query, typeof(Clock), typeof(Book)); Assert.AreEqual(2, hibQuery.ResultSize, "Query with explicit class filter"); query = parser.Parse("Summary:Festina Or Brand:Seiko"); hibQuery = s.CreateFullTextQuery(query); Assert.AreEqual(2, hibQuery.ResultSize, "Query with no class filter"); foreach (Object element in hibQuery.List()) { 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); Assert.AreEqual(0, hibQuery.ResultSize, "Query with delete objects"); 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(); }
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(); }
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 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 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 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(); }