private Document getSingleIndexedDocument(FullTextSession fullTextSession) throws ParseException { Query query = createLuceneQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Value.class ); fullTextQuery.setProjection( FullTextQuery.DOCUMENT ); fullTextQuery.setResultTransformer( new ProjectionToMapResultTransformer() ); List<?> mappedResults = fullTextQuery.list(); assertTrue( "Wrong result size", mappedResults.size() == 1 ); Map<?, ?> map = (Map<?, ?>) mappedResults.get( 0 ); Document document = (Document) map.get( FullTextQuery.DOCUMENT ); assertNotNull( document ); return document; }
@Test public void testProjectedValueGetsConvertedToNull() throws Exception { Value nullValue = new Value( null ); FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); Transaction tx = fullTextSession.beginTransaction(); getSession().save( nullValue ); tx.commit(); fullTextSession.clear(); tx = fullTextSession.beginTransaction(); Query query = new MatchAllDocsQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Value.class ); fullTextQuery.setProjection( "id", "value" ); fullTextQuery.setResultTransformer( new ProjectionToMapResultTransformer() ); List<?> mappedResults = fullTextQuery.list(); assertTrue( "Wrong result size", mappedResults.size() == 1 ); Map<?, ?> map = (Map<?, ?>) mappedResults.get( 0 ); Integer id = (Integer) map.get( "id" ); assertNotNull( id ); String value = (String) map.get( "value" ); assertEquals( "The null token should be converted back to null", null, value ); tx.commit(); fullTextSession.close(); }
@Test public void testProjectedValueGetsConvertedToNull() throws Exception { ProgrammaticConfiguredValue nullValue = new ProgrammaticConfiguredValue( null ); FullTextSession fullTextSession = Search.getFullTextSession( openSession() ); Transaction tx = fullTextSession.beginTransaction(); getSession().save( nullValue ); tx.commit(); fullTextSession.clear(); tx = fullTextSession.beginTransaction(); Query query = new MatchAllDocsQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, ProgrammaticConfiguredValue.class ); fullTextQuery.setProjection( "id", "value" ); fullTextQuery.setResultTransformer( new ProjectionToMapResultTransformer() ); List<?> mappedResults = fullTextQuery.list(); assertTrue( "Wrong result size", mappedResults.size() == 1 ); Map<?, ?> map = (Map<?, ?>) mappedResults.get( 0 ); Integer id = (Integer) map.get( "id" ); assertNotNull( id ); String value = (String) map.get( "value" ); assertEquals( "The null token should be converted back to null", null, value ); tx.commit(); fullTextSession.close(); }
@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(); }
hibQuery.setSort( new Sort( new SortField( "id", SortField.Type.STRING ), SortField.FIELD_SCORE ) ); hibQuery.setResultTransformer( new ProjectionToMapResultTransformer() );
@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(); }