Java Code Examples for org.apache.lucene.search.similarities.Similarity.SimScorer

The following examples show how to use org.apache.lucene.search.similarities.Similarity.SimScorer. These examples are extracted from open source projects. 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 Project: lucene-solr   Source File: NormValueSource.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FunctionValues getValues(Map<Object, Object> context, LeafReaderContext readerContext) throws IOException {
  IndexSearcher searcher = (IndexSearcher)context.get("searcher");
  final TFIDFSimilarity similarity = IDFValueSource.asTFIDF(searcher.getSimilarity(), field);
  if (similarity == null) {
    throw new UnsupportedOperationException("requires a TFIDFSimilarity (such as ClassicSimilarity)");
  }
  // Only works if the contribution of the tf is 1 when the freq is 1 and contribution of the idf
  // is 1 when docCount == docFreq == 1
  final SimScorer simScorer = similarity.scorer(1f,
      new CollectionStatistics(field, 1, 1, 1, 1),
      new TermStatistics(new BytesRef("bogus"), 1, 1));
  final LeafSimScorer leafSimScorer = new LeafSimScorer(simScorer, readerContext.reader(), field, true);
  
  return new FloatDocValues(this) {
    int lastDocID = -1;
    @Override
    public float floatVal(int docID) throws IOException {
      if (docID < lastDocID) {
        throw new AssertionError("docs out of order: lastDocID=" + lastDocID + " docID=" + docID);
      }
      lastDocID = docID;
      return leafSimScorer.score(docID, 1f);
    }
  };
}
 
Example 2
Source Project: lucene-solr   Source File: ExactPhraseMatcher.java    License: Apache License 2.0 6 votes vote down vote up
ExactPhraseMatcher(PhraseQuery.PostingsAndFreq[] postings, ScoreMode scoreMode, SimScorer scorer, float matchCost) {
  super(matchCost);

  final DocIdSetIterator approximation = ConjunctionDISI.intersectIterators(Arrays.stream(postings).map(p -> p.postings).collect(Collectors.toList()));
  final ImpactsSource impactsSource = mergeImpacts(Arrays.stream(postings).map(p -> p.impacts).toArray(ImpactsEnum[]::new));

  if (scoreMode == ScoreMode.TOP_SCORES) {
    this.approximation = this.impactsApproximation = new ImpactsDISI(approximation, impactsSource, scorer);
  } else {
    this.approximation = approximation;
    this.impactsApproximation = new ImpactsDISI(approximation, impactsSource, scorer);
  }

  List<PostingsAndPosition> postingsAndPositions = new ArrayList<>();
  for(PhraseQuery.PostingsAndFreq posting : postings) {
    postingsAndPositions.add(new PostingsAndPosition(posting.postings, posting.position));
  }
  this.postings = postingsAndPositions.toArray(new PostingsAndPosition[postingsAndPositions.size()]);
}
 
Example 3
Source Project: lucene-solr   Source File: MultiNormsLeafSimScorer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sole constructor: Score documents of {@code reader} with {@code scorer}.
 *
 */
MultiNormsLeafSimScorer(SimScorer scorer, LeafReader reader, Collection<FieldAndWeight> normFields, boolean needsScores) throws IOException {
  this.scorer = Objects.requireNonNull(scorer);
  if (needsScores) {
    final List<NumericDocValues> normsList = new ArrayList<>();
    final List<Float> weightList = new ArrayList<>();
    for (FieldAndWeight field : normFields) {
      NumericDocValues norms = reader.getNormValues(field.field);
      if (norms != null) {
        normsList.add(norms);
        weightList.add(field.weight);
      }
    }
    if (normsList.isEmpty()) {
      norms = null;
    } else if (normsList.size() == 1) {
      norms = normsList.get(0);
    } else {
      final NumericDocValues[] normsArr = normsList.toArray(new NumericDocValues[0]);
      final float[] weightArr = new float[normsList.size()];
      for (int i = 0; i < weightList.size(); i++) {
        weightArr[i] = weightList.get(i);
      }
      norms = new MultiFieldNormValues(normsArr, weightArr);
    }
  } else {
    norms = null;
  }
}
 
Example 4
Source Project: lucene-solr   Source File: MaxScoreCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sole constructor.
 */
public MaxScoreCache(ImpactsSource impactsSource, SimScorer scorer) {
  this.impactsSource = impactsSource;
  this.scorer = scorer;
  maxScoreCache = new float[0];
  maxScoreCacheUpTo = new int[0];
}
 
Example 5
Source Project: lucene-solr   Source File: ImpactsDISI.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Sole constructor.
 * @param in            wrapped iterator
 * @param impactsSource source of impacts
 * @param scorer        scorer
 */
public ImpactsDISI(DocIdSetIterator in, ImpactsSource impactsSource, SimScorer scorer) {
  this.in = in;
  this.impactsSource = impactsSource;
  this.maxScoreCache = new MaxScoreCache(impactsSource, scorer);
  this.globalMaxScore = scorer.score(Float.MAX_VALUE, 1L);
}
 
Example 6
Source Project: lucene-solr   Source File: FeatureField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
SimScorer scorer(float weight) {
  return new SimScorer() {
    @Override
    public float score(float freq, long norm) {
      return (float) (weight * Math.log(scalingFactor + decodeFeatureValue(freq)));
    }
  };
}
 
Example 7
Source Project: lucene-solr   Source File: FeatureField.java    License: Apache License 2.0 5 votes vote down vote up
@Override
SimScorer scorer(float weight) {
  return new SimScorer() {
    @Override
    public float score(float freq, long norm) {
      float f = decodeFeatureValue(freq);
      // should be f^a / (f^a + k^a) but we rewrite it to
      // 1 - k^a / (f + k^a) to make sure it doesn't decrease
      // with f in spite of rounding
      return (float) (weight * (1 - pivotPa / (Math.pow(f, a) + pivotPa)));
    }
  };
}
 
Example 8
Source Project: lucene-solr   Source File: TestFeatureField.java    License: Apache License 2.0 5 votes vote down vote up
private void doTestSimScorer(SimScorer s) {
  float maxScore = s.score(Float.MAX_VALUE, 1);
  assertTrue(Float.isFinite(maxScore)); // used to compute max scores
  // Test that the score doesn't decrease with freq
  for (int freq = 2; freq < 65536; ++freq) {
    assertTrue(s.score(freq - 1, 1L) <= s.score(freq, 1L));
  }
  assertTrue(s.score(65535, 1L) <= maxScore);
}
 
Example 9
Source Project: pyramid   Source File: CustomSpanWeight.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PhraseCountScorer scorer(LeafReaderContext context) throws IOException {
  final Spans spans = getSpans(context, Postings.POSITIONS);
  if (spans == null) {
    return null;
  }
  final SimScorer docScorer = getSimScorer(context);
  return new PhraseCountScorer(this, spans, docScorer, weightedCount);
}
 
Example 10
Source Project: Elasticsearch   Source File: AllTermQuery.java    License: Apache License 2.0 4 votes vote down vote up
AllTermScorer(Weight weight, PostingsEnum postings, Similarity.SimScorer docScorer) {
    super(weight);
    this.postings = postings;
    this.docScorer = docScorer;
}
 
Example 11
Source Project: lucene-solr   Source File: LeafSimScorer.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Sole constructor: Score documents of {@code reader} with {@code scorer}.
 */
public LeafSimScorer(SimScorer scorer, LeafReader reader, String field, boolean needsScores) throws IOException {
  this.scorer = Objects.requireNonNull(scorer);
  norms = needsScores ? reader.getNormValues(field) : null;
}
 
Example 12
Source Project: lucene-solr   Source File: LeafSimScorer.java    License: Apache License 2.0 4 votes vote down vote up
/** Return the wrapped {@link SimScorer}. */
public SimScorer getSimScorer() {
  return scorer;
}
 
Example 13
Source Project: mtas   Source File: MtasSpanPositionQuery.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SimScorer getSimScorer(LeafReaderContext context) {
  return new MtasSimScorer();
}
 
Example 14
Source Project: mtas   Source File: MtasSpanMatchNoneQuery.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SimScorer getSimScorer(LeafReaderContext context) {
  return new MtasSimScorer();
}
 
Example 15
Source Project: mtas   Source File: MtasSpanMatchAllQuery.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public SimScorer getSimScorer(LeafReaderContext context) {
  return new MtasSimScorer();
}
 
Example 16
Source Project: pyramid   Source File: CustomSpanWeight.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Return a SimScorer for this context
 * @param context the LeafReaderContext
 * @return a SimWeight
 * @throws IOException on error
 */
public SimScorer getSimScorer(LeafReaderContext context) throws IOException {
  return simWeight == null ? null : similarity.simScorer(simWeight, context);
}
 
Example 17
Source Project: lucene-solr   Source File: PhraseWeight.java    License: Apache License 2.0 votes vote down vote up
protected abstract Similarity.SimScorer getStats(IndexSearcher searcher) throws IOException; 
Example 18
Source Project: lucene-solr   Source File: PhraseWeight.java    License: Apache License 2.0 votes vote down vote up
protected abstract PhraseMatcher getPhraseMatcher(LeafReaderContext context, SimScorer scorer, boolean exposeOffsets) throws IOException; 
Example 19
Source Project: lucene-solr   Source File: FeatureField.java    License: Apache License 2.0 votes vote down vote up
abstract SimScorer scorer(float w);