public FunctionValues getValues(Map context, AtomicReaderContext readerContext) throws IOException {
final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field);
// To be sorted or not to be sorted, that is the question
// TODO: do it cleaner?
if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) {
final BinaryDocValues binaryValues = FieldCache.DEFAULT.getTerms(readerContext.reader(), field, true);
final Bits docsWithField = FieldCache.DEFAULT.getDocsWithField(readerContext.reader(), field);
return new FunctionValues() {
@Override
public boolean exists(int doc) {
return docsWithField.get(doc);
}
@Override
public boolean bytesVal(int doc, BytesRef target) {
target.copyBytes(binaryValues.get(doc));
return target.length > 0;
}
public String strVal(int doc) {
final BytesRef bytes = new BytesRef();
return bytesVal(doc, bytes)
? bytes.utf8ToString()
: null;
}
@Override
public Object objectVal(int doc) {
return strVal(doc);
}
@Override
public String toString(int doc) {
return description() + '=' + strVal(doc);
}
@Override
public ValueFiller getValueFiller() {
return new ValueFiller() {
private final MutableValueStr mval = new MutableValueStr();
@Override
public MutableValue getValue() {
return mval;
}
@Override
public void fillValue(int doc) {
mval.exists = docsWithField.get(doc);
mval.value.length = 0;
mval.value.copyBytes(binaryValues.get(doc));
}
};
}
};