use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class IndexFingerprint method getFingerprint.
public static IndexFingerprint getFingerprint(SolrIndexSearcher searcher, LeafReaderContext ctx, Long maxVersion) throws IOException {
SchemaField versionField = VersionInfo.getAndCheckVersionField(searcher.getSchema());
ValueSource vs = versionField.getType().getValueSource(versionField, null);
Map funcContext = ValueSource.newContext(searcher);
vs.createWeight(funcContext, searcher);
IndexFingerprint f = new IndexFingerprint();
f.maxVersionSpecified = maxVersion;
f.maxDoc = ctx.reader().maxDoc();
f.numDocs = ctx.reader().numDocs();
int maxDoc = ctx.reader().maxDoc();
Bits liveDocs = ctx.reader().getLiveDocs();
FunctionValues fv = vs.getValues(funcContext, ctx);
for (int doc = 0; doc < maxDoc; doc++) {
if (liveDocs != null && !liveDocs.get(doc))
continue;
long v = fv.longVal(doc);
f.maxVersionEncountered = Math.max(v, f.maxVersionEncountered);
if (v <= f.maxVersionSpecified) {
f.maxInHash = Math.max(v, f.maxInHash);
f.versionsHash += Hash.fmix64(v);
f.numVersions++;
}
}
return f;
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class BytesRefFieldSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
final FieldInfo fieldInfo = readerContext.reader().getFieldInfos().fieldInfo(field);
// TODO: do it cleaner?
if (fieldInfo != null && fieldInfo.getDocValuesType() == DocValuesType.BINARY) {
final BinaryDocValues binaryValues = DocValues.getBinary(readerContext.reader(), field);
return new FunctionValues() {
int lastDocID = -1;
private BytesRef getValueForDoc(int doc) throws IOException {
if (doc < lastDocID) {
throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc);
}
lastDocID = doc;
int curDocID = binaryValues.docID();
if (doc > curDocID) {
curDocID = binaryValues.advance(doc);
}
if (doc == curDocID) {
return binaryValues.binaryValue();
} else {
return null;
}
}
@Override
public boolean exists(int doc) throws IOException {
return getValueForDoc(doc) != null;
}
@Override
public boolean bytesVal(int doc, BytesRefBuilder target) throws IOException {
BytesRef value = getValueForDoc(doc);
if (value == null || value.length == 0) {
return false;
} else {
target.copyBytes(value);
return true;
}
}
public String strVal(int doc) throws IOException {
final BytesRefBuilder bytes = new BytesRefBuilder();
return bytesVal(doc, bytes) ? bytes.get().utf8ToString() : null;
}
@Override
public Object objectVal(int doc) throws IOException {
return strVal(doc);
}
@Override
public String toString(int doc) throws IOException {
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) throws IOException {
BytesRef value = getValueForDoc(doc);
mval.exists = value != null;
mval.value.clear();
if (value != null) {
mval.value.copyBytes(value);
}
}
};
}
};
} else {
return new DocTermsIndexDocValues(this, readerContext, field) {
@Override
protected String toTerm(String readableValue) {
return readableValue;
}
@Override
public Object objectVal(int doc) throws IOException {
return strVal(doc);
}
@Override
public String toString(int doc) throws IOException {
return description() + '=' + strVal(doc);
}
};
}
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class ScaleFloatFunction method createScaleInfo.
private ScaleInfo createScaleInfo(Map context, LeafReaderContext readerContext) throws IOException {
final List<LeafReaderContext> leaves = ReaderUtil.getTopLevelContext(readerContext).leaves();
float minVal = Float.POSITIVE_INFINITY;
float maxVal = Float.NEGATIVE_INFINITY;
for (LeafReaderContext leaf : leaves) {
int maxDoc = leaf.reader().maxDoc();
FunctionValues vals = source.getValues(context, leaf);
for (int i = 0; i < maxDoc; i++) {
if (!vals.exists(i)) {
continue;
}
float val = vals.floatVal(i);
if ((Float.floatToRawIntBits(val) & (0xff << 23)) == 0xff << 23) {
// which don't make sense to factor into the scale function
continue;
}
if (val < minVal) {
minVal = val;
}
if (val > maxVal) {
maxVal = val;
}
}
}
if (minVal == Float.POSITIVE_INFINITY) {
// must have been an empty index
minVal = maxVal = 0;
}
ScaleInfo scaleInfo = new ScaleInfo();
scaleInfo.minVal = minVal;
scaleInfo.maxVal = maxVal;
context.put(ScaleFloatFunction.this, scaleInfo);
return scaleInfo;
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class ScaleFloatFunction method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
ScaleInfo scaleInfo = (ScaleInfo) context.get(ScaleFloatFunction.this);
if (scaleInfo == null) {
scaleInfo = createScaleInfo(context, readerContext);
}
final float scale = (scaleInfo.maxVal - scaleInfo.minVal == 0) ? 0 : (max - min) / (scaleInfo.maxVal - scaleInfo.minVal);
final float minSource = scaleInfo.minVal;
final float maxSource = scaleInfo.maxVal;
final FunctionValues vals = source.getValues(context, readerContext);
return new FloatDocValues(this) {
@Override
public boolean exists(int doc) throws IOException {
return vals.exists(doc);
}
@Override
public float floatVal(int doc) throws IOException {
return (vals.floatVal(doc) - minSource) * scale + min;
}
@Override
public String toString(int doc) throws IOException {
return "scale(" + vals.toString(doc) + ",toMin=" + min + ",toMax=" + max + ",fromMin=" + minSource + ",fromMax=" + maxSource + ")";
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class MultiFunction method toString.
public static String toString(String name, FunctionValues[] valsArr, int doc) throws IOException {
StringBuilder sb = new StringBuilder();
sb.append(name).append('(');
boolean firstTime = true;
for (FunctionValues vals : valsArr) {
if (firstTime) {
firstTime = false;
} else {
sb.append(',');
}
sb.append(vals.toString(doc));
}
sb.append(')');
return sb.toString();
}
Aggregations