下面列出了怎么用org.apache.lucene.search.FieldComparatorSource的API类实例代码及写法,或者点击链接到github查看源代码。
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);
}
/**
* 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);
}
})};
}
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);
}
/**
* 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)
);
}
}