If you have string type field but you want to sort as number wise and apply rang query on that. you must have convert string field to number field but here without converting field you can implement rang query on field and apply sorting like as number.
Add or create custom @FieldBridge on field.
@Field(index=Index.YES,analyze=Analyze.NO,store=Store.YES)
@FieldBridge(impl=StringToNumberBridge.class)
private String currentCtc;
Suppose you want to search currentCtc field as number but it's string field. You can implement Rang Query on currentCtc field.
public class StringToNumberBridge implements TwoWayStringBridge {
Logger logger=Logger.getLogger(StringToNumberBridge.class);
public static String PADDING_PROPERTY = "padding";
private int padding = 7; //default
public String objectToString(Object object) {
try {
if(object!=null)
{
String rawInteger = ((String) object).toString();
String decimalPoint="";
if(rawInteger.matches("\\d*\\.\\d+"))
{
decimalPoint=rawInteger.substring(rawInteger.indexOf("."));
rawInteger=rawInteger.substring(0,rawInteger.indexOf("."));
System.out.println(decimalPoint);
System.out.println(rawInteger);
}
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).append(decimalPoint).toString();
}
else {
return "";
}
//return object.toString();
}
catch (Exception e) {
logger.error("NumberFormateException:::::::::::"+e);
return null;
}
}
public Object stringToObject(String stringValue) {
return Double.valueOf(stringValue);
}
}
You have indexing string field with padding same like number.
Apply Rang Query on String field but it's work like as number.
booleanQuery.must(qb.range().onField("currentCtc").below(25000).createQuery());
Apply Sorting on string field as number
SortField field = new SortField("currentCtc".trim(), SortField.DOUBLE,true);