Search in sources :

Example 1 with ValueSourceScorer

use of org.apache.lucene.queries.function.ValueSourceScorer in project lucene-solr by apache.

the class EnumFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final NumericDocValues arr = DocValues.getNumeric(readerContext.reader(), field);
    return new IntDocValues(this) {

        final MutableValueInt val = new MutableValueInt();

        int lastDocID;

        private int getValueForDoc(int doc) throws IOException {
            if (doc < lastDocID) {
                throw new AssertionError("docs were sent out-of-order: lastDocID=" + lastDocID + " vs doc=" + doc);
            }
            lastDocID = doc;
            int curDocID = arr.docID();
            if (doc > curDocID) {
                curDocID = arr.advance(doc);
            }
            if (doc == curDocID) {
                return (int) arr.longValue();
            } else {
                return 0;
            }
        }

        @Override
        public int intVal(int doc) throws IOException {
            return getValueForDoc(doc);
        }

        @Override
        public String strVal(int doc) throws IOException {
            Integer intValue = intVal(doc);
            return intValueToStringValue(intValue);
        }

        @Override
        public boolean exists(int doc) throws IOException {
            getValueForDoc(doc);
            return arr.docID() == doc;
        }

        @Override
        public ValueSourceScorer getRangeScorer(LeafReaderContext readerContext, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
            Integer lower = stringValueToIntValue(lowerVal);
            Integer upper = stringValueToIntValue(upperVal);
            if (lower == null) {
                lower = Integer.MIN_VALUE;
            } else {
                if (!includeLower && lower < Integer.MAX_VALUE)
                    lower++;
            }
            if (upper == null) {
                upper = Integer.MAX_VALUE;
            } else {
                if (!includeUpper && upper > Integer.MIN_VALUE)
                    upper--;
            }
            final int ll = lower;
            final int uu = upper;
            return new ValueSourceScorer(readerContext, this) {

                @Override
                public boolean matches(int doc) throws IOException {
                    if (!exists(doc))
                        return false;
                    int val = intVal(doc);
                    return val >= ll && val <= uu;
                }
            };
        }

        @Override
        public ValueFiller getValueFiller() {
            return new ValueFiller() {

                private final MutableValueInt mval = new MutableValueInt();

                @Override
                public MutableValue getValue() {
                    return mval;
                }

                @Override
                public void fillValue(int doc) throws IOException {
                    mval.value = intVal(doc);
                    mval.exists = arr.docID() == doc;
                }
            };
        }
    };
}
Also used : NumericDocValues(org.apache.lucene.index.NumericDocValues) ValueSourceScorer(org.apache.lucene.queries.function.ValueSourceScorer) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) MutableValueInt(org.apache.lucene.util.mutable.MutableValueInt) IntDocValues(org.apache.lucene.queries.function.docvalues.IntDocValues)

Example 2 with ValueSourceScorer

use of org.apache.lucene.queries.function.ValueSourceScorer in project lucene-solr by apache.

the class DoubleDocValues method getRangeScorer.

@Override
public ValueSourceScorer getRangeScorer(LeafReaderContext readerContext, String lowerVal, String upperVal, boolean includeLower, boolean includeUpper) {
    double lower, upper;
    if (lowerVal == null) {
        lower = Double.NEGATIVE_INFINITY;
    } else {
        lower = Double.parseDouble(lowerVal);
    }
    if (upperVal == null) {
        upper = Double.POSITIVE_INFINITY;
    } else {
        upper = Double.parseDouble(upperVal);
    }
    final double l = lower;
    final double u = upper;
    if (includeLower && includeUpper) {
        return new ValueSourceScorer(readerContext, this) {

            @Override
            public boolean matches(int doc) throws IOException {
                if (!exists(doc))
                    return false;
                double docVal = doubleVal(doc);
                return docVal >= l && docVal <= u;
            }
        };
    } else if (includeLower && !includeUpper) {
        return new ValueSourceScorer(readerContext, this) {

            @Override
            public boolean matches(int doc) throws IOException {
                if (!exists(doc))
                    return false;
                double docVal = doubleVal(doc);
                return docVal >= l && docVal < u;
            }
        };
    } else if (!includeLower && includeUpper) {
        return new ValueSourceScorer(readerContext, this) {

            @Override
            public boolean matches(int doc) throws IOException {
                if (!exists(doc))
                    return false;
                double docVal = doubleVal(doc);
                return docVal > l && docVal <= u;
            }
        };
    } else {
        return new ValueSourceScorer(readerContext, this) {

            @Override
            public boolean matches(int doc) throws IOException {
                if (!exists(doc))
                    return false;
                double docVal = doubleVal(doc);
                return docVal > l && docVal < u;
            }
        };
    }
}
Also used : ValueSourceScorer(org.apache.lucene.queries.function.ValueSourceScorer) IOException(java.io.IOException)

Aggregations

ValueSourceScorer (org.apache.lucene.queries.function.ValueSourceScorer)2 IOException (java.io.IOException)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 NumericDocValues (org.apache.lucene.index.NumericDocValues)1 IntDocValues (org.apache.lucene.queries.function.docvalues.IntDocValues)1 MutableValueInt (org.apache.lucene.util.mutable.MutableValueInt)1