@Test
@SuppressWarnings("unchecked")
public void testBoolean() throws Exception {
Transaction transaction = fullTextSession.beginTransaction();
final QueryBuilder monthQb = fullTextSession.getSearchFactory()
.buildQueryBuilder().forEntity( Month.class ).get();
//must
Query query = monthQb
.bool()
.must( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() )
.createQuery();
List<Month> results = fullTextSession.createFullTextQuery( query, Month.class ).list();
assertEquals( 1, results.size() );
assertEquals( "January", results.get( 0 ).getName() );
//must not + all
query = monthQb
.bool()
.should( monthQb.all().createQuery() )
.must( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() )
.not()
.createQuery();
results = fullTextSession.createFullTextQuery( query, Month.class ).list();
assertEquals( 2, results.size() );
assertEquals( "February", results.get( 0 ).getName() );
assertEquals( "March", results.get( 1 ).getName() );
//implicit must not + all (not recommended)
query = monthQb
.bool()
.must( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() )
.not()
.createQuery();
results = fullTextSession.createFullTextQuery( query, Month.class ).list();
assertEquals( 2, results.size() );
assertEquals( "February", results.get( 0 ).getName() );
assertEquals( "March", results.get( 1 ).getName() );
//all except (recommended)
query = monthQb
.all()
.except( monthQb.keyword().onField( "mythology" ).matching( "colder" ).createQuery() )
.createQuery();
results = fullTextSession.createFullTextQuery( query, Month.class ).list();
assertEquals( 2, results.size() );
assertEquals( "February", results.get( 0 ).getName() );