Hi,
try:
Code:
Query rootQuery = queryBuilder.bool()
.must(queryBuilder.range().onField('price').above(10000l).createQuery())
.must(queryBuilder.range().onField('price').below(20000l).createQuery())
.createQuery();
Numeric range queries in Lucene are type specific (int, double, long, etc). You are storing the values as long, but when you create the query you are using effectively ints which will give you a mismatch.
Here is a working unit test I used to test this:
Code:
public class NumericBigDecimalBridgeTest extends SearchTestCase {
public void testNumericFieldWithBigDecimals() throws Exception {
Session session = openSession();
Transaction tx = session.beginTransaction();
// create entities
Item item = new Item();
item.setPrice( new BigDecimal( 154.34 ) );
session.save( item );
tx.commit();
tx = session.beginTransaction();
FullTextSession fullTextSession = Search.getFullTextSession( session );
QueryBuilder queryBuilder = fullTextSession.getSearchFactory()
.buildQueryBuilder()
.forEntity( Item.class )
.get();
Query rootQuery = queryBuilder.bool()
.must( queryBuilder.range().onField( "price" ).above( 10000l ).createQuery() )
.must( queryBuilder.range().onField( "price" ).below( 20000l ).createQuery() )
.createQuery();
List<Item> resultList = ( List<Item> ) fullTextSession.createFullTextQuery( rootQuery, Item.class ).list();
assertNotNull( resultList );
assertTrue( resultList.size() == 1 );
tx.commit();
session.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Item.class };
}
@Entity
@Indexed
public static class Item {
@Id
@GeneratedValue
private int id;
@Field
@NumericField
@FieldBridge(impl = BigDecimalNumericFieldBridge.class)
private BigDecimal price;
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public int getId() {
return id;
}
}
public static class BigDecimalNumericFieldBridge extends NumericFieldBridge {
private static final BigDecimal storeFactor = BigDecimal.valueOf( 100 );
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
if ( value != null ) {
BigDecimal decimalValue = ( BigDecimal ) value;
long indexedValue = decimalValue.multiply( storeFactor ).longValue();
luceneOptions.addNumericFieldToDocument( name, indexedValue, document );
}
}
@Override
public Object get(String name, Document document) {
String fromLucene = document.get( name );
BigDecimal storedBigDecimal = new BigDecimal( fromLucene );
return storedBigDecimal.divide( storeFactor );
}
}
}
--Hardy