Java Code Examples for org.apache.lucene.search.Explanation#match()

The following examples show how to use org.apache.lucene.search.Explanation#match() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicModelIne.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Explanation explain(BasicStats stats, double tfn, double aeTimes1pTfn) {
  double F = stats.getTotalTermFreq();
  double N = stats.getNumberOfDocuments();
  double ne = N * (1 - Math.pow((N - 1) / N, F));
  Explanation explNe = Explanation.match((float) ne,
      "ne, computed as N * (1 - Math.pow((N - 1) / N, F)) from:",
      Explanation.match((float) F,
          "F, total number of occurrences of term across all docs"),
      Explanation.match((float) N,
          "N, total number of documents with field"));

  return Explanation.match(
      (float) (score(stats, tfn, aeTimes1pTfn) * (1 + tfn) / aeTimes1pTfn),
      getClass().getSimpleName() + ", computed as " +
          "tfn * log2((N + 1) / (ne + 0.5)) from:",
      Explanation.match((float) tfn, "tfn, normalized term frequency"),
      explNe);
}
 
Example 2
Source File: GlobalOrdinalsWithScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
  SortedDocValues values = DocValues.getSorted(context.reader(), joinField);
  if (values == null) {
    return Explanation.noMatch("Not a match");
  }
  if (values.advance(doc) != doc) {
    return Explanation.noMatch("Not a match");
  }

  int segmentOrd = values.ordValue();
  BytesRef joinValue = values.lookupOrd(segmentOrd);

  int ord;
  if (globalOrds != null) {
    ord = (int) globalOrds.getGlobalOrds(context.ord).get(segmentOrd);
  } else {
    ord = segmentOrd;
  }
  if (collector.match(ord) == false) {
    return Explanation.noMatch("Not a match, join value " + Term.toString(joinValue));
  }

  float score = collector.score(ord);
  return Explanation.match(score, "A match, join value " + Term.toString(joinValue));
}
 
Example 3
Source File: ToParentBlockJoinQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public Explanation explain(LeafReaderContext context, Weight childWeight) throws IOException {
  int prevParentDoc = parentBits.prevSetBit(parentApproximation.docID() - 1);
  int start = context.docBase + prevParentDoc + 1; // +1 b/c prevParentDoc is previous parent doc
  int end = context.docBase + parentApproximation.docID() - 1; // -1 b/c parentDoc is parent doc

  Explanation bestChild = null;
  int matches = 0;
  for (int childDoc = start; childDoc <= end; childDoc++) {
    Explanation child = childWeight.explain(context, childDoc - context.docBase);
    if (child.isMatch()) {
      matches++;
      if (bestChild == null || child.getValue().floatValue() > bestChild.getValue().floatValue()) {
        bestChild = child;
      }
    }
  }

  return Explanation.match(score(), String.format(Locale.ROOT,
      "Score based on %d child docs in range from %d to %d, best match:", matches, start, end), bestChild
  );
}
 
Example 4
Source File: DependentTermQueryBuilder.java    From querqy with Apache License 2.0 6 votes vote down vote up
@Override
public Explanation explain(final LeafReaderContext context, final int doc) throws IOException {

    TermScorer scorer = scorer(context);
    if (scorer != null) {
        int newDoc = scorer.iterator().advance(doc);
        if (newDoc == doc) {
            float freq = scorer.freq();
            LeafSimScorer docScorer = new LeafSimScorer(simScorer, context.reader(), getTerm().field(), true);
            Explanation freqExplanation = Explanation.match(freq, "freq, occurrences of term within document");
            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");
}
 
Example 5
Source File: PositionMatchScorer.java    From elasticsearch-position-similarity with Apache License 2.0 6 votes vote down vote up
Explanation explain(int docID) {
    List<Explanation> explanations = new ArrayList<>();

    float totalScore = 0.0f;

    Set<Term> terms = new HashSet<>();
    weight.extractTerms(terms);

    for (Term term : terms) {
        Explanation termExplanation = explainTerm(docID, term);
        explanations.add(termExplanation);
        totalScore += termExplanation.getValue().floatValue();
    }

    return Explanation.match(
        totalScore,
        String.format("score(doc=%d), sum of:", docID),
        explanations
    );
}
 
Example 6
Source File: PayloadScoreQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
  PayloadSpanScorer scorer = (PayloadSpanScorer)scorer(context);
  if (scorer == null || scorer.iterator().advance(doc) != doc)
    return Explanation.noMatch("No match");

  scorer.score();  // force freq calculation
  Explanation payloadExpl = scorer.getPayloadExplanation();

  if (includeSpanScore) {
    SpanWeight innerWeight = ((PayloadSpanWeight) scorer.getWeight()).innerWeight;
    Explanation innerExpl = innerWeight.explain(context, doc);
    return Explanation.match(scorer.scoreCurrentDoc(), "PayloadSpanQuery, product of:", innerExpl, payloadExpl);
  }

  return scorer.getPayloadExplanation();
}
 
Example 7
Source File: AxiomaticF3EXP.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected Explanation tfExplain(BasicStats stats, double freq, double docLen){
  return Explanation.match((float) tf(stats, freq, docLen),
      "tf, term frequency computed as 1 + log(1 + log(freq)) from:",
      Explanation.match((float) freq,
          "freq, number of occurrences of term in the document"));
}
 
Example 8
Source File: IntervalScoreFunction.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(String interval, float weight, float sloppyFreq) {
  float score = scorer(weight).score(sloppyFreq, 1L);
  return Explanation.match(score,
      "Saturation function on interval frequency, computed as w * S / (S + k) from:",
      Explanation.match(weight, "w, weight of this function"),
      Explanation.match(pivot, "k, pivot feature value that would give a score contribution equal to w/2"),
      Explanation.match(sloppyFreq, "S, the sloppy frequency of the interval query " + interval));
}
 
Example 9
Source File: FunctionScoreQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
  Explanation inner = query.explain(ctx, docId, scoreExplanation);
  if (inner.isMatch() == false) {
    return inner;
  }
  return Explanation.match(boost, "Matched boosting query " + query.toString());
}
 
Example 10
Source File: ClassicSimilarity.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Explanation idfExplain(CollectionStatistics collectionStats, TermStatistics termStats) {
  final long df = termStats.docFreq();
  final long docCount = collectionStats.docCount();
  final float idf = idf(df, docCount);
  return Explanation.match(idf, "idf, computed as log((docCount+1)/(docFreq+1)) + 1 from:",
      Explanation.match(df, "docFreq, number of documents containing term"),
      Explanation.match(docCount, "docCount, total number of documents with field"));
}
 
Example 11
Source File: BM25Similarity.java    From lucene4ir with Apache License 2.0 5 votes vote down vote up
private Explanation explainScore(int doc, Explanation freq, BM25Stats stats, NumericDocValues norms) {
  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);
  subs.add(tfNormExpl);
  return Explanation.match(
      boostExpl.getValue() * stats.idf.getValue() * tfNormExpl.getValue(),
      "score(doc="+doc+",freq="+freq+"), product of:", subs);
}
 
Example 12
Source File: NormalizationH2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(BasicStats stats, double tf, double len) {
  return Explanation.match(
      (float) tfn(stats, tf, len),
      getClass().getSimpleName()
          + ", computed as tf * log2(1 + c * avgfl / fl) from:",
      Explanation.match((float) tf,
          "tf, number of occurrences of term in the document"),
      Explanation.match(c,
          "c, hyper-parameter"),
      Explanation.match((float) stats.getAvgFieldLength(),
          "avgfl, average length of field across all documents"),
      Explanation.match((float) len, "fl, field length of the document"));
}
 
Example 13
Source File: LMDirichletSimilarity.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected Explanation explain(
    BasicStats stats, Explanation freq, double docLen) {
  List<Explanation> subs = new ArrayList<>();
  explain(subs, stats, freq.getValue().doubleValue(), docLen);

  return Explanation.match(
      (float) score(stats, freq.getValue().doubleValue(), docLen),
      "score(" + getClass().getSimpleName() + ", freq=" +
          freq.getValue() +"), computed as boost * " +
          "(term weight + document norm) from:",
      subs);
}
 
Example 14
Source File: AfterEffectB.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public final Explanation explain(BasicStats stats, double tfn) {
  return Explanation.match(
      (float) (scoreTimes1pTfn(stats) / (1 + tfn)),
      getClass().getSimpleName()
          + ", computed as (F + 1) / (n * (tfn + 1)) from:",
      Explanation.match((float) tfn, "tfn, normalized term frequency"),
      Explanation.match(stats.getTotalTermFreq(),
          "F, total number of occurrences of term across all documents + 1"),
      Explanation.match(stats.getDocFreq(),
          "n, number of documents containing term + 1"),
      Explanation.match((float) tfn, "tfn, normalized term frequency"));
}
 
Example 15
Source File: FieldFeatureTFExtractor.java    From ltr4l with Apache License 2.0 5 votes vote down vote up
@Override
public Explanation explain(int target) throws IOException {
  int current = pe.docID();
  if(current < target){
    current = pe.advance(target);
  }
  if(current == target){
    int freq = pe.freq();
    return Explanation.match(freq, "freq: " + freq);
  }
  else{
    return Explanation.noMatch("no matching terms");
  }
}
 
Example 16
Source File: ReciprocalDoubleValuesSource.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
  Explanation expl = input.explain(ctx, docId, scoreExplanation);
  return Explanation.match(recip(expl.getValue().doubleValue()),
      distToEdge + " / (v + " + distToEdge + "), computed from:", expl);
}
 
Example 17
Source File: Normalization.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public Explanation explain(BasicStats stats, double tf, double len) {
  return Explanation.match(1, "no normalization");
}
 
Example 18
Source File: BM25Similarity.java    From lucene4ir with Apache License 2.0 3 votes vote down vote up
/**
 * 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[]) {
  final long docCount = collectionStats.docCount() == -1 ? collectionStats.maxDoc() : collectionStats.docCount();
  float idf = 0.0f;
  List<Explanation> details = new ArrayList<>();
  for (final TermStatistics stat : termStats ) {
    final long df = stat.docFreq();
    final float termIdf = idf(df, docCount);
    details.add(Explanation.match(termIdf, "idf(docFreq=" + df + ", docCount=" + docCount + ")"));
    idf += termIdf;
  }
  return Explanation.match(idf, "idf(), sum of:", details);
}
 
Example 19
Source File: BM25Similarity.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Computes a score factor for a simple term and returns an explanation
 * for that score factor.
 * 
 * <p>
 * The default implementation uses:
 * 
 * <pre class="prettyprint">
 * idf(docFreq, docCount);
 * </pre>
 * 
 * Note that {@link CollectionStatistics#docCount()} is used instead of
 * {@link org.apache.lucene.index.IndexReader#numDocs() IndexReader#numDocs()} because also 
 * {@link TermStatistics#docFreq()} is used, and when the latter 
 * is inaccurate, so is {@link CollectionStatistics#docCount()}, and in the same direction.
 * In addition, {@link CollectionStatistics#docCount()} does not skew when fields are sparse.
 *   
 * @param collectionStats collection-level statistics
 * @param termStats term-level statistics for the term
 * @return an Explain object that includes both an idf score factor 
           and an explanation for the term.
 */
public Explanation idfExplain(CollectionStatistics collectionStats, TermStatistics termStats) {
  final long df = termStats.docFreq();
  final long docCount = collectionStats.docCount();
  final float idf = idf(df, docCount);
  return Explanation.match(idf, "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
      Explanation.match(df, "n, number of documents containing term"),
      Explanation.match(docCount, "N, total number of documents with field"));
}
 
Example 20
Source File: SimilarityBase.java    From lucene-solr with Apache License 2.0 3 votes vote down vote up
/**
 * Explains the score. The implementation here provides a basic explanation
 * in the format <em>score(name-of-similarity, doc=doc-id,
 * freq=term-frequency), computed from:</em>, and
 * attaches the score (computed via the {@link #score(BasicStats, double, double)}
 * method) and the explanation for the term frequency. Subclasses content with
 * this format may add additional details in
 * {@link #explain(List, BasicStats, double, double)}.
 *  
 * @param stats the corpus level statistics.
 * @param freq the term frequency and its explanation.
 * @param docLen the document length.
 * @return the explanation.
 */
protected Explanation explain(
    BasicStats stats, Explanation freq, double docLen) {
  List<Explanation> subs = new ArrayList<>();
  explain(subs, stats, freq.getValue().floatValue(), docLen);
  
  return Explanation.match(
      (float) score(stats, freq.getValue().floatValue(), docLen),
      "score(" + getClass().getSimpleName() + ", freq=" + freq.getValue() +"), computed from:",
      subs);
}