/** * Adds a work to the current plan. The entityClass of the work must be of the * type managed by this. * * @param work the {@code Work} instance to add to the plan */ public void addWork(Work work) { if ( work.getType() == WorkType.PURGE_ALL ) { entityById.clear(); this.deletionQueries.clear(); purgeAll = true; } else if ( work.getType() == WorkType.DELETE_BY_QUERY ) { DeleteByQueryWork delWork = (DeleteByQueryWork) work; this.deletionQueries.add( delWork.getDeleteByQuery() ); } else { Serializable id = extractProperId( work ); PerEntityWork entityWork = entityById.get( id ); if ( entityWork == null ) { entityWork = new PerEntityWork( work ); entityById.put( id, entityWork ); } entityWork.addWork( work ); } }
@Test public void testStringIdTermQuery() { ExtendedSearchIntegrator integrator = this.factoryHolder.getSearchFactory(); Worker worker = integrator.getWorker(); makeBooksForSingularTermQuery(); this.assertCount( Book.class, 2 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( BOOK_TYPE, new SingularTermDeletionQuery( "id", String.valueOf( 5 ) ) ), tc ); tc.end(); } this.assertCount( Book.class, 1 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( BOOK_TYPE, new SingularTermDeletionQuery( "id", String.valueOf( 6 ) ) ), tc ); tc.end(); } this.assertCount( Book.class, 0 ); // this should stay empty now! }
@Test public void canDeleteByQuery() throws Exception { Session s = openSession(); FullTextSession session = Search.getFullTextSession( s ); ExtendedSearchIntegrator integrator = session.getSearchFactory() .unwrap( ExtendedSearchIntegrator.class ); DeleteByQueryWork queryWork = new DeleteByQueryWork( new PojoIndexedTypeIdentifier( HockeyPlayer.class ), new SingularTermDeletionQuery( "active", "false" ) ); TransactionContext tc = new TransactionContextForTest(); integrator.getWorker().performWork( queryWork, tc ); integrator.getWorker().flushWorks( tc ); QueryDescriptor query = ElasticsearchQueries.fromJson( "{ 'query': { 'match_all' : {} } }" ); Transaction tx = s.beginTransaction(); @SuppressWarnings("unchecked") List<HockeyPlayer> result = session.createFullTextQuery( query, HockeyPlayer.class ).list(); assertThat( result ).extracting( "name" ).containsExactlyInAnyOrder( "Hergesheimer", "Brand" ); tx.commit(); s.close(); }
@Test public void testNumericIdTermQuery() { ExtendedSearchIntegrator integrator = this.factoryHolder.getSearchFactory(); Worker worker = integrator.getWorker(); makeMoviesForNumericIdTermQuery(); this.assertCount( Movie.class, 2 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( MOVIE_TYPE, new SingularTermDeletionQuery( "id", 3 ) ), tc ); tc.end(); } this.assertCount( Movie.class, 1 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( MOVIE_TYPE, new SingularTermDeletionQuery( "id", 4 ) ), tc ); tc.end(); } this.assertCount( Movie.class, 0 ); // this should stay empty now! }
/** * Adds a work to the current plan. The entityClass of the work must be of the * type managed by this. * * @param work the {@code Work} instance to add to the plan */ public void addWork(Work work) { if ( work.getType() == WorkType.PURGE_ALL ) { entityById.clear(); this.deletionQueries.clear(); purgeAll = true; } else if ( work.getType() == WorkType.DELETE_BY_QUERY ) { DeleteByQueryWork delWork = (DeleteByQueryWork) work; this.deletionQueries.add( delWork.getDeleteByQuery() ); } else { Serializable id = extractProperId( work ); PerEntityWork entityWork = entityById.get( id ); if ( entityWork == null ) { entityWork = new PerEntityWork( work ); entityById.put( id, entityWork ); } entityWork.addWork( work ); } }
@Test public void testSingularTermQuery() { ExtendedSearchIntegrator integrator = this.factoryHolder.getSearchFactory(); Worker worker = integrator.getWorker(); makeBooksForSingularTermQuery(); this.assertCount( Book.class, 2 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( BOOK_TYPE, new SingularTermDeletionQuery( "url", "lordoftherings" ) ), tc ); tc.end(); } this.assertCount( Book.class, 1 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( BOOK_TYPE, new SingularTermDeletionQuery( "url", "thehobbit" ) ), tc ); tc.end(); } this.assertCount( Book.class, 0 ); // this should stay empty now! }
TransactionContextForTest tc = new TransactionContextForTest(); ExtendedSearchIntegrator integrator = this.getExtendedSearchIntegrator(); integrator.getWorker().performWork( new DeleteByQueryWork( TSHIRT_TYPE, new SingularTermDeletionQuery( "id", String.valueOf( ts.getId() ) ) ), tc ); tc.end(); tx.commit();
.unwrap( ExtendedSearchIntegrator.class ); DeleteByQueryWork queryWork = new DeleteByQueryWork( DOWN_TO_THE_EARTH, new PojoIndexedTypeIdentifier( HockeyPlayer.class ),
private void testForQuery(Class<?> entityType, ExtendedSearchIntegrator integrator, DeletionQuery query, int expectedCount) { Worker worker = integrator.getWorker(); makeBooksForNumRangeQuery(); this.assertCount( entityType, 2 ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new DeleteByQueryWork( BOOK_TYPE, query ), tc ); tc.end(); } this.assertCount( entityType, expectedCount ); { TransactionContextForTest tc = new TransactionContextForTest(); worker.performWork( new Work( BOOK_TYPE, null, WorkType.PURGE_ALL ), tc ); tc.end(); } this.assertCount( entityType, 0 ); }