use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class FloatPayloadValueSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
Fields fields = readerContext.reader().fields();
final Terms terms = fields.terms(indexedField);
FunctionValues defaultValues = defaultValueSource.getValues(context, readerContext);
// copied the bulk of this from TFValueSource - TODO: this is a very repeated pattern - base-class this advance logic stuff?
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, PostingsEnum.ALL);
} else {
docs = null;
}
} else {
docs = null;
}
if (docs == null) {
// dummy PostingsEnum so floatVal() can work
// when would this be called? if field/val did not match? this is called for every doc? create once and cache?
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 defaultValues.floatVal(doc);
}
// a match!
int freq = docs.freq();
int numPayloadsSeen = 0;
float currentScore = 0;
for (int i = 0; i < freq; i++) {
docs.nextPosition();
BytesRef payload = docs.getPayload();
if (payload != null) {
float payloadVal = decoder.decode(atDoc, docs.startOffset(), docs.endOffset(), payload);
// payloadFunction = null represents "first"
if (payloadFunction == null)
return payloadVal;
currentScore = payloadFunction.currentScore(doc, indexedField, docs.startOffset(), docs.endOffset(), numPayloadsSeen, currentScore, payloadVal);
numPayloadsSeen++;
}
}
return (numPayloadsSeen > 0) ? payloadFunction.docScore(doc, indexedField, numPayloadsSeen, currentScore) : defaultValues.floatVal(doc);
} catch (IOException e) {
throw new RuntimeException("caught exception in function " + description() + " : doc=" + doc, e);
}
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class IntersectsRPTVerifyQuery method createWeight.
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
final Map valueSourceContext = ValueSource.newContext(searcher);
return new ConstantScoreWeight(this, boost) {
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
// Compute approx & exact
final IntersectsDifferentiatingQuery.IntersectsDifferentiatingVisitor result = intersectsDiffQuery.compute(context);
if (result.approxDocIdSet == null) {
return null;
}
final DocIdSetIterator approxDISI = result.approxDocIdSet.iterator();
if (approxDISI == null) {
return null;
}
final DocIdSetIterator exactIterator;
if (result.exactDocIdSet != null) {
// If both sets are the same, there's nothing to verify; we needn't return a TwoPhaseIterator
if (result.approxDocIdSet == result.exactDocIdSet) {
return new ConstantScoreScorer(this, score(), approxDISI);
}
exactIterator = result.exactDocIdSet.iterator();
assert exactIterator != null;
} else {
exactIterator = null;
}
final FunctionValues predFuncValues = predicateValueSource.getValues(valueSourceContext, context);
final TwoPhaseIterator twoPhaseIterator = new TwoPhaseIterator(approxDISI) {
@Override
public boolean matches() throws IOException {
final int doc = approxDISI.docID();
if (exactIterator != null) {
if (exactIterator.docID() < doc) {
exactIterator.advance(doc);
}
if (exactIterator.docID() == doc) {
return true;
}
}
return predFuncValues.boolVal(doc);
}
@Override
public float matchCost() {
// TODO: use cost of exactIterator.advance() and predFuncValues.boolVal()
return 100;
}
};
return new ConstantScoreScorer(this, score(), twoPhaseIterator);
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class BBoxSimilarityValueSource method getValues.
@Override
public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException {
final FunctionValues shapeValues = bboxValueSource.getValues(context, readerContext);
return new DoubleDocValues(this) {
@Override
public double doubleVal(int doc) throws IOException {
//? limit to Rect or call getBoundingBox()? latter would encourage bad practice
final Rectangle rect = (Rectangle) shapeValues.objectVal(doc);
return rect == null ? 0 : score(rect, null);
}
@Override
public boolean exists(int doc) throws IOException {
return shapeValues.exists(doc);
}
@Override
public Explanation explain(int doc) throws IOException {
final Rectangle rect = (Rectangle) shapeValues.objectVal(doc);
if (rect == null) {
return Explanation.noMatch("no rect");
}
AtomicReference<Explanation> explanation = new AtomicReference<>();
score(rect, explanation);
return explanation.get();
}
};
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class VersionInfo method getMaxVersionFromIndex.
/**
* Returns the highest version from the index, or 0L if no versions can be found in the index.
*/
public Long getMaxVersionFromIndex(IndexSearcher searcher) throws IOException {
String versionFieldName = versionField.getName();
log.debug("Refreshing highest value of {} for {} version buckets from index", versionFieldName, buckets.length);
long maxVersionInIndex = 0L;
// if indexed, then we have terms to get the max from
if (versionField.indexed()) {
LeafReader leafReader = SlowCompositeReaderWrapper.wrap(searcher.getIndexReader());
Terms versionTerms = leafReader.terms(versionFieldName);
Long max = (versionTerms != null) ? LegacyNumericUtils.getMaxLong(versionTerms) : null;
if (max != null) {
maxVersionInIndex = max.longValue();
log.debug("Found MAX value {} from Terms for {} in index", maxVersionInIndex, versionFieldName);
} else {
log.debug("No terms found for {}, cannot seed version bucket highest value from index", versionFieldName);
}
} else {
ValueSource vs = versionField.getType().getValueSource(versionField, null);
Map funcContext = ValueSource.newContext(searcher);
vs.createWeight(funcContext, searcher);
// TODO: multi-thread this
for (LeafReaderContext ctx : searcher.getTopReaderContext().leaves()) {
int maxDoc = ctx.reader().maxDoc();
FunctionValues fv = vs.getValues(funcContext, ctx);
for (int doc = 0; doc < maxDoc; doc++) {
long v = fv.longVal(doc);
maxVersionInIndex = Math.max(v, maxVersionInIndex);
}
}
}
return maxVersionInIndex;
}
use of org.apache.lucene.queries.function.FunctionValues in project lucene-solr by apache.
the class VersionInfo method getVersionFromIndex.
/**
* Returns the latest version from the index, searched by the given id (bytes) as seen from the realtime searcher.
* Returns null if no document can be found in the index for the given id.
*/
public Long getVersionFromIndex(BytesRef idBytes) {
// TODO: we could cache much of this and invalidate during a commit.
// TODO: most DocValues classes are threadsafe - expose which.
RefCounted<SolrIndexSearcher> newestSearcher = ulog.uhandler.core.getRealtimeSearcher();
try {
SolrIndexSearcher searcher = newestSearcher.get();
long lookup = searcher.lookupId(idBytes);
// this means the doc doesn't exist in the index yet
if (lookup < 0)
return null;
ValueSource vs = versionField.getType().getValueSource(versionField, null);
Map context = ValueSource.newContext(searcher);
vs.createWeight(context, searcher);
FunctionValues fv = vs.getValues(context, searcher.getTopReaderContext().leaves().get((int) (lookup >> 32)));
long ver = fv.longVal((int) lookup);
return ver;
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading version from index", e);
} finally {
if (newestSearcher != null) {
newestSearcher.decref();
}
}
}
Aggregations