/** * Sort the results of a numeric range query if the query in this context * is a {@link NumericRangeQuery}, see {@link #numericRange(String, Number, Number)}, * Otherwise an {@link IllegalStateException} will be thrown. * * @param key the key to sort on. * @param reversed if the sort order should be reversed or not. {@code true} * for lowest first (ascending), {@code false} for highest first (descending) * @return a QueryContext with sorting by numeric value. */ public QueryContext sortNumeric( String key, boolean reversed ) { if ( !( queryOrQueryObject instanceof NumericRangeQuery ) ) { throw new IllegalStateException( "Not a numeric range query" ); } Number number = ((NumericRangeQuery)queryOrQueryObject).getMin(); number = number != null ? number : ((NumericRangeQuery)queryOrQueryObject).getMax(); SortField.Type fieldType = SortField.Type.INT; if ( number instanceof Long ) { fieldType = SortField.Type.LONG; } else if ( number instanceof Float ) { fieldType = SortField.Type.FLOAT; } else if ( number instanceof Double ) { fieldType = SortField.Type.DOUBLE; } sort( new Sort( new SortedNumericSortField( key, fieldType, reversed ) ) ); return this; }
private static SortField getSortField(Pager pager) { if (DOC_ID_FIELD_NAME.equals(pager.getSortby())) { return new SortedNumericSortField(DOC_ID_FIELD_NAME, LONG, pager.isDesc()); } else { return new SortField(pager.getSortby(), STRING, pager.isDesc()); } }
private QueryContext sort( QueryContext query, boolean reversed ) { return query.sort( new Sort( new SortedNumericSortField( FIELD, SortField.Type.LONG, reversed ) ) ); }