Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-2983 Take boosts into account for all queries built with the DSL
Instead of setting the boost on a per query type basis, let's set it one level up so that we're sure it's defined for all the query types.
- Loading branch information
Showing
4 changed files
with
134 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
engine/src/test/java/org/hibernate/search/test/dsl/BoostDSLTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.test.dsl; | ||
|
||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.Sort; | ||
import org.apache.lucene.search.SortField; | ||
import org.hibernate.search.query.dsl.QueryBuilder; | ||
import org.hibernate.search.testsupport.TestForIssue; | ||
import org.hibernate.search.testsupport.junit.SearchFactoryHolder; | ||
import org.hibernate.search.testsupport.junit.SearchITHelper; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author Guillaume Smet | ||
*/ | ||
@TestForIssue(jiraKey = "HSEARCH-2983") | ||
public class BoostDSLTest { | ||
|
||
@Rule | ||
public final SearchFactoryHolder sfHolder = new SearchFactoryHolder( Coffee.class, CoffeeBrand.class ); | ||
|
||
private final SearchITHelper helper = new SearchITHelper( sfHolder ); | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
indexTestData(); | ||
} | ||
|
||
@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" ); | ||
} | ||
|
||
private void indexTestData() { | ||
createCoffee( | ||
"Kazaar", | ||
"EXCEPTIONALLY INTENSE AND SYRUPY", | ||
"A daring blend of two Robustas from Brazil and Guatemala, specially prepared for Nespresso, and a separately roasted Arabica from South America, Kazaar is a coffee of exceptional intensity. Its powerful bitterness and notes of pepper are balanced by a full and creamy texture.", | ||
12 | ||
); | ||
createCoffee( | ||
"Dharkan", | ||
"LONG ROASTED AND VELVETY", | ||
"This blend of Arabicas from Latin America and Asia fully unveils its character thanks to the technique of long roasting at a low temperature. Its powerful personality reveals intense roasted notes together with hints of bitter cocoa powder and toasted cereals that express themselves in a silky and velvety txture.", | ||
11 | ||
); | ||
createCoffee( | ||
"Ristretto", | ||
"POWERFUL AND CONTRASTING", | ||
"A blend of South American and East African Arabicas, with a touch of Robusta, roasted separately to create the subtle fruity note of this full-bodied, intense espresso.", | ||
10 | ||
); | ||
} | ||
|
||
private void createCoffee(String name, String summary, String description, int intensity) { | ||
Coffee coffee = new Coffee(); | ||
coffee.setId( name ); | ||
coffee.setName( name ); | ||
coffee.setSummary( summary ); | ||
coffee.setDescription( description ); | ||
coffee.setIntensity( intensity ); | ||
helper.add( coffee ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters