Example usage for org.apache.lucene.search Explanation Explanation

List of usage examples for org.apache.lucene.search Explanation Explanation

Introduction

In this page you can find the example usage for org.apache.lucene.search Explanation Explanation.

Prototype

Explanation

Source Link

Usage

From source file:com.browseengine.bobo.query.FacetBasedBoostScorerBuilder.java

License:Apache License

public Explanation explain(IndexReader indexReader, int docid, Explanation innerExplaination)
        throws IOException {
    if (!(indexReader instanceof BoboIndexReader))
        throw new IllegalArgumentException("IndexReader is not BoboIndexReader");
    BoboIndexReader reader = (BoboIndexReader) indexReader;

    Explanation exp = new Explanation();
    exp.setDescription("FacetBasedBoost");

    float boost = 1.0f;
    for (Map.Entry<String, Map<String, Float>> boostEntry : _boostMaps.entrySet()) {
        String facetName = boostEntry.getKey();
        FacetHandler<?> handler = reader.getFacetHandler(facetName);
        if (!(handler instanceof FacetScoreable))
            throw new IllegalArgumentException(facetName + " does not implement FacetScoreable");

        FacetScoreable facetScoreable = (FacetScoreable) handler;
        BoboDocScorer scorer = facetScoreable.getDocScorer(reader, _scoringFunctionFactory,
                boostEntry.getValue());/*  w w  w  . ja  v a2 s .c om*/
        float facetBoost = scorer.score(docid);

        Explanation facetExp = new Explanation();
        facetExp.setDescription(facetName);
        facetExp.setValue(facetBoost);
        facetExp.addDetail(scorer.explain(docid));
        boost *= facetBoost;
        exp.addDetail(facetExp);
    }
    exp.setValue(boost);
    exp.addDetail(innerExplaination);
    return exp;
}

From source file:com.browseengine.bobo.query.RecencyBoostScorerBuilder.java

License:Apache License

public Explanation explain(IndexReader reader, int doc, Explanation innerExplaination) throws IOException {
    if (reader instanceof BoboIndexReader) {
        BoboIndexReader boboReader = (BoboIndexReader) reader;
        Object dataObj = boboReader.getFacetData(_timeFacetName);
        if (dataObj instanceof FacetDataCache<?>) {
            FacetDataCache<Long> facetDataCache = (FacetDataCache<Long>) (boboReader
                    .getFacetData(_timeFacetName));
            final BigSegmentedArray orderArray = facetDataCache.orderArray;
            final TermLongList termList = (TermLongList) facetDataCache.valArray;
            final long now = System.currentTimeMillis();
            Explanation finalExpl = new Explanation();
            finalExpl.addDetail(innerExplaination);
            float rawScore = innerExplaination.getValue();
            long timeVal = termList.getPrimitiveValue(orderArray.get(doc));
            float timeScore = computeTimeFactor(timeVal);
            float finalScore = combineScores(timeScore, rawScore);
            finalExpl.setValue(finalScore);
            finalExpl.setDescription("final score = (time score: " + timeScore + ") * (raw score: " + rawScore
                    + "), timeVal: " + timeVal);
            return finalExpl;
        } else {//from  w  ww  .  ja  v a2 s.c  o m
            throw new IllegalStateException("underlying facet data must be of type FacetDataCache<Long>");
        }
    } else {
        throw new IllegalStateException("reader not instance of " + BoboIndexReader.class);
    }
}

From source file:com.browseengine.bobo.query.scoring.MultiplicativeFacetTermScoringFunction.java

License:Apache License

public Explanation explain(int df, float boost) {
    Explanation expl = new Explanation();
    expl.setValue(score(df, boost));// w w  w  .j  a va  2 s  .  c  o  m
    expl.setDescription("boost value of: " + boost);
    return expl;
}

From source file:com.browseengine.bobo.query.scoring.MultiplicativeFacetTermScoringFunction.java

License:Apache License

public Explanation explain(float... scores) {
    Explanation expl = new Explanation();
    float boost = 1.0f;
    for (float score : scores) {
        boost *= score;/*  ww w .  j  a va2  s.  com*/
    }
    expl.setValue(boost);
    expl.setDescription("product of: " + Arrays.toString(scores));
    return expl;
}

From source file:com.factweavers.elasticsearch.payloadscorefunction.PayloadScoringFunction.java

License:Apache License

@Override
public Explanation explainScore(int docId, Explanation subQueryScore) {
    Explanation exp = new Explanation();
    double score = score(docId, subQueryScore.getValue());
    exp.setValue(CombineFunction.toFloat(score));
    exp.setDescription(//  w  w  w . j  a  va2 s. c  o  m
            String.format(Locale.ROOT, "field value function: (Payload['%s']['%s'])", field, values));
    return exp;
}

From source file:com.xiaomi.linden.lucene.query.flexiblequery.FlexibleScoreModelStrategy.java

License:Apache License

public Explanation explain(Similarity similarity, Query query, int doc) throws IOException {
    Explanation expl = new Explanation();
    isExplain = true;// w w  w. jav  a2 s.  c  o m
    if (fieldExpls == null) {
        fieldExpls = new String[matchedMatrix.getFieldLength()];
        fieldScores = new Float[matchedMatrix.getFieldLength()];
    } else {
        Arrays.fill(fieldExpls, null);
        Arrays.fill(fieldScores, null);
    }
    if (termExpls == null) {
        termExpls = new String[matchedMatrix.getFieldLength()][matchedMatrix.getTermLength()];
        termScores = new Float[matchedMatrix.getFieldLength()][matchedMatrix.getTermLength()];
    } else {
        Arrays.fill(termExpls, null);
        Arrays.fill(termScores, null);
    }
    double score = computeScore();
    expl.setDescription(explanation != null ? explanation : "FlexibleWeight");
    for (int i = 0; i < getFieldLength(); ++i) {
        Explanation subExpl = new Explanation();
        int matched = 0;
        for (int j = 0; j < getTermLength(); ++j) {
            if (isMatched(i, j)) {
                Explanation result = new Explanation();
                if (termExpls[i][j] != null) {
                    result.setDescription(String.format("%s", termExpls[i][j]));
                    result.setValue(termScores[i][j] != null ? termScores[i][j] : getScore(i, j));
                } else {
                    result.setDescription(String.format("%.2f * %.2f * %.2f -- (%s %d)", getRawScore(i, j),
                            getFieldBoost(i), getTermBoost(j), text(i, j), position(i, j)));
                    result.setValue(getScore(i, j));
                }
                subExpl.addDetail(result);
                ++matched;
            }
        }
        if (fieldExpls[i] != null) {
            subExpl.setDescription(
                    String.format("%s        [FIELD:%s MATCHED:%d]", fieldExpls[i], field(i, 0), matched));
            subExpl.setValue(fieldScores[i] != null ? fieldScores[i] : 1);
        } else {
            subExpl.setDescription(String.format("FIELD:%s MATCHED:%d", field(i, 0), matched));
            subExpl.setValue(1);
        }
        expl.addDetail(subExpl);
    }
    expl.setValue((float) score);
    return expl;
}

From source file:de.unihildesheim.iw.lucene.search.FDRDefaultSimilarity.java

License:Open Source License

/**
 * Same as {@link TFIDFSimilarity#idfExplain(CollectionStatistics,
 * TermStatistics[])}, but uses {@link CollectionStatistics#docCount()}
 * instead of {@link CollectionStatistics#maxDoc()}.
 *
 * @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.
 * @see TFIDFSimilarity#idfExplain(CollectionStatistics, TermStatistics[])
 *///from   w w w.java  2 s  . c om
@SuppressWarnings("ObjectAllocationInLoop")
@Override
public Explanation idfExplain(final CollectionStatistics collectionStats, final TermStatistics termStats[]) {
    final long max = collectionStats.docCount();
    float idf = 0.0f;
    final Explanation exp = new Explanation();
    exp.setDescription("idf(), sum of:");
    for (final TermStatistics stat : termStats) {
        final long df = stat.docFreq();
        final float termIdf = idf(df, max);
        exp.addDetail(new Explanation(termIdf, "idf(docFreq=" + df + ", maxDocs=" + max + ')'));
        idf += termIdf;
    }
    exp.setValue(idf);
    return exp;
}

From source file:elhuyar.bilakit.SimilarityCLIRFactory.java

License:Open Source License

/**
 * Computes a score factor for a phrase.
 * /* w w w .  jav a  2s .  c o m*/
 * <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[]) {
    final long max = collectionStats.maxDoc();
    float idf = 0.0f;
    final Explanation exp = new Explanation();
    exp.setDescription("idf(), sum of:");
    for (final TermStatistics stat : termStats) {
        final long df = stat.docFreq();
        final float termIdf = idf(df, max);
        exp.addDetail(new Explanation(termIdf, "idf(docFreq=" + df + ", maxDocs=" + max + ")"));
        idf += termIdf;
    }
    exp.setValue(idf);
    return exp;
}

From source file:elhuyar.bilakit.SimilarityCLIRFactory.java

License:Open Source License

private Explanation explainScore(int doc, Explanation freq, IDFStats stats, NumericDocValues norms) {
    Explanation result = new Explanation();
    result.setDescription("score(doc=" + doc + ",freq=" + freq.getValue() + "), product of:");

    // explain query weight
    Explanation queryExpl = new Explanation();
    queryExpl.setDescription("queryWeight, product of:");

    Explanation boostExpl = new Explanation(stats.queryBoost, "boost");
    if (stats.queryBoost != 1.0f)
        queryExpl.addDetail(boostExpl);/*from w ww  . j  av  a 2 s  .  c om*/
    queryExpl.addDetail(stats.idf);

    Explanation queryNormExpl = new Explanation(stats.queryNorm, "queryNorm");
    queryExpl.addDetail(queryNormExpl);

    queryExpl.setValue(boostExpl.getValue() * stats.idf.getValue() * queryNormExpl.getValue());

    result.addDetail(queryExpl);

    // explain field weight
    Explanation fieldExpl = new Explanation();
    fieldExpl.setDescription("fieldWeight in " + doc + ", product of:");

    Explanation tfExplanation = new Explanation();
    tfExplanation.setValue(tf(freq.getValue()));
    tfExplanation.setDescription("tf(freq=" + freq.getValue() + "), with freq of:");
    tfExplanation.addDetail(freq);
    fieldExpl.addDetail(tfExplanation);
    fieldExpl.addDetail(stats.idf);

    Explanation fieldNormExpl = new Explanation();
    float fieldNorm = norms != null ? decodeNormValue(norms.get(doc)) : 1.0f;
    fieldNormExpl.setValue(fieldNorm);
    fieldNormExpl.setDescription("fieldNorm(doc=" + doc + ")");
    fieldExpl.addDetail(fieldNormExpl);

    fieldExpl.setValue(tfExplanation.getValue() * stats.idf.getValue() * fieldNormExpl.getValue());

    result.addDetail(fieldExpl);

    // combine them
    result.setValue(queryExpl.getValue() * fieldExpl.getValue());

    if (queryExpl.getValue() == 1.0f)
        return fieldExpl;

    return result;
}

From source file:eu.europeana.assets.service.ir.text.bm25f.scorer.BM25FBooleanScorer.java

License:Apache License

@Override
public Explanation explain(int doc) throws IOException {
    BM25FBooleanScorer s = new BM25FBooleanScorer(reader, terms, similarity, bmParams);

    if (s.advance(doc) != doc)
        return null;
    Explanation result = new Explanation();
    result.setDescription("Total");
    float value = 0f;
    for (int i = 0; i < s.termScorers.length; i++) {
        if (s.termScorers[i].docID() == doc) {
            value += s.termScorers[i].score();
            result.addDetail(s.termScorers[i].explain(doc));
        }// w  ww.  j  a  va 2 s. c  om
    }
    result.setValue(value);
    return result;
}