private void deleteTestContractors() { Transaction tx = fullTextSession.beginTransaction(); fullTextSession.createQuery( "delete " + Plumber.class.getName() ).executeUpdate(); fullTextSession.createQuery( "delete " + BrickLayer.class.getName() ).executeUpdate(); fullTextSession.createQuery( "delete " + Thatcher.class.getName() ).executeUpdate(); tx.commit(); fullTextSession.clear(); }
private void deleteTestNumbers() { Transaction tx = fullTextSession.beginTransaction(); fullTextSession.createQuery( "delete " + NumberHolder.class.getName() ).executeUpdate(); tx.commit(); fullTextSession.clear(); }
private void deleteTestBooks() { Transaction tx = fullTextSession.beginTransaction(); fullTextSession.createQuery( "delete " + Book.class.getName() ).executeUpdate(); tx.commit(); fullTextSession.clear(); }
private void deleteTestContractors() { Transaction tx = fullTextSession.beginTransaction(); fullTextSession.createQuery( "delete " + BrickLayer.class.getName() ).executeUpdate(); tx.commit(); fullTextSession.clear(); }
FullTextSession session = Search.getFullTextSession(getSessionFactory().openSession()); session.setFlushMode(FlushMode.MANUAL); session.setCacheMode(CacheMode.IGNORE); Transaction transaction = session.beginTransaction(); ScrollableResults items = session.createQuery("from Item i ") .scroll(ScrollMode.FORWARD_ONLY); while(items.next()) { Object nextToIndex = items.get(0); session.index(nextToIndex); session.flushToIndexes(); session.clear(); } transaction.commit();
@Test public void testDoubleInsert() throws Exception { PersonalContact contact = createTestData(); FullTextSession s = Search.getFullTextSession( openSession( ) ); s.getTransaction().begin(); Term term = new Term( "county", "county" ); TermQuery termQuery = new TermQuery( term ); Query query = s.createFullTextQuery( termQuery ); assertEquals( 1, query.list().size() ); contact = (PersonalContact) s.get( PersonalContact.class, contact.getId() ); contact.getPhoneNumbers().clear(); contact.getAddresses().clear(); s.flush(); s.clear(); s.createQuery( "delete " + Address.class.getName() ).executeUpdate(); s.createQuery( "delete " + Phone.class.getName() ).executeUpdate(); s.createQuery( "delete " + Contact.class.getName() ).executeUpdate(); s.getTransaction().commit(); s.close(); }
public Object doInJpa(EntityManager em) throws PersistenceException { FullTextSession fullTextSession = getFullTextSession(getHibernateSession(em)); fullTextSession.purgeAll(Webbis.class); // Do not update the second level cache. It will just slow things down. fullTextSession.setCacheMode(CacheMode.GET); // Read 5000 entries at a time. final int BATCH_SIZE = 5000; // Due to a bug in Hibernate (HHH-1283) a join does not work here. // See http://opensource.atlassian.com/projects/hibernate/browse/HHH-1283 ScrollableResults results = fullTextSession.createQuery("from Webbis w").scroll(); int index = 0; while (results.next()) { index++; fullTextSession.index(results.get(0)); // index each element if (index % BATCH_SIZE == 0) { fullTextSession.flushToIndexes(); // apply changes to indexes fullTextSession.clear(); // clear since the queue is processed } } return null; } });
@Override @After public void tearDown() throws Exception { try { Transaction tx = fts.getTransaction(); if ( tx.getStatus() != TransactionStatus.ACTIVE ) { tx = fts.beginTransaction(); } assertEquals( 1000, fts.createQuery( "delete from " + Clock.class.getName() ).executeUpdate() ); fts.purgeAll( Clock.class ); tx.commit(); fts.close(); } finally { super.tearDown(); } }
@Test public void testResultTransformToDelimString() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); prepEmployeeIndex( s ); Transaction tx; s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer ); Query query = parser.parse( "dept:ITech" ); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class ); hibQuery.setProjection( "id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.ID ); hibQuery.setResultTransformer( new ProjectionToDelimStringResultTransformer() ); hibQuery.setSort( new Sort( new SortField( "id", SortField.Type.STRING ), SortField.FIELD_SCORE ) ); @SuppressWarnings("unchecked") List<String> result = hibQuery.list(); assertTrue( "incorrect transformation", result.get( 0 ).startsWith( "1000, Griffin, ITech" ) ); assertTrue( "incorrect transformation", result.get( 1 ).startsWith( "1002, Jimenez, ITech" ) ); //cleanup for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) { s.delete( element ); } tx.commit(); s.close(); }
for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) { s.delete( element );
@Test public void testNoTermVector() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); Transaction tx = s.beginTransaction(); Employee e1 = new Employee( 1000, "Griffin", "ITech" ); s.save( e1 ); tx.commit(); s.clear(); tx = s.beginTransaction(); // Here's how to get a reader from a FullTextSession SearchFactory searchFactory = s.getSearchFactory(); IndexReader reader = searchFactory.getIndexReaderAccessor().open( Employee.class ); Terms termVector = reader.getTermVector( 0, "dept" ); assertNull( "should not find a term position vector", termVector ); // cleanup for ( Object element : s.createQuery( "from " + ElectricalProperties.class.getName() ).list() ) { s.delete( element ); } searchFactory.getIndexReaderAccessor().close( reader ); tx.commit(); s.close(); }
@Test @TestForIssue(jiraKey = "HSEARCH-296") public void testClassProjection() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); prepEmployeeIndex( s ); s.clear(); Transaction tx = s.beginTransaction(); QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer ); Query query = parser.parse( "dept:ITech" ); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class ); hibQuery.setProjection( FullTextQuery.OBJECT_CLASS ); List<?> result = hibQuery.list(); assertNotNull( result ); Object[] projection = (Object[]) result.get( 0 ); assertNotNull( projection ); assertEquals( "Wrong projected class", Employee.class, projection[0] ); //cleanup for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) { s.delete( element ); } tx.commit(); s.close(); }
@Test public void testProjectionOnScoreWithoutRelevanceSort() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); prepEmployeeIndex( s ); Transaction tx; s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer ); Query query = parser.parse( "dept:Accounting" ); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class ); hibQuery.setSort( new Sort( new SortField( "id", SortField.Type.STRING ) ) ); hibQuery.setProjection( FullTextQuery.SCORE, FullTextQuery.ID ); List<?> result = hibQuery.list(); assertNotNull( result ); Object[] projection = (Object[]) result.get( 0 ); assertNotNull( projection ); assertTrue( "SCORE incorrect", projection[0] instanceof Float ); assertFalse( "SCORE should not be a NaN", Float.isNaN( (Float) projection[0] ) ); //cleanup for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) { s.delete( element ); } tx.commit(); s.close(); }
@Test public void testProjectionUnmappedFieldValues() throws ParseException, IOException { FullTextSession s = Search.getFullTextSession( openSession() ); Transaction tx = s.beginTransaction(); s.persist( new CalendarDay().setDayFromItalianString( "01/04/2011" ) ); s.persist( new CalendarDay().setDayFromItalianString( "02/04/2011" ) ); tx.commit(); s.clear(); tx = s.beginTransaction(); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( new MatchAllDocsQuery(), CalendarDay.class ); hibQuery.setProjection( "day_year" ); List<?> result = hibQuery.list(); assertNotNull( result ); assertEquals( "Wrong number of results", 2, result.size() ); for ( Object resultLine : result ) { Object[] projection = (Object[]) resultLine; assertNotNull( projection ); assertEquals( "Wrong projected result", "2011", projection[0] ); } //cleanup for ( Object element : s.createQuery( "from " + CalendarDay.class.getName() ).list() ) { s.delete( element ); } tx.commit(); s.close(); }
@Test public void testOptimize() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); Transaction tx = s.beginTransaction(); int loop = 2000; 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(); s = Search.getFullTextSession( openSession() ); tx = s.beginTransaction(); s.getSearchFactory().optimize( Email.class ); tx.commit(); s.close(); //check non indexed object get indexed by s.index s = Search.getFullTextSession( openSession() ); tx = s.beginTransaction(); QueryParser parser = new QueryParser( "id", TestConstants.stopAnalyzer ); int result = s.createFullTextQuery( parser.parse( "body:wrote" ) ).getResultSize(); assertEquals( 2000, result ); s.createQuery( "delete " + Email.class.getName() ).executeUpdate(); tx.commit(); s.close(); }
for ( Object element : fullTextSession.createQuery( "from " + BoostedGetDescriptionLibrary.class.getName() ) .list() ) { fullTextSession.delete( element );
for ( Object element : fullTextSession.createQuery( "from " + BoostedFieldDescriptionLibrary.class.getName() ) .list() ) { fullTextSession.delete( element );
for ( Object element : fullTextSession.createQuery( "from " + BoostedDescriptionLibrary.class.getName() ) .list() ) { fullTextSession.delete( element );
@Test public void testTransformListIsCalled() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); prepEmployeeIndex( s ); Transaction tx; s.clear(); tx = s.beginTransaction(); QueryParser parser = new QueryParser( "dept", TestConstants.standardAnalyzer ); Query query = parser.parse( "dept:ITech" ); org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class ); hibQuery.setProjection( "id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.ID ); hibQuery.setSort( new Sort( new SortField( "id", SortField.Type.STRING ), SortField.FIELD_SCORE ) ); final CounterCallsProjectionToMapResultTransformer counters = new CounterCallsProjectionToMapResultTransformer(); hibQuery.setResultTransformer( counters ); hibQuery.list(); assertEquals( counters.getTransformListCounter(), 1 ); //cleanup for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) { s.delete( element ); } tx.commit(); s.close(); }
@Test @Category(ElasticsearchSupportInProgress.class) // HSEARCH-2428 Provide an alternative to org.hibernate.search.analyzer.Discriminator for Elasticsearch? public void testAnalyzerDiscriminator() throws Exception { FullTextSession s = Search.getFullTextSession( openSession() ); Transaction tx = s.beginTransaction(); BlogEntry deEntry = new BlogEntry(); deEntry.setTitle( "aufeinanderschl\u00FCgen" ); deEntry.setDescription( "aufeinanderschl\u00FCgen" ); deEntry.setLanguage( "de" ); s.persist( deEntry ); BlogEntry enEntry = new BlogEntry(); enEntry.setTitle( "acknowledgment" ); enEntry.setDescription( "acknowledgment" ); enEntry.setLanguage( "en" ); s.persist( enEntry ); tx.commit(); s.clear(); tx = s.beginTransaction(); // at query time we use a standard analyzer. We explicitly search for tokens which can only be found if the // right language specific stemmer was used at index time assertEquals( 1, nbrOfMatchingResults( "description", "aufeinanderschlug", s ) ); assertEquals( 1, nbrOfMatchingResults( "description", "acknowledg", s ) ); assertEquals( 0, nbrOfMatchingResults( "title", "aufeinanderschlug", s ) ); assertEquals( 1, nbrOfMatchingResults( "title", "acknowledgment", s ) ); for ( Object result : s.createQuery( "from " + BlogEntry.class.getName() ).list() ) { s.delete( result ); } tx.commit(); s.close(); }