Search in sources :

Example 36 with Explanation

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

the class IBSimilarity method explain.

@Override
protected void explain(List<Explanation> subs, BasicStats stats, int doc, float freq, float docLen) {
    if (stats.getBoost() != 1.0f) {
        subs.add(Explanation.match(stats.getBoost(), "boost"));
    }
    Explanation normExpl = normalization.explain(stats, freq, docLen);
    Explanation lambdaExpl = lambda.explain(stats);
    subs.add(normExpl);
    subs.add(lambdaExpl);
    subs.add(distribution.explain(stats, normExpl.getValue(), lambdaExpl.getValue()));
}
Also used : Explanation(org.apache.lucene.search.Explanation)

Example 37 with Explanation

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

the class BM25Similarity method explainScore.

private Explanation explainScore(int doc, Explanation freq, BM25Stats stats, NumericDocValues norms, float[] lengthCache) throws IOException {
    Explanation boostExpl = Explanation.match(stats.boost, "boost");
    List<Explanation> subs = new ArrayList<>();
    if (boostExpl.getValue() != 1.0f)
        subs.add(boostExpl);
    subs.add(stats.idf);
    Explanation tfNormExpl = explainTFNorm(doc, freq, stats, norms, lengthCache);
    subs.add(tfNormExpl);
    return Explanation.match(boostExpl.getValue() * stats.idf.getValue() * tfNormExpl.getValue(), "score(doc=" + doc + ",freq=" + freq + "), product of:", subs);
}
Also used : Explanation(org.apache.lucene.search.Explanation) ArrayList(java.util.ArrayList)

Example 38 with Explanation

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

the class TFIDFSimilarity 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> subs = new ArrayList<>();
    for (final TermStatistics stat : termStats) {
        Explanation idfExplain = idfExplain(collectionStats, stat);
        subs.add(idfExplain);
        idf += idfExplain.getValue();
    }
    return Explanation.match((float) idf, "idf(), sum of:", subs);
}
Also used : Explanation(org.apache.lucene.search.Explanation) ArrayList(java.util.ArrayList) TermStatistics(org.apache.lucene.search.TermStatistics)

Example 39 with Explanation

use of org.apache.lucene.search.Explanation in project elasticsearch by elastic.

the class SearchHit method buildExplanation.

private void buildExplanation(XContentBuilder builder, Explanation explanation) throws IOException {
    builder.startObject();
    builder.field(Fields.VALUE, explanation.getValue());
    builder.field(Fields.DESCRIPTION, explanation.getDescription());
    Explanation[] innerExps = explanation.getDetails();
    if (innerExps != null) {
        builder.startArray(Fields.DETAILS);
        for (Explanation exp : innerExps) {
            buildExplanation(builder, exp);
        }
        builder.endArray();
    }
    builder.endObject();
}
Also used : Lucene.readExplanation(org.elasticsearch.common.lucene.Lucene.readExplanation) Explanation(org.apache.lucene.search.Explanation) Lucene.writeExplanation(org.elasticsearch.common.lucene.Lucene.writeExplanation)

Example 40 with Explanation

use of org.apache.lucene.search.Explanation in project elasticsearch by elastic.

the class QueryRescorer method explain.

@Override
public Explanation explain(int topLevelDocId, SearchContext context, RescoreSearchContext rescoreContext, Explanation sourceExplanation) throws IOException {
    QueryRescoreContext rescore = (QueryRescoreContext) rescoreContext;
    ContextIndexSearcher searcher = context.searcher();
    if (sourceExplanation == null) {
        // this should not happen but just in case
        return Explanation.noMatch("nothing matched");
    }
    // TODO: this isn't right?  I.e., we are incorrectly pretending all first pass hits were rescored?  If the requested docID was
    // beyond the top rescoreContext.window() in the first pass hits, we don't rescore it now?
    Explanation rescoreExplain = searcher.explain(rescore.query(), topLevelDocId);
    float primaryWeight = rescore.queryWeight();
    Explanation prim;
    if (sourceExplanation.isMatch()) {
        prim = Explanation.match(sourceExplanation.getValue() * primaryWeight, "product of:", sourceExplanation, Explanation.match(primaryWeight, "primaryWeight"));
    } else {
        prim = Explanation.noMatch("First pass did not match", sourceExplanation);
    }
    // we should add QueryRescorer.explainCombine to Lucene?
    if (rescoreExplain != null && rescoreExplain.isMatch()) {
        float secondaryWeight = rescore.rescoreQueryWeight();
        Explanation sec = Explanation.match(rescoreExplain.getValue() * secondaryWeight, "product of:", rescoreExplain, Explanation.match(secondaryWeight, "secondaryWeight"));
        QueryRescoreMode scoreMode = rescore.scoreMode();
        return Explanation.match(scoreMode.combine(prim.getValue(), sec.getValue()), scoreMode + " of:", prim, sec);
    } else {
        return prim;
    }
}
Also used : Explanation(org.apache.lucene.search.Explanation) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher)

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