Search in sources :

Example 1 with FieldComparatorSource

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));
    }
}
Also used : ColumnIdent(io.crate.metadata.ColumnIdent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) FieldComparatorSource(org.apache.lucene.search.FieldComparatorSource)

Example 2 with FieldComparatorSource

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);
}
Also used : InputFactory(io.crate.expression.InputFactory) DocInputFactory(io.crate.execution.engine.collect.DocInputFactory) SortField(org.apache.lucene.search.SortField) SortedSetSortField(org.apache.lucene.search.SortedSetSortField) SortedNumericSortField(org.apache.lucene.search.SortedNumericSortField) FieldComparatorSource(org.apache.lucene.search.FieldComparatorSource) FieldComparator(org.apache.lucene.search.FieldComparator) DataType(io.crate.types.DataType) CollectorContext(io.crate.expression.reference.doc.lucene.CollectorContext)

Aggregations

FieldComparatorSource (org.apache.lucene.search.FieldComparatorSource)2 SortField (org.apache.lucene.search.SortField)2 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)2 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)2 DocInputFactory (io.crate.execution.engine.collect.DocInputFactory)1 InputFactory (io.crate.expression.InputFactory)1 CollectorContext (io.crate.expression.reference.doc.lucene.CollectorContext)1 ColumnIdent (io.crate.metadata.ColumnIdent)1 DataType (io.crate.types.DataType)1 FieldComparator (org.apache.lucene.search.FieldComparator)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1