@Override public Scorer scorer(final AtomicReaderContext context, final boolean scoreDocsInOrder, final boolean topScorer, final Bits acceptDocs) throws IOException { final NodeScorer nodeScorer = (NodeScorer) weight.scorer(context, scoreDocsInOrder, topScorer, acceptDocs); return nodeScorer == null ? null // no match : new LuceneProxyNodeScorer(nodeScorer); }
/** * Scores and collects all matching documents. * * @param collector * The collector to which all matching documents are passed through. */ @Override public void score(final Collector collector) throws IOException { collector.setScorer(this); while (this.nextDoc() != NO_MORE_DOCS) { collector.collect(this.docID()); } }
@Override public float score() throws IOException { this.computeScoreAndFreq(); return score; }
@Test public void testScore() throws IOException { this.addDocument("\"Renaud renaud\" \"renaud\" . "); final LuceneProxyNodeScorer scorer = new LuceneProxyNodeScorer(this.getScorer(ntq("renaud"))); assertTrue(scorer.nextDoc() != DocsAndNodesIterator.NO_MORE_DOC); assertEquals(0, scorer.docID()); assertEquals(3.0, scorer.freq(), 0.01); final float score = scorer.score(); assertFalse(score + " != " + 0, score == 0); }
@Override public Explanation explain(final AtomicReaderContext context, final int doc) throws IOException { final LuceneProxyNodeScorer dScorer = (LuceneProxyNodeScorer) this.scorer(context, true, false, context.reader().getLiveDocs()); if (dScorer != null) { if (dScorer.advance(doc) != DocIdSetIterator.NO_MORE_DOCS && dScorer.docID() == doc) { final Explanation exp = dScorer.getWeight().explain(context, doc); exp.setValue(dScorer.score()); return exp; } } return new ComplexExplanation(false, 0.0f, "no matching term"); }
@Override public int nextDoc() throws IOException { while (scorer.nextCandidateDocument()) { if (scorer.nextNode()) { // check if there is at least 1 node that matches the query return this.docID(); } } return NO_MORE_DOCS; }
this.addDocuments(docs); final LuceneProxyNodeScorer scorer = new LuceneProxyNodeScorer(this.getScorer(nbq(must("renaud"), must("delbru")))); assertTrue(scorer.nextDoc() != DocsAndNodesIterator.NO_MORE_DOC); lastLastScore = scorer.score(); assertTrue(scorer.nextDoc() != DocsAndNodesIterator.NO_MORE_DOC); lastScore = scorer.score(); assertTrue("doc=" + scorer.docID() + " lastScore=" + lastLastScore + " score=" + lastScore, lastLastScore > lastScore); assertTrue(scorer.nextDoc() != DocsAndNodesIterator.NO_MORE_DOC); lastLastScore = lastScore; lastScore = scorer.score(); assertTrue("lastScore=" + lastLastScore + " score=" + lastScore, lastLastScore > lastScore); lastLastScore = scorer.score(); assertTrue(scorer.nextDoc() != DocsAndNodesIterator.NO_MORE_DOC); lastLastScore = lastScore; lastScore = scorer.score(); assertFalse(scorer.nextDoc() != DocsAndNodesIterator.NO_MORE_DOC);
@Override public int advance(final int target) throws IOException { if (scorer.skipToCandidate(target)) { do { if (scorer.nextNode()) { return this.docID(); } } while (scorer.nextCandidateDocument()); } return NO_MORE_DOCS; }
/** * Expert: Collects matching documents in a range. Hook for optimization. Note * that {@link #nextDoc()} must be called once before this method is * called for the first time. * * @param collector * The collector to which all matching documents are passed through. * @param max * Do not score documents past this. * @return true if more matching documents may remain. */ @Override public boolean score(final Collector collector, final int max, final int firstDocID) throws IOException { // firstDocID is ignored since nextDocument() sets 'currentDoc' collector.setScorer(this); while (this.docID() < max) { collector.collect(this.docID()); if (this.nextDoc() == NO_MORE_DOCS) { return false; } } return true; }
/** * Compute the score and the frequency of the current document * @throws IOException */ private void computeScoreAndFreq() throws IOException { final int doc = this.docID(); if (doc != lastDoc) { lastDoc = doc; score = 0; freq = 0; do { // nextNode() was already called in nextDoc() or in advance() score += scorer.scoreInNode(); freq += scorer.freqInNode(); } while (scorer.nextNode()); } }
/** * Returns number of matches for the current document. This returns a float * (not int) because SloppyPhraseScorer discounts its freq according to how * "sloppy" the match was. * <p> * Only valid after calling {@link #nextDoc()} or {@link #advance(int)} */ @Override public float freq() throws IOException { this.computeScoreAndFreq(); return freq; }