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 . 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: SpanWeight.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private Similarity.SimScorer buildSimWeight(SpanQuery query, IndexSearcher searcher, Map<Term, TermStates> termStates, float boost) throws IOException {
  if (termStates == null || termStates.size() == 0 || query.getField() == null)
    return null;
  TermStatistics[] termStats = new TermStatistics[termStates.size()];
  int termUpTo = 0;
  for (Map.Entry<Term, TermStates> entry : termStates.entrySet()) {
    TermStates ts = entry.getValue();
    if (ts.docFreq() > 0) {
      termStats[termUpTo++] = searcher.termStatistics(entry.getKey(), ts.docFreq(), ts.totalTermFreq());
    }
  }
  CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField());
  if (termUpTo > 0) {
    return similarity.scorer(boost, collectionStats, ArrayUtil.copyOfSubArray(termStats, 0, termUpTo));
  } else {
    return null; // no terms at all exist, we won't use similarity
  }
}
 
Example 2
Source File: CustomSpanPayloadCheckQuery.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public SpanScorer scorer(LeafReaderContext context) throws IOException {
    if (field == null)
        return null;

    Terms terms = context.reader().terms(field);
    if (terms != null && !terms.hasPositions()) {
        throw new IllegalStateException("field \"" + field +
                "\" was indexed without position data; cannot run SpanQuery (query=" + parentQuery + ")");
    }

    final Spans spans = getSpans(context, Postings.PAYLOADS);
    if (spans == null) {
        return null;
    }
    final Similarity.SimScorer docScorer = getSimScorer(context);
    return new SpanScorer(this, spans, docScorer);
}
 
Example 3
Source File: TermDocsEnum.java    From linden with Apache License 2.0 5 votes vote down vote up
public TermDocsEnum(FlexibleQuery.FlexibleTerm term, int docFreq, DocsAndPositionsEnum postings,
                    Similarity.SimScorer docScorer, int field, int termPos) throws IOException {
  this.doc = -1;
  this.term = term;
  this.postings = postings;
  this.docFreq = docFreq;
  this.docScorer = docScorer;
  this.field = field;
  this.termPos = termPos;
  this.positions = new int[initPositionSize];
  this.matchedPositions = new int[initPositionSize];
}
 
Example 4
Source File: IntervalScoreFunction.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Similarity.SimScorer scorer(float weight) {
  return new Similarity.SimScorer() {
    @Override
    public float score(float freq, long norm) {
      // should be f / (f + k) but we rewrite it to
      // 1 - k / (f + k) to make sure it doesn't decrease
      // with f in spite of rounding
      return weight * (1.0f - pivot / (pivot + freq));
    }
  };
}
 
Example 5
Source File: IntervalScoreFunction.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Similarity.SimScorer scorer(float weight) {
  return new Similarity.SimScorer() {
    @Override
    public float score(float freq, long norm) {
      // 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.0f - pivotPa / (Math.pow(freq, a) + pivotPa)));
    }
  };
}
 
Example 6
Source File: PhraseCountScorer.java    From pyramid with Apache License 2.0 5 votes vote down vote up
/** Sole constructor. */
public PhraseCountScorer(CustomSpanWeight weight, Spans spans, Similarity.SimScorer docScorer, boolean weightedCount) {
    super(weight);
    this.spans = Objects.requireNonNull(spans);
    this.docScorer = docScorer;
    this.weightedCount = weightedCount;
}
 
Example 7
Source File: AllTermQuery.java    From Elasticsearch with 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 8
Source File: PRMSFieldBoostTest.java    From querqy with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetThatFieldProbabilityRatioIsReflectedInBoost() throws Exception {

    DocumentFrequencyCorrection dfc = new DocumentFrequencyCorrection();

    Directory directory = newDirectory();
    
    Analyzer analyzer = new StandardAnalyzer();

    IndexWriterConfig conf = new IndexWriterConfig(analyzer);
    conf.setCodec(Codec.forName(TestUtil.LUCENE_CODEC));
    IndexWriter indexWriter = new IndexWriter(directory, conf);
   
    addNumDocs("f1", "abc", indexWriter, 2);
    addNumDocs("f1", "def", indexWriter, 4);
    addNumDocs("f2", "abc", indexWriter, 4);
    addNumDocs("f2", "def", indexWriter, 2);
    indexWriter.close();

    
    Map<String, Float> fields = new HashMap<>();
    fields.put("f1", 1f);
    fields.put("f2", 1f);
    SearchFieldsAndBoosting searchFieldsAndBoosting = new SearchFieldsAndBoosting(FieldBoostModel.PRMS, fields, fields, 0.8f);
    
    LuceneQueryBuilder queryBuilder = new LuceneQueryBuilder(new DependentTermQueryBuilder(dfc), analyzer,
            searchFieldsAndBoosting, 0.01f, null);
    
    WhiteSpaceQuerqyParser parser = new WhiteSpaceQuerqyParser();
    
    Query query = queryBuilder.createQuery(parser.parse("abc"));
    dfc.finishedUserQuery();
    
    assertTrue(query instanceof DisjunctionMaxQuery);
    
    DisjunctionMaxQuery dmq = (DisjunctionMaxQuery) query;
    List<Query> disjuncts = dmq.getDisjuncts();
    assertEquals(2, disjuncts.size());
    
    Query disjunct1 = disjuncts.get(0);
    assertTrue(disjunct1 instanceof DependentTermQueryBuilder.DependentTermQuery);
    DependentTermQueryBuilder.DependentTermQuery dtq1 = (DependentTermQueryBuilder.DependentTermQuery) disjunct1;
    
    Query disjunct2 = disjuncts.get(1);
    assertTrue(disjunct2 instanceof DependentTermQueryBuilder.DependentTermQuery);
    DependentTermQueryBuilder.DependentTermQuery dtq2 = (DependentTermQueryBuilder.DependentTermQuery) disjunct2;

    assertNotEquals(dtq1.getTerm().field(), dtq2.getTerm().field());

    Similarity similarity = Mockito.mock(Similarity.class);
    Similarity.SimScorer simScorer = Mockito.mock(Similarity.SimScorer.class);

    ArgumentCaptor<Float> computeWeightBoostCaptor = ArgumentCaptor.forClass(Float.class);

    when(similarity.scorer(
            computeWeightBoostCaptor.capture(),
            any(CollectionStatistics.class),
            ArgumentMatchers.<TermStatistics>any())).thenReturn(simScorer);

    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher =  new IndexSearcher(indexReader);
    indexSearcher.setSimilarity(similarity);

    Weight weight1 = indexSearcher.createWeight(dtq1, ScoreMode.COMPLETE, 1.0f);
    Weight weight2 = indexSearcher.createWeight(dtq2, ScoreMode.COMPLETE, 1.0f);

    final List<Float> capturedBoosts = computeWeightBoostCaptor.getAllValues();
    float bf1 = capturedBoosts.get(0);
    float bf2 = capturedBoosts.get(1);

    assertEquals(2f, bf2 / bf1, 0.00001);

    indexReader.close();
    directory.close();
    analyzer.close();
    
    
}
 
Example 9
Source File: IntervalScoreFunction.java    From lucene-solr with Apache License 2.0 votes vote down vote up
public abstract Similarity.SimScorer scorer(float weight); 
Example 10
Source File: PhraseWeight.java    From lucene-solr with Apache License 2.0 votes vote down vote up
protected abstract Similarity.SimScorer getStats(IndexSearcher searcher) throws IOException;