org.apache.lucene.search.FieldComparatorSource Java Examples

The following examples show how to use org.apache.lucene.search.FieldComparatorSource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SortableBinaryField.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public BinarySortField(final String field, final boolean reverse) {
  super(field, new FieldComparatorSource() {
    @Override
    public FieldComparator.TermOrdValComparator newComparator
        (final String fieldname, final int numHits, final int sortPos, final boolean reversed) {
      return new FieldComparator.TermOrdValComparator(numHits, fieldname);
    }}, reverse);
}
 
Example #2
Source File: ClusteringKeyMapper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Lucene {@link SortField} array for sorting documents/rows according to the column family name.
 *
 * @return A Lucene {@link SortField} array for sorting documents/rows according to the column family name.
 */
public SortField[] sortFields() {
    return new SortField[]{new SortField(FIELD_NAME, new FieldComparatorSource() {
        @Override
        public FieldComparator<?> newComparator(String field,
                                                int hits,
                                                int sort,
                                                boolean reversed) throws IOException {
            return new ClusteringKeySorter(ClusteringKeyMapper.this, hits, field);
        }
    })};
}
 
Example #3
Source File: SortSymbolVisitor.java    From crate with Apache License 2.0 5 votes vote down vote up
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 non `null` sentinel values, the nullSentinel already implies reverse+nullsFirst logic
                // 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);
}
 
Example #4
Source File: SortSymbolVisitor.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * generate a SortField from a Reference symbol.
 * <p>
 * the implementation is similar to how ES 2.4 SortParseElement worked
 */
@Override
public SortField visitReference(final Reference symbol, final SortSymbolContext context) {
    // can't use the SortField(fieldName, type) constructor
    // because values are saved using docValues and therefore they're indexed in lucene as binary and not
    // with the reference valueType.
    // this is why we use a custom comparator source with the same logic as ES

    ColumnIdent columnIdent = symbol.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), symbol, context);
    }
    if (symbol.isColumnStoreDisabled()) {
        return customSortField(symbol.toString(), symbol, context);
    }

    MappedFieldType fieldType = fieldTypeLookup.get(columnIdent.fqn());
    if (fieldType == null) {
        FieldComparatorSource fieldComparatorSource = new NullFieldComparatorSource(NullSentinelValues.nullSentinelForScoreDoc(
            symbol.valueType(),
            context.reverseFlag,
            context.nullFirst
        ));
        return new SortField(
            columnIdent.fqn(),
            fieldComparatorSource,
            context.reverseFlag);
    } else if (symbol.valueType().equals(DataTypes.IP)) {
        return customSortField(symbol.toString(), symbol, context);
    } else {
        return mappedSortField(
            symbol,
            fieldType,
            context.reverseFlag,
            NullValueOrder.fromFlag(context.nullFirst)
        );
    }
}