Search in sources :

Example 1 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 2 with FloatDocValues

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

the class DualFloatFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues aVals = a.getValues(context, readerContext);
    final FunctionValues bVals = b.getValues(context, readerContext);
    return new FloatDocValues(this) {

        @Override
        public float floatVal(int doc) throws IOException {
            return func(doc, aVals, bVals);
        }

        /** 
       * True if and only if <em>all</em> of the wrapped {@link FunctionValues} 
       * <code>exists</code> for the specified doc 
       */
        @Override
        public boolean exists(int doc) throws IOException {
            return MultiFunction.allExists(doc, aVals, bVals);
        }

        @Override
        public String toString(int doc) throws IOException {
            return name() + '(' + aVals.toString(doc) + ',' + bVals.toString(doc) + ')';
        }
    };
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues)

Example 3 with FloatDocValues

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

the class RangeMapFloatFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues vals = source.getValues(context, readerContext);
    final FunctionValues targets = target.getValues(context, readerContext);
    final FunctionValues defaults = (this.defaultVal == null) ? null : defaultVal.getValues(context, readerContext);
    return new FloatDocValues(this) {

        @Override
        public float floatVal(int doc) throws IOException {
            float val = vals.floatVal(doc);
            return (val >= min && val <= max) ? targets.floatVal(doc) : (defaultVal == null ? val : defaults.floatVal(doc));
        }

        @Override
        public String toString(int doc) throws IOException {
            return "map(" + vals.toString(doc) + ",min=" + min + ",max=" + max + ",target=" + targets.toString(doc) + ",defaultVal=" + (defaults == null ? "null" : (defaults.toString(doc))) + ")";
        }
    };
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues)

Example 4 with FloatDocValues

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

the class StringDistanceFunction method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    final FunctionValues str1DV = str1.getValues(context, readerContext);
    final FunctionValues str2DV = str2.getValues(context, readerContext);
    return new FloatDocValues(this) {

        @Override
        public float floatVal(int doc) throws IOException {
            String s1 = str1DV.strVal(doc);
            String s2 = str2DV.strVal(doc);
            if (null == s1 || null == s2) {
                // the only thing a missing value scores 1.0 with is another missing value
                return (s1 == s2) ? 1.0F : 0.0F;
            }
            return dist.getDistance(s1, s2);
        }

        @Override
        public boolean exists(int doc) throws IOException {
            return str1DV.exists(doc) && str2DV.exists(doc);
        }

        @Override
        public String toString(int doc) throws IOException {
            StringBuilder sb = new StringBuilder();
            sb.append("strdist").append('(');
            sb.append(str1DV.toString(doc)).append(',').append(str2DV.toString(doc)).append(", dist=").append(dist.getClass().getName());
            sb.append(')');
            return sb.toString();
        }
    };
}
Also used : FunctionValues(org.apache.lucene.queries.function.FunctionValues) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues)

Example 5 with FloatDocValues

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

the class TFValueSource method getValues.

@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
    Fields fields = readerContext.reader().fields();
    final Terms terms = fields.terms(indexedField);
    IndexSearcher searcher = (IndexSearcher) context.get("searcher");
    final TFIDFSimilarity similarity = IDFValueSource.asTFIDF(searcher.getSimilarity(true), indexedField);
    if (similarity == null) {
        throw new UnsupportedOperationException("requires a TFIDFSimilarity (such as ClassicSimilarity)");
    }
    return new FloatDocValues(this) {

        PostingsEnum docs;

        int atDoc;

        int lastDocRequested = -1;

        {
            reset();
        }

        public void reset() throws IOException {
            if (terms != null) {
                final TermsEnum termsEnum = terms.iterator();
                if (termsEnum.seekExact(indexedBytes)) {
                    docs = termsEnum.postings(null);
                } else {
                    docs = null;
                }
            } else {
                docs = null;
            }
            if (docs == null) {
                docs = new PostingsEnum() {

                    @Override
                    public int freq() {
                        return 0;
                    }

                    @Override
                    public int nextPosition() throws IOException {
                        return -1;
                    }

                    @Override
                    public int startOffset() throws IOException {
                        return -1;
                    }

                    @Override
                    public int endOffset() throws IOException {
                        return -1;
                    }

                    @Override
                    public BytesRef getPayload() throws IOException {
                        return null;
                    }

                    @Override
                    public int docID() {
                        return DocIdSetIterator.NO_MORE_DOCS;
                    }

                    @Override
                    public int nextDoc() {
                        return DocIdSetIterator.NO_MORE_DOCS;
                    }

                    @Override
                    public int advance(int target) {
                        return DocIdSetIterator.NO_MORE_DOCS;
                    }

                    @Override
                    public long cost() {
                        return 0;
                    }
                };
            }
            atDoc = -1;
        }

        @Override
        public float floatVal(int doc) {
            try {
                if (doc < lastDocRequested) {
                    // out-of-order access.... reset
                    reset();
                }
                lastDocRequested = doc;
                if (atDoc < doc) {
                    atDoc = docs.advance(doc);
                }
                if (atDoc > doc) {
                    // end, or because the next doc is after this doc.
                    return similarity.tf(0);
                }
                // a match!
                return similarity.tf(docs.freq());
            } catch (IOException e) {
                throw new RuntimeException("caught exception in function " + description() + " : doc=" + doc, e);
            }
        }
    };
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Terms(org.apache.lucene.index.Terms) IOException(java.io.IOException) TermsEnum(org.apache.lucene.index.TermsEnum) Fields(org.apache.lucene.index.Fields) FloatDocValues(org.apache.lucene.queries.function.docvalues.FloatDocValues) TFIDFSimilarity(org.apache.lucene.search.similarities.TFIDFSimilarity) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef)

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