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;
}
}
};
}
};
}
};
}
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) + ')';
}
};
}
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))) + ")";
}
};
}
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();
}
};
}
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);
}
}
};
}
Aggregations