edit: Created Jira issue
HSEARCH-770Failing test (for testing, add it to org.hibernate.search.test.query.facet.RangeFacetingTest)
Code:
public void testRangeBelowWithNullValues() {
final String facetingName = "truckHorsePowerFaceting";
FacetingRequest rangeRequest = queryBuilder( Truck.class ).facet()
.name( facetingName )
.onField( "horsePower" )
.range()
.below( 1000 )
.createFacetingRequest();
FullTextQuery query = createMatchAllQuery( Truck.class );
FacetManager facetManager = query.getFacetManager();
query.getFacetManager().enableFaceting( rangeRequest );
List<Facet> facets = facetManager.getFacets( facetingName ); //OK
facets = facetManager.getFacets( facetingName ); //Still OK
assertFacetCounts( facets, new int[] { 6 } );
facetManager.getFacetGroup( facetingName ).selectFacets( facets.get( 0 ) ); //narrow search on facet
facets = facetManager.getFacets( facetingName ); //Exception...
assertFacetCounts( facets, new int[] { 6 } );
}
Change the methods loadTestData and getAnnotatedClasses to:
Code:
public void loadTestData(Session session) {
Transaction tx = session.beginTransaction();
for ( int i = 0; i < albums.length; i++ ) {
Cd cd = new Cd( albums[i], albumPrices[i], releaseDates[i] );
session.save( cd );
}
for ( int i = 0; i < fruits.length; i++ ) {
Fruit fruit = new Fruit( fruits[i], fruitPrices[i] );
session.save( fruit );
}
for ( int i = 0; i < horsePowers.length; i++){
Truck truck = new Truck(horsePowers[i]);
session.save( truck );
}
tx.commit();
session.clear();
}
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Cd.class,
Fruit.class,
Truck.class
};
}
Add Truck.java:
Code:
/*
* for testing facetting with Integer fields
*/
package org.hibernate.search.test.query.facet;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.NumericField;
@Entity
@Indexed
public class Truck {
@Id
@GeneratedValue
private int id;
@Field(index = Index.UN_TOKENIZED)
@NumericField
private Integer horsePower;
private Truck() {
}
public Truck(Integer horsePower) {
this.horsePower = horsePower;
}
public int getId() {
return id;
}
public Integer getHorsePower() {
return horsePower;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "Truck" );
sb.append( "{id=" ).append( id );
sb.append( ", horsePower='" ).append( horsePower );
sb.append( '}' );
return sb.toString();
}
}
Add this line to org.hibernate.search.test.query.facet.AbstractFacetTest
Code:
public static final Integer[] horsePowers = { 200, 400, 600, null, 1300, null, 730 };
My guess, entities with null values are included in the result of the below facet, so probably
null is evaluated to be 'lower' than any valid Integer. The lowest value found is probably used somewhere to set some range during
.selectFacets( facetName ). This range is then used again when calling
.getFacets( facetingName ), which tries to obtain the type from a null value -> AssertionFailure: Unsupported range type.