Search in sources :

Example 6 with FloatDocValues

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

the class TrieFloatField method getSingleValueSource.

@Override
protected ValueSource getSingleValueSource(SortedSetSelector.Type choice, SchemaField f) {
    return new SortedSetFieldSource(f.getName(), choice) {

        @Override
        public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
            // needed for nested anon class ref
            SortedSetFieldSource thisAsSortedSetFieldSource = this;
            SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field);
            SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector);
            return new FloatDocValues(thisAsSortedSetFieldSource) {

                private int lastDocID;

                private boolean setDoc(int docID) throws IOException {
                    if (docID < lastDocID) {
                        throw new IllegalArgumentException("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
                    }
                    if (docID > view.docID()) {
                        return docID == view.advance(docID);
                    } else {
                        return docID == view.docID();
                    }
                }

                @Override
                public float floatVal(int doc) throws IOException {
                    if (setDoc(doc)) {
                        BytesRef bytes = view.binaryValue();
                        assert bytes.length > 0;
                        return NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(bytes));
                    } else {
                        return 0F;
                    }
                }

                @Override
                public boolean exists(int doc) throws IOException {
                    return setDoc(doc);
                }

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

                        private final MutableValueFloat mval = new MutableValueFloat();

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

                        @Override
                        public void fillValue(int doc) throws IOException {
                            if (setDoc(doc)) {
                                mval.exists = true;
                                mval.value = NumericUtils.sortableIntToFloat(LegacyNumericUtils.prefixCodedToInt(view.binaryValue()));
                            } else {
                                mval.exists = false;
                                mval.value = 0F;
                            }
                        }
                    };
                }
            };
        }
    };
}
Also used : SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) MutableValueFloat(org.apache.lucene.util.mutable.MutableValueFloat) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues) Map(java.util.Map) SortedSetFieldSource(org.apache.lucene.queries.function.valuesource.SortedSetFieldSource) SortedDocValues(org.apache.lucene.index.SortedDocValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 7 with FloatDocValues

use of org.apache.lucene.queries.function.docvalues.FloatDocValues 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 8 with FloatDocValues

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

the class FloatFieldSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final NumericDocValues arr = getNumericDocValues(context, readerContext);
    return new FloatDocValues(this) {

        int lastDocID;

        private float 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 = arr.docID();
            if (doc > curDocID) {
                curDocID = arr.advance(doc);
            }
            if (doc == curDocID) {
                return Float.intBitsToFloat((int) arr.longValue());
            } else {
                return 0f;
            }
        }

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

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

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

                private final MutableValueFloat mval = new MutableValueFloat();

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

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

Example 9 with FloatDocValues

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

the class NormValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    IndexSearcher searcher = (IndexSearcher) context.get("searcher");
    final TFIDFSimilarity similarity = IDFValueSource.asTFIDF(searcher.getSimilarity(true), field);
    if (similarity == null) {
        throw new UnsupportedOperationException("requires a TFIDFSimilarity (such as ClassicSimilarity)");
    }
    // Only works if the contribution of the tf is 1 when the freq is 1 and contribution of the idf
    // is 1 when docCount == docFreq == 1
    final SimWeight simWeight = similarity.computeWeight(1f, new CollectionStatistics(field, 1, 1, 1, 1), new TermStatistics(new BytesRef("bogus"), 1, 1));
    final SimScorer simScorer = similarity.simScorer(simWeight, readerContext);
    return new FloatDocValues(this) {

        int lastDocID = -1;

        @Override
        public float floatVal(int docID) throws IOException {
            if (docID < lastDocID) {
                throw new AssertionError("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
            }
            lastDocID = docID;
            return simScorer.score(docID, 1f);
        }
    };
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SimWeight(org.apache.lucene.search.similarities.Similarity.SimWeight) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues) SimScorer(org.apache.lucene.search.similarities.Similarity.SimScorer) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity) TermStatistics(org.apache.lucene.search.TermStatistics) BytesRef(org.apache.lucene.util.BytesRef) CollectionStatistics(org.apache.lucene.search.CollectionStatistics)

Example 10 with FloatDocValues

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

the class ConstDateSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    return new FloatDocValues(this) {

        @Override
        public float floatVal(int doc) {
            return getFloat();
        }

        @Override
        public int intVal(int doc) {
            return getInt();
        }

        @Override
        public long longVal(int doc) {
            return getLong();
        }

        @Override
        public double doubleVal(int doc) {
            return getDouble();
        }

        @Override
        public String toString(int doc) {
            return description();
        }

        @Override
        public Object objectVal(int doc) {
            return new Date(longVal(doc));
        }

        @SuppressWarnings("deprecation")
        @Override
        public String strVal(int doc) {
            return Instant.ofEpochMilli(longVal(doc)).toString();
        }

        @Override
        public boolean boolVal(int doc) {
            return getFloat() != 0.0f;
        }

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

                private final MutableValueDate mval = new MutableValueDate();

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

                @Override
                public void fillValue(int doc) {
                    mval.value = longVal(doc);
                    mval.exists = true;
                }
            };
        }
    };
}
Also used : MutableValueDate(org.apache.lucene.util.mutable.MutableValueDate) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues) Date(java.util.Date) MutableValueDate(org.apache.lucene.util.mutable.MutableValueDate)

Aggregations

FloatDocValues (org.apache.lucene.queries.function.docvalues.FloatDocValues)10 FunctionValues (org.apache.lucene.queries.function.FunctionValues)5 BytesRef (org.apache.lucene.util.BytesRef)4 IOException (java.io.IOException)2 Fields (org.apache.lucene.index.Fields)2 PostingsEnum (org.apache.lucene.index.PostingsEnum)2 Terms (org.apache.lucene.index.Terms)2 TermsEnum (org.apache.lucene.index.TermsEnum)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 TFIDFSimilarity (org.apache.lucene.search.similarities.TFIDFSimilarity)2 MutableValueFloat (org.apache.lucene.util.mutable.MutableValueFloat)2 Date (java.util.Date)1 Map (java.util.Map)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 NumericDocValues (org.apache.lucene.index.NumericDocValues)1 SortedDocValues (org.apache.lucene.index.SortedDocValues)1 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)1 SortedSetFieldSource (org.apache.lucene.queries.function.valuesource.SortedSetFieldSource)1 CollectionStatistics (org.apache.lucene.search.CollectionStatistics)1 TermStatistics (org.apache.lucene.search.TermStatistics)1