public void testQueryOnAllEntities() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); Transaction tx = s.beginTransaction(); Person person = new Person(); person.setName("Jon Doe"); s.save(person); tx.commit(); tx = s.beginTransaction(); QueryParser parser = new QueryParser( TestConstants.getTargetLuceneVersion(), "name", TestConstants.standardAnalyzer); Query query = parser.parse("name:foo"); FullTextQuery hibQuery = s.createFullTextQuery(query); try { hibQuery.list(); fail(); } catch (SearchException e) { assertTrue("Wrong message", e.getMessage().startsWith("There are no mapped entities")); } tx.rollback(); s.close(); }
public void testBoostedFieldDesc() throws Exception { FullTextSession fullTextSession = Search.getFullTextSession(openSession()); buildBoostedFieldIndex(fullTextSession); fullTextSession.clear(); Transaction tx = fullTextSession.beginTransaction(); QueryParser authorParser = new QueryParser( TestConstants.getTargetLuceneVersion(), "author", TestConstants.standardAnalyzer); QueryParser descParser = new QueryParser( TestConstants.getTargetLuceneVersion(), "description", TestConstants.standardAnalyzer); Query author = authorParser.parse("Wells"); Query desc = descParser.parse("martians"); BooleanQuery query = new BooleanQuery(); query.add(author, BooleanClause.Occur.SHOULD); query.add(desc, BooleanClause.Occur.SHOULD); log.debug(query.toString()); org.hibernate.search.FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, BoostedFieldDescriptionLibrary.class); List results = hibQuery.list(); assertTrue( "incorrect document boost", ((BoostedFieldDescriptionLibrary) results.get(0)).getDescription().startsWith("Martians")); log.debug(hibQuery.explain(0).toString()); log.debug(hibQuery.explain(1).toString()); // cleanup for (Object element : fullTextSession .createQuery("from " + BoostedFieldDescriptionLibrary.class.getName()) .list()) { fullTextSession.delete(element); } tx.commit(); fullTextSession.close(); }
public void testBatchSize() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); Transaction tx = s.beginTransaction(); final int loop = 14; s.doWork( new Work() { @Override public void execute(Connection connection) throws SQLException { for (int i = 0; i < loop; i++) { Statement statmt = connection.createStatement(); statmt.executeUpdate( "insert into Domain(id, name) values( + " + (i + 1) + ", 'sponge" + i + "')"); statmt.executeUpdate( "insert into Email(id, title, body, header, domain_id) values( + " + (i + 1) + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope', " + (i + 1) + ")"); statmt.close(); } } }); tx.commit(); s.close(); // check non created object does get found!!1 s = Search.getFullTextSession(openSession()); tx = s.beginTransaction(); ScrollableResults results = s.createCriteria(Email.class).scroll(ScrollMode.FORWARD_ONLY); int index = 0; while (results.next()) { index++; s.index(results.get(0)); if (index % 5 == 0) { s.clear(); } } tx .commit(); // if you get a LazyInitializationException, that's because we clear() the // session in the loop.. it only works with a batch size of 5 (the point of the // test) s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer); List result = s.createFullTextQuery(parser.parse("body:create")).list(); assertEquals(14, result.size()); for (Object object : result) { s.delete(object); } tx.commit(); s.close(); }
/** * This tests that a field created by a user-supplied EquipmentType class has been created and is * a translation from an identifier to a manufacturer name. * * @throws Exception */ public void testClassBridges() throws Exception { org.hibernate.Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(getDepts1()); s.persist(getDepts2()); s.persist(getDepts3()); s.persist(getDepts4()); s.flush(); tx.commit(); tx = s.beginTransaction(); FullTextSession session = Search.getFullTextSession(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( TestConstants.getTargetLuceneVersion(), "equipment", TestConstants.simpleAnalyzer); // Check the second ClassBridge annotation Query query = parser.parse("equiptype:Cisco"); org.hibernate.search.FullTextQuery hibQuery = session.createFullTextQuery(query, Departments.class); List<Departments> result = hibQuery.list(); assertNotNull(result); assertEquals("incorrect number of results returned", 2, result.size()); for (Departments d : result) { assertEquals("incorrect manufacturer", "C", d.getManufacturer()); } // No data cross-ups. query = parser.parse("branchnetwork:Kent Lewin"); hibQuery = session.createFullTextQuery(query, Departments.class); result = hibQuery.list(); assertNotNull(result); assertTrue("problem with field cross-ups", result.size() == 0); // Non-ClassBridge field. parser = new QueryParser( TestConstants.getTargetLuceneVersion(), "branchHead", TestConstants.simpleAnalyzer); query = parser.parse("branchHead:Kent Lewin"); hibQuery = session.createFullTextQuery(query, Departments.class); result = hibQuery.list(); assertNotNull(result); assertTrue("incorrect entity returned, wrong branch head", result.size() == 1); assertEquals("incorrect entity returned", "Kent Lewin", (result.get(0)).getBranchHead()); // Check other ClassBridge annotation. parser = new QueryParser( TestConstants.getTargetLuceneVersion(), "branchnetwork", TestConstants.simpleAnalyzer); query = parser.parse("branchnetwork:st. george 1D"); hibQuery = session.createFullTextQuery(query, Departments.class); result = hibQuery.list(); assertNotNull(result); assertEquals("incorrect entity returned, wrong network", "1D", (result.get(0)).getNetwork()); assertEquals( "incorrect entity returned, wrong branch", "St. George", (result.get(0)).getBranch()); assertEquals("incorrect number of results returned", 1, result.size()); // cleanup for (Object element : s.createQuery("from " + Departments.class.getName()).list()) { s.delete(element); } tx.commit(); s.close(); }
/** * This test checks for two fields being concatentated by the user-supplied CatFieldsClassBridge * class which is specified as the implementation class in the ClassBridge annotation of the * Department class. * * @throws Exception */ public void testClassBridge() throws Exception { org.hibernate.Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(getDept1()); s.persist(getDept2()); s.persist(getDept3()); s.flush(); tx.commit(); tx = s.beginTransaction(); FullTextSession session = Search.getFullTextSession(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( TestConstants.getTargetLuceneVersion(), "branchnetwork", TestConstants.simpleAnalyzer); Query query = parser.parse("branchnetwork:layton 2B"); org.hibernate.search.FullTextQuery hibQuery = session.createFullTextQuery(query, Department.class); List result = hibQuery.list(); assertNotNull(result); assertEquals( "incorrect entity returned, wrong network", "2B", ((Department) result.get(0)).getNetwork()); assertEquals( "incorrect entity returned, wrong branch", "Layton", ((Department) result.get(0)).getBranch()); assertEquals("incorrect number of results returned", 1, result.size()); // Partial match. query = parser.parse("branchnetwork:3c"); hibQuery = session.createFullTextQuery(query, Department.class); result = hibQuery.list(); assertNotNull(result); assertEquals( "incorrect entity returned, wrong network", "3C", ((Department) result.get(0)).getNetwork()); assertEquals( "incorrect entity returned, wrong branch", "West Valley", ((Department) result.get(0)).getBranch()); assertEquals("incorrect number of results returned", 1, result.size()); // No data cross-ups . query = parser.parse("branchnetwork:Kent Lewin"); hibQuery = session.createFullTextQuery(query, Department.class); result = hibQuery.list(); assertNotNull(result); assertTrue("problem with field cross-ups", result.size() == 0); // Non-ClassBridge field. parser = new QueryParser( TestConstants.getTargetLuceneVersion(), "branchHead", TestConstants.simpleAnalyzer); query = parser.parse("branchHead:Kent Lewin"); hibQuery = session.createFullTextQuery(query, Department.class); result = hibQuery.list(); assertNotNull(result); assertTrue("incorrect entity returned, wrong branch head", result.size() == 1); assertEquals( "incorrect entity returned", "Kent Lewin", ((Department) result.get(0)).getBranchHead()); // cleanup for (Object element : s.createQuery("from " + Department.class.getName()).list()) { s.delete(element); } tx.commit(); s.close(); }
/** * This is the same test as above with a projection query to show the presence of the ClassBridge * impl built fields just in case you don't believe us. * * @throws Exception */ public void testClassBridgesWithProjection() throws Exception { org.hibernate.Session s = openSession(); Transaction tx = s.beginTransaction(); s.persist(getDepts1()); s.persist(getDepts2()); s.persist(getDepts3()); s.persist(getDepts4()); s.flush(); tx.commit(); tx = s.beginTransaction(); FullTextSession session = Search.getFullTextSession(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( TestConstants.getTargetLuceneVersion(), "equipment", TestConstants.simpleAnalyzer); // Check the second ClassBridge annotation Query query = parser.parse("equiptype:Cisco"); org.hibernate.search.FullTextQuery hibQuery = session.createFullTextQuery(query, Departments.class); hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT); ScrollableResults projections = hibQuery.scroll(); assertNotNull(projections); projections.beforeFirst(); projections.next(); Object[] projection = projections.get(); assertTrue("DOCUMENT incorrect", projection[0] instanceof Departments); assertEquals("id incorrect", 1, ((Departments) projection[0]).getId()); assertTrue("DOCUMENT incorrect", projection[1] instanceof Document); assertEquals("DOCUMENT size incorrect", 8, ((Document) projection[1]).getFields().size()); assertNotNull("equiptype is null", ((Document) projection[1]).getField("equiptype")); assertEquals( "equiptype incorrect", "Cisco", ((Document) projection[1]).getField("equiptype").stringValue()); assertNotNull("branchnetwork is null", ((Document) projection[1]).getField("branchnetwork")); assertEquals( "branchnetwork incorrect", "Salt Lake City 1A", ((Document) projection[1]).getField("branchnetwork").stringValue()); projections.next(); projection = projections.get(); assertTrue("DOCUMENT incorrect", projection[0] instanceof Departments); assertEquals("id incorrect", 4, ((Departments) projection[0]).getId()); assertTrue("DOCUMENT incorrect", projection[1] instanceof Document); assertEquals("DOCUMENT size incorrect", 8, ((Document) projection[1]).getFields().size()); assertNotNull("equiptype is null", ((Document) projection[1]).getField("equiptype")); assertEquals( "equiptype incorrect", "Cisco", ((Document) projection[1]).getField("equiptype").stringValue()); assertNotNull("branchnetwork is null", ((Document) projection[1]).getField("branchnetwork")); assertEquals( "branchnetwork incorrect", "St. George 1D", ((Document) projection[1]).getField("branchnetwork").stringValue()); assertTrue("incorrect result count returned", projections.isLast()); // cleanup for (Object element : s.createQuery("from " + Departments.class.getName()).list()) { s.delete(element); } tx.commit(); s.close(); }
public void testTransactional() throws Exception { FullTextSession s = Search.getFullTextSession(openSession()); Transaction tx = s.beginTransaction(); final int loop = 4; for (int i = 0; i < loop; i++) { Email email = new Email(); email.setId((long) i + 1); email.setTitle("JBoss World Berlin"); email.setBody("Meet the guys who wrote the software"); s.persist(email); } tx.commit(); s.close(); // check non created object does get found!!1 s = Search.getFullTextSession(openSession()); tx = s.beginTransaction(); QueryParser parser = new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer); List result = s.createFullTextQuery(parser.parse("body:create")).list(); assertEquals(0, result.size()); tx.commit(); s.close(); s = Search.getFullTextSession(openSession()); s.getTransaction().begin(); s.doWork( new Work() { @Override public void execute(Connection connection) throws SQLException { Statement stmt = connection.createStatement(); stmt.executeUpdate("update Email set body='Meet the guys who write the software'"); stmt.close(); } }); s.doWork( new Work() { @Override public void execute(Connection connection) throws SQLException { // insert an object never indexed Statement stmt = connection.createStatement(); stmt.executeUpdate( "insert into Email(id, title, body, header) values( + " + (loop + 1) + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope')"); stmt.close(); } }); s.getTransaction().commit(); s.close(); s = Search.getFullTextSession(openSession()); tx = s.beginTransaction(); parser = new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer); result = s.createFullTextQuery(parser.parse("body:write")).list(); assertEquals(0, result.size()); result = s.createCriteria(Email.class).list(); for (int i = 0; i < loop / 2; i++) { s.index(result.get(i)); } tx.commit(); // do the process s.index(result.get(loop / 2)); // do the process out of tx tx = s.beginTransaction(); for (int i = loop / 2 + 1; i < loop; i++) { s.index(result.get(i)); } tx.commit(); // do the process s.close(); s = Search.getFullTextSession(openSession()); tx = s.beginTransaction(); // object never indexed Email email = (Email) s.get(Email.class, Long.valueOf(loop + 1)); s.index(email); tx.commit(); s.close(); // check non indexed object get indexed by s.index s = Search.getFullTextSession(openSession()); tx = s.beginTransaction(); result = s.createFullTextQuery(parser.parse("body:create")).list(); assertEquals(1, result.size()); tx.commit(); s.close(); }