@Override @SuppressWarnings("unchecked") public SearchResult<Page> search(String term, int offset, int limit) { FullTextEntityManager fullTextEm = Search.getFullTextEntityManager( getEm() ); QueryBuilder queryBuilder = fullTextEm.getSearchFactory().buildQueryBuilder() .forEntity( Page.class ).get(); Query luceneQuery; if ( term == null || term.isEmpty() ) { luceneQuery = queryBuilder.all().createQuery(); } else { luceneQuery = queryBuilder.keyword() .onField( "title" ).boostedTo( 2.0f ) .andField( "content" ) .matching( term ) .createQuery(); } Sort scoreSort = queryBuilder.sort().byScore().createSort(); FullTextQuery query = fullTextEm.createFullTextQuery( luceneQuery, Page.class ) .setFirstResult( offset ) .setMaxResults( limit ) .setSort( scoreSort ); return new SearchResult<>( query.getResultSize(), query.getResultList() ); }
@Test public void testBoostOnTermQuery() { QueryBuilder qb = helper.queryBuilder( Coffee.class ); Query query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 40f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "summary" ).boostedTo( 1f ).matching( "VELVETY" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Kazaar", "Dharkan" ); query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 1f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "summary" ).boostedTo( 40f ).matching( "VELVETY" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Dharkan", "Kazaar" ); }
@Test public void testBoostOnNumericQuery() { QueryBuilder qb = helper.queryBuilder( Coffee.class ); Query query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 40f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "intensity" ).boostedTo( 1f ).matching( 11 ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Kazaar", "Dharkan" ); query = qb.bool() .should( qb.keyword().onField( "name" ).boostedTo( 1f ).matching( "Kazaar" ).createQuery() ) .should( qb.keyword().onField( "intensity" ).boostedTo( 40f ).matching( 11 ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Coffee.class ) .sort( new Sort( SortField.FIELD_SCORE ) ) .matchesExactlyIds( "Dharkan", "Kazaar" ); }
@Test public void testDSLKeywordWithBoost() throws Exception { try ( Session session = openSession() ) { FullTextSession fullTextSession = Search.getFullTextSession( session ); final QueryBuilder queryBuilder = queryBuilder( fullTextSession ); Query query = queryBuilder .keyword() .onField( "message" ) .boostedTo( 2.0f ) .matching( "A very important matter" ) .createQuery(); FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( query, Letter.class ); String queryString = fullTextQuery.getQueryString(); assertJsonEquals( "{'query':{'match':{'message':{'query':'A very important matter','boost':2.0}}}}", queryString ); } }
public void testBooleanQueriesShouldNot() { loadTestingData(); Query subQuery1 = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get() .keyword().onField("name").boostedTo(0.5f) .matching("Goat").createQuery(); Query subQuery2 = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get() .range().onField("age").boostedTo(2f).below(20).createQuery(); Query query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .should(subQuery1).should(subQuery2).createQuery(); CacheQuery<Person> cacheQuery = Search.getSearchManager(cache).getQuery(query); List<Person> found = cacheQuery.list(); assertEquals(3, found.size()); assert found.get(0).equals(person1); assert found.get(1).equals(person2); assert found.get(2).equals(person3); subQuery1 = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get() .keyword().onField("name").boostedTo(3.5f) .matching("Goat").createQuery(); query = Search.getSearchManager(cache).buildQueryBuilderForClass(Person.class).get().bool() .should(subQuery1).should(subQuery2).createQuery(); cacheQuery = Search.getSearchManager(cache).getQuery(query); found = cacheQuery.list(); assertEquals(3, found.size()); assert found.get(0).equals(person2); assert found.get(1).equals(person3); assert found.get(2).equals(person1); }
@Test public void testMultipleFields() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //combined query, January and February both contain whitening but February in a longer text Query query = monthQb.keyword() .onField( "mythology" ) .andField( "history" ) .matching( "whitening" ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1, 2 ); //combined query, January and February both contain whitening but February in a longer text query = monthQb.keyword() .onFields( "mythology", "history" ) .boostedTo( 30 ) .matching( "whitening" ).createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1, 2 ); //boosted query, January and February both contain whitening but February in a longer text //since history is boosted, February should come first though query = monthQb.keyword() .onField( "mythology" ) .andField( "history" ) .boostedTo( 30 ) .matching( "whitening" ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2, 1 ); }
@Test public void testQueryCustomization() throws Exception { final QueryBuilder monthQb = helper.queryBuilder( Month.class ); //combined query, January and February both contain whitening but February in a longer text Query query = monthQb .bool() .should( monthQb.keyword().onField( "mythology" ).matching( "whitening" ).createQuery() ) .should( monthQb.keyword().onField( "history" ).matching( "whitening" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 1, 2 ); //boosted query, January and February both contain whitening but February in a longer text //since history is boosted, February should come first though query = monthQb .bool() .should( monthQb.keyword().onField( "mythology" ).matching( "whitening" ).createQuery() ) .should( monthQb.keyword().onField( "history" ).boostedTo( 30 ).matching( "whitening" ).createQuery() ) .createQuery(); helper.assertThat( query ).from( Month.class ).matchesExactlyIds( 2, 1 ); //FIXME add other method tests besides boostedTo }