Search in sources :

Example 36 with FunctionValues

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;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) ValueSource(org.apache.lucene.queries.function.ValueSource) Bits(org.apache.lucene.util.Bits) FunctionValues(org.apache.lucene.queries.function.FunctionValues) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 37 with FunctionValues

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);
            }
        };
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DocTermsIndexDocValues(org.apache.lucene.queries.function.docvalues.DocTermsIndexDocValues) MutableValueStr(org.apache.lucene.util.mutable.MutableValueStr) FunctionValues(org.apache.lucene.queries.function.FunctionValues) FieldInfo(org.apache.lucene.index.FieldInfo) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 38 with FunctionValues

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;
}
Also used : LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Example 39 with FunctionValues

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 + ")";
        }
    };
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues)

Example 40 with FunctionValues

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();
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues)

Aggregations

FunctionValues (org.apache.lucene.queries.function.FunctionValues)45 ValueSource (org.apache.lucene.queries.function.ValueSource)10 DoubleDocValues (org.apache.lucene.queries.function.docvalues.DoubleDocValues)9 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)7 Map (java.util.Map)6 LeafReader (org.apache.lucene.index.LeafReader)6 NumericDocValues (org.apache.lucene.index.NumericDocValues)6 IOException (java.io.IOException)5 FloatDocValues (org.apache.lucene.queries.function.docvalues.FloatDocValues)5 Explanation (org.apache.lucene.search.Explanation)4 ArrayList (java.util.ArrayList)3 Terms (org.apache.lucene.index.Terms)3 BoolDocValues (org.apache.lucene.queries.function.docvalues.BoolDocValues)3 BytesRef (org.apache.lucene.util.BytesRef)3 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)3 Point (org.locationtech.spatial4j.shape.Point)3 Rectangle (org.locationtech.spatial4j.shape.Rectangle)3 Date (java.util.Date)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 SimpleFloatFunction (org.apache.lucene.queries.function.valuesource.SimpleFloatFunction)2