Search in sources :

Example 31 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class ExpressionValueSource method explain.

@Override
public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
    Explanation[] explanations = new Explanation[variables.length];
    DoubleValues dv = getValues(ctx, DoubleValuesSource.constant(scoreExplanation.getValue()).getValues(ctx, null));
    if (dv.advanceExact(docId) == false) {
        return Explanation.noMatch(expression.sourceText);
    }
    int i = 0;
    for (DoubleValuesSource var : variables) {
        explanations[i++] = var.explain(ctx, docId, scoreExplanation);
    }
    return Explanation.match((float) dv.doubleValue(), expression.sourceText + ", computed from:", explanations);
}
Also used : Explanation(org.apache.lucene.search.Explanation) DoubleValues(org.apache.lucene.search.DoubleValues) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 32 with Explanation

use of org.apache.lucene.search.Explanation 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();
        }
    };
}
Also used : Explanation(org.apache.lucene.search.Explanation) DoubleDocValues(org.apache.lucene.queries.function.docvalues.DoubleDocValues) Rectangle(org.locationtech.spatial4j.shape.Rectangle) FunctionValues(org.apache.lucene.queries.function.FunctionValues) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 33 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class SpanWeight method explain.

@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
    SpanScorer scorer = scorer(context);
    if (scorer != null) {
        int newDoc = scorer.iterator().advance(doc);
        if (newDoc == doc) {
            float freq = scorer.sloppyFreq();
            SimScorer docScorer = similarity.simScorer(simWeight, context);
            Explanation freqExplanation = Explanation.match(freq, "phraseFreq=" + freq);
            Explanation scoreExplanation = docScorer.explain(doc, freqExplanation);
            return Explanation.match(scoreExplanation.getValue(), "weight(" + getQuery() + " in " + doc + ") [" + similarity.getClass().getSimpleName() + "], result of:", scoreExplanation);
        }
    }
    return Explanation.noMatch("no matching term");
}
Also used : Explanation(org.apache.lucene.search.Explanation) SimScorer(org.apache.lucene.search.similarities.Similarity.SimScorer)

Example 34 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class BM25Similarity method computeWeight.

@Override
public final SimWeight computeWeight(float boost, CollectionStatistics collectionStats, TermStatistics... termStats) {
    Explanation idf = termStats.length == 1 ? idfExplain(collectionStats, termStats[0]) : idfExplain(collectionStats, termStats);
    float avgdl = avgFieldLength(collectionStats);
    float[] oldCache = new float[256];
    float[] cache = new float[256];
    for (int i = 0; i < cache.length; i++) {
        oldCache[i] = k1 * ((1 - b) + b * OLD_LENGTH_TABLE[i] / avgdl);
        cache[i] = k1 * ((1 - b) + b * LENGTH_TABLE[i] / avgdl);
    }
    return new BM25Stats(collectionStats.field(), boost, idf, avgdl, oldCache, cache);
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Example 35 with Explanation

use of org.apache.lucene.search.Explanation in project lucene-solr by apache.

the class BM25Similarity method idfExplain.

/**
   * Computes a score factor for a phrase.
   * 
   * <p>
   * The default implementation sums the idf factor for
   * each term in the phrase.
   * 
   * @param collectionStats collection-level statistics
   * @param termStats term-level statistics for the terms in the phrase
   * @return an Explain object that includes both an idf 
   *         score factor for the phrase and an explanation 
   *         for each term.
   */
public Explanation idfExplain(CollectionStatistics collectionStats, TermStatistics[] termStats) {
    // sum into a double before casting into a float
    double idf = 0d;
    List<Explanation> details = new ArrayList<>();
    for (final TermStatistics stat : termStats) {
        Explanation idfExplain = idfExplain(collectionStats, stat);
        details.add(idfExplain);
        idf += idfExplain.getValue();
    }
    return Explanation.match((float) idf, "idf(), sum of:", details);
}
Also used : Explanation(org.apache.lucene.search.Explanation) ArrayList(java.util.ArrayList) TermStatistics(org.apache.lucene.search.TermStatistics)

Aggregations

Explanation (org.apache.lucene.search.Explanation)66 TermQuery (org.apache.lucene.search.TermQuery)15 Query (org.apache.lucene.search.Query)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)13 ArrayList (java.util.ArrayList)12 Term (org.apache.lucene.index.Term)10 IOException (java.io.IOException)8 BooleanQuery (org.apache.lucene.search.BooleanQuery)8 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)8 Directory (org.apache.lucene.store.Directory)8 Document (org.apache.lucene.document.Document)7 IndexReader (org.apache.lucene.index.IndexReader)7 TopDocs (org.apache.lucene.search.TopDocs)7 IndexWriter (org.apache.lucene.index.IndexWriter)6 DirectoryReader (org.apache.lucene.index.DirectoryReader)5 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)5 Collectors (java.util.stream.Collectors)4 IntStream (java.util.stream.IntStream)4 Store (org.apache.lucene.document.Field.Store)4 FunctionValues (org.apache.lucene.queries.function.FunctionValues)4