use of org.apache.lucene.search.FieldComparatorSource in project crate by crate.
the class SortSymbolVisitor method visitReference.
/**
* generate a SortField from a Reference symbol.
* <p>
* the implementation is similar to how ES 2.4 SortParseElement worked
*/
@Override
public SortField visitReference(Reference ref, final SortSymbolContext context) {
// default to the `NullFieldComparatorSource` - leading to `null` values.
if (ref.column().isChildOf(DocSysColumns.DOC)) {
ref = (Reference) DocReferences.inverseSourceLookup(ref);
}
ColumnIdent columnIdent = ref.column();
if (DocSysColumns.SCORE.equals(columnIdent)) {
return !context.reverseFlag ? SORT_SCORE_REVERSE : SORT_SCORE;
}
if (DocSysColumns.RAW.equals(columnIdent) || DocSysColumns.ID.equals(columnIdent)) {
return customSortField(DocSysColumns.nameForLucene(columnIdent), ref, context);
}
if (!ref.hasDocValues()) {
return customSortField(ref.toString(), ref, context);
}
MappedFieldType fieldType = fieldTypeLookup.get(columnIdent.fqn());
if (fieldType == null) {
FieldComparatorSource fieldComparatorSource = new NullFieldComparatorSource(NullSentinelValues.nullSentinelForScoreDoc(ref.valueType(), context.reverseFlag, context.nullFirst));
return new SortField(columnIdent.fqn(), fieldComparatorSource, context.reverseFlag);
} else if (ref.valueType().equals(DataTypes.IP) || ref.valueType().id() == BitStringType.ID) {
return customSortField(ref.toString(), ref, context);
} else {
return mappedSortField(ref, fieldType, context.reverseFlag, NullValueOrder.fromFlag(context.nullFirst));
}
}
use of org.apache.lucene.search.FieldComparatorSource in project crate by crate.
the class SortSymbolVisitor method customSortField.
private SortField customSortField(String name, final Symbol symbol, final SortSymbolContext context) {
InputFactory.Context<? extends LuceneCollectorExpression<?>> inputContext = docInputFactory.getCtx(context.txnCtx);
final Input<?> input = inputContext.add(symbol);
final List<? extends LuceneCollectorExpression<?>> expressions = inputContext.expressions();
final CollectorContext collectorContext = context.context;
final boolean nullFirst = context.nullFirst;
return new SortField(name, new FieldComparatorSource() {
@Override
public FieldComparator<?> newComparator(String fieldName, int numHits, int sortPos, boolean reversed) {
for (int i = 0; i < expressions.size(); i++) {
expressions.get(i).startCollect(collectorContext);
}
@SuppressWarnings("unchecked") DataType<Object> dataType = (DataType<Object>) symbol.valueType();
Object nullSentinel = NullSentinelValues.nullSentinel(dataType, NullValueOrder.fromFlag(nullFirst), reversed);
return new InputFieldComparator(numHits, expressions, input, // for `null` sentinels we need to have a comparator that can deal with that
nullSentinel == null ? nullFirst ^ reversed ? Comparator.nullsFirst(dataType) : Comparator.nullsLast(dataType) : dataType, nullSentinel);
}
}, context.reverseFlag);
}
Aggregations