@TestForIssue(jiraKey = "HSEARCH-1290")
public class BooleanQueryTest extends SearchTestCaseJUnit4 {
private static final String TEST_DATA[] = { "Hibernate", "Lucene", "ORM" };
@Before
public void setUp() throws Exception {
super.setUp();
indexTestData();
}
@Test
public void testBooleanMustQueryInCombinationWithOnFields() {
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
Transaction tx = fullTextSession.beginTransaction();
QueryBuilder queryBuilder = getSearchFactory().buildQueryBuilder().forEntity( Foo.class ).get();
Query luceneQuery = queryBuilder.bool()
.must( queryBuilder.keyword().onFields( "text" ).matching( "hibernate" ).createQuery() )
.createQuery();
org.hibernate.Query hsearchQuery = fullTextSession.createFullTextQuery( luceneQuery );
@SuppressWarnings("unchecked")
List<Foo> results = (List<Foo>) hsearchQuery.list();
assertEquals( "There should be only one match for the given query", 1, results.size() );
tx.commit();
fullTextSession.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Foo.class };
}
private void indexTestData() {
FullTextSession fullTextSession = Search.getFullTextSession( openSession() );
Transaction tx = fullTextSession.beginTransaction();
for ( String testString : TEST_DATA ) {
Foo foo = new Foo( testString );
fullTextSession.save( foo );
}
tx.commit();
fullTextSession.close();
}
@Entity
@Indexed
static class Foo {
@Id
@GeneratedValue
int id;
@Field
String text;
Foo() {
}
Foo(String text) {
this.text = text;
}
}
}