tx.commit();
s.close();
}
public void testLuceneObjectsProjectionWithList() throws Exception {
FullTextSession s = Search.getFullTextSession( openSession() );
prepEmployeeIndex( s );
Transaction tx;
s.clear();
tx = s.beginTransaction();
QueryParser parser = new QueryParser( "dept", new StandardAnalyzer() );
Query query = parser.parse( "dept:Accounting" );
org.hibernate.search.FullTextQuery hibQuery = s.createFullTextQuery( query, Employee.class );
hibQuery.setProjection(
"id", "lastname", "dept", FullTextQuery.THIS, FullTextQuery.SCORE,
FullTextQuery.DOCUMENT, FullTextQuery.ID, FullTextQuery.DOCUMENT_ID
);
List result = hibQuery.list();
assertNotNull( result );
Object[] projection = ( Object[] ) result.get( 0 );
assertNotNull( projection );
assertEquals( "id incorrect", 1001, projection[0] );
assertEquals( "last name incorrect", "Jackson", projection[1] );
assertEquals( "dept incorrect", "Accounting", projection[2] );
assertEquals( "THIS incorrect", "Jackson", ( ( Employee ) projection[3] ).getLastname() );
assertEquals( "THIS incorrect", projection[3], s.get( Employee.class, ( Serializable ) projection[0] ) );
assertTrue( "SCORE incorrect", projection[4] instanceof Float );
assertTrue( "DOCUMENT incorrect", projection[5] instanceof Document );
assertEquals( "DOCUMENT size incorrect", 5, ( ( Document ) projection[5] ).getFields().size() );
assertEquals( "ID incorrect", 1001, projection[6] );
assertNotNull( "Lucene internal doc id", projection[7] );
// Change the projection order and null one
hibQuery.setProjection(
FullTextQuery.DOCUMENT, FullTextQuery.THIS, FullTextQuery.SCORE, null, FullTextQuery.ID,
"id", "lastname", "dept", "hireDate", FullTextQuery.DOCUMENT_ID
);
result = hibQuery.list();
assertNotNull( result );
projection = ( Object[] ) result.get( 0 );
assertNotNull( projection );
assertTrue( "DOCUMENT incorrect", projection[0] instanceof Document );
assertEquals( "DOCUMENT size incorrect", 5, ( ( Document ) projection[0] ).getFields().size() );
assertEquals( "THIS incorrect", projection[1], s.get( Employee.class, ( Serializable ) projection[4] ) );
assertTrue( "SCORE incorrect", projection[2] instanceof Float );
assertNull( "BOOST not removed", projection[3] );
assertEquals( "ID incorrect", 1001, projection[4] );
assertEquals( "id incorrect", 1001, projection[5] );
assertEquals( "last name incorrect", "Jackson", projection[6] );
assertEquals( "dept incorrect", "Accounting", projection[7] );
assertNotNull( "Date", projection[8] );
assertNotNull( "Lucene internal doc id", projection[9] );
//cleanup
for ( Object element : s.createQuery( "from " + Employee.class.getName() ).list() ) {
s.delete( element );
}
tx.commit();
s.close();
}