I'm trying to perform a search for an integer value. I have annotated the param with @Field like this:
@Field(name = "confirmedCount", index = UN_TOKENIZED, store = Store.YES)
public int getConfirmedCount() {
return stuff.size();
}
I then perform a range search with luke:
confirmedCount:[5 TO 100]
the result that I get back is empty. I then try:
confirmedCount:[1 TO 2]
The result is:
name confirmedCount
b 1
a 1
d 19
c 2
So my question is: Why do I get this response and how do I solve it? I use hibernate search 3.0.1.GA
Ok I guess the answer to my question is RTFM! The documentation clearly states:
Numbers are converted into their string representation. Note that numbers cannot be compared by Lucene (ie used in ranged queries) out of the box: they have to be padded
So we need to implement a class bridge:
public class PaddedIntegerBridge implements StringBridge {
private int PADDING = 5;
public String objectToString(Object object) {
String rawInteger = ( (Integer) object ).toString();
if (rawInteger.length() > PADDING)
throw new IllegalArgumentException( "Try to pad on a number too big" );
StringBuilder paddedInteger = new StringBuilder( );
for ( int padIndex = rawInteger.length() ; padIndex < PADDING ; padIndex++ ) {
paddedInteger.append('0');
}
return paddedInteger.append( rawInteger ).toString();
}
}
Then we need to annotate the field so that it get indexed:
@Field(name = "confirmedCount", index = UN_TOKENIZED, store = Store.YES, bridge = @FieldBridge(impl = PaddedIntegerBridge.class))
public int getConfirmedCount() {
return stuff.size();
}
Then in my searches I just need to make use of this bridge when creating a query and voila it works =)
Some testing with Luke:
confirmedCount:[00005 TO 00100]
name confirmedCount
g 00006
d 00019