Example usage for org.apache.lucene.search Scorer docID

List of usage examples for org.apache.lucene.search Scorer docID

Introduction

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

Prototype

public abstract int docID();

Source Link

Document

Returns the doc ID that is currently being scored.

Usage

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

License:Apache License

public Scorer createScorer(final Scorer innerScorer, IndexReader reader, boolean scoreDocsInOrder,
        boolean topScorer) 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;
            return new Scorer(innerScorer.getSimilarity()) {

                @Override// ww  w  . ja  v a2 s .  c om
                public float score() throws IOException {
                    float rawScore = innerScorer.score();
                    long timeVal = termList.getRawValue(orderArray.get(innerScorer.docID()));
                    float timeScore = computeTimeFactor(timeVal);
                    return combineScores(timeScore, rawScore);
                }

                @Override
                public int advance(int target) throws IOException {
                    return innerScorer.advance(target);
                }

                @Override
                public int docID() {
                    return innerScorer.docID();
                }

                @Override
                public int nextDoc() throws IOException {
                    return innerScorer.nextDoc();
                }

            };
        } else {
            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.o19s.es.ltr.logging.LoggingFetchSubPhase.java

License:Apache License

void doLog(Query query, List<HitLogConsumer> loggers, IndexSearcher searcher, SearchHit[] hits)
        throws IOException {
    // Reorder hits by id so we can scan all the docs belonging to the same
    // segment by reusing the same scorer.
    SearchHit[] reordered = new SearchHit[hits.length];
    System.arraycopy(hits, 0, reordered, 0, hits.length);
    Arrays.sort(reordered, Comparator.comparingInt(SearchHit::docId));

    int hitUpto = 0;
    int readerUpto = -1;
    int endDoc = 0;
    int docBase = 0;
    Scorer scorer = null;
    Weight weight = searcher.createNormalizedWeight(query, true);
    // Loop logic borrowed from lucene QueryRescorer
    while (hitUpto < reordered.length) {
        SearchHit hit = reordered[hitUpto];
        int docID = hit.docId();
        loggers.forEach((l) -> l.nextDoc(hit));
        LeafReaderContext readerContext = null;
        while (docID >= endDoc) {
            readerUpto++;//from   ww  w.  ja  v  a  2  s  . com
            readerContext = searcher.getTopReaderContext().leaves().get(readerUpto);
            endDoc = readerContext.docBase + readerContext.reader().maxDoc();
        }

        if (readerContext != null) {
            // We advanced to another segment:
            docBase = readerContext.docBase;
            scorer = weight.scorer(readerContext);
        }

        if (scorer != null) {
            int targetDoc = docID - docBase;
            int actualDoc = scorer.docID();
            if (actualDoc < targetDoc) {
                actualDoc = scorer.iterator().advance(targetDoc);
            }
            if (actualDoc == targetDoc) {
                // Scoring will trigger log collection
                scorer.score();
            }
        }

        hitUpto++;
    }
}

From source file:com.o19s.es.ltr.query.LtrScorer.java

License:Apache License

@Override
public float score() throws IOException {
    DataPoint allScores = new DenseProgramaticDataPoint(_subScorers.size());
    int featureIdx = 1; // RankLib is 1-based
    for (Scorer scorer : _subScorers) {
        if (scorer.docID() < docID()) {
            scorer.iterator().advance(docID());
        }//from w w  w  .  java  2 s  .  c  o  m
        float featureVal = 0.0f;
        if (scorer.docID() == docID()) {
            featureVal = scorer.score();
        }
        //System.out.printf("Doc %d, feature %d, val %f\n", docID(), featureIdx, featureVal);
        allScores.setFeatureValue(featureIdx, featureVal);
        featureIdx++;
    }
    float score = (float) _rankModel.eval(allScores);
    //System.out.printf("Doc %d, score %f\n", docID(), score);
    return score;
}

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeScorer.java

License:Open Source License

@Test
public void testNextDoc() throws Exception {
    this.addDocuments("{ \"aaa bbb\" : \"aaa ccc\" , \"ccc\" : \"bbb ccc\" }", "{ \"aaa\" : \"aaa bbb ddd\" }");

    final Scorer scorer1 = this.getScorer(ntq("aaa").getLuceneProxyQuery());

    assertTrue(scorer1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(0, scorer1.docID());
    assertEquals(2, scorer1.freq(), 0);//from   ww w  .  jav a2  s .co m
    assertTrue(scorer1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(1, scorer1.docID());
    assertEquals(2, scorer1.freq(), 0);
    assertTrue(scorer1.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);

    final Scorer scorer2 = this.getScorer(ntq("ccc").getLuceneProxyQuery());

    assertTrue(scorer2.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(0, scorer2.docID());
    assertEquals(3, scorer2.freq(), 0);
    assertTrue(scorer2.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);

    final Scorer scorer3 = this.getScorer(ntq("ddd").getLuceneProxyQuery());

    assertTrue(scorer3.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(1, scorer3.docID());
    assertEquals(1, scorer3.freq(), 0);
    assertTrue(scorer3.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
}

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeScorer.java

License:Open Source License

@Test
public void testAdvance() throws Exception {
    this.addDocuments("{ \"baba\" : \"aaa ccc\" , \"ccc\" : \"bbb ccc\" }", "{ \"aaa\" : \"aaa bbb ddd\" }",
            "{ \"ddd\" : [ \"bobo\", \"bibi\" ] }");

    final Scorer scorer1 = this.getScorer(ntq("bobo").getLuceneProxyQuery());

    assertTrue(scorer1.advance(2) != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(2, scorer1.docID());
    assertEquals(1, scorer1.freq(), 0);/*from www.  java  2 s .co m*/
    assertTrue(scorer1.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);

    final Scorer scorer2 = this.getScorer(ntq("baba").getLuceneProxyQuery());
    assertTrue(scorer2.advance(2) == DocIdSetIterator.NO_MORE_DOCS);
}

From source file:com.sindicetech.siren.search.node.TestLuceneProxyNodeScorer.java

License:Open Source License

@Test
public void testAdvanceInfiniteLoop() throws Exception {
    this.addDocuments("{ \"baba\" : \"bibi ccc\" , \"ccc\" : \"bbb ccc\" }",
            "{ \"baba bibi baba bibi\" : \"aaa bbb ddd\" }", "{ \"baba bibi\" : \"aaa bbb ddd\" }");

    final Scorer scorer1 = this.getScorer(nbq(must("baba", "bibi")).getLuceneProxyQuery());

    assertTrue(scorer1.advance(0) != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(1, scorer1.docID());
    assertEquals(2, scorer1.freq(), 0);/*from   www .j a v  a  2  s  .c o m*/
    final float score1 = scorer1.score();
    assertTrue(scorer1.nextDoc() != DocIdSetIterator.NO_MORE_DOCS);
    assertEquals(2, scorer1.docID());
    assertEquals(2, scorer1.freq(), 0);
    final float score2 = scorer1.score();
    assertTrue(score1 > score2);
    assertTrue(scorer1.nextDoc() == DocIdSetIterator.NO_MORE_DOCS);
}

From source file:de.uop.code.disambiguation.ltr.lucene.query.AbstractDisjunctionScorer.java

License:Apache License

/**
 * The subtree of subScorers at root is a min heap except possibly for its
 * root element. Bubble the root down as required to make the subtree a
 * heap.//ww  w .ja  va  2s .  c  o m
 */
protected final void heapAdjust(final int root) {
    final Scorer scorer = this.subScorers[root];
    final LearnToRankClause clause = this.ltrClauses[root];
    final int doc = scorer.docID();
    int var = root;
    while (var <= ((this.numScorers >> 1) - 1)) {
        final int lchild = (var << 1) + 1;
        final Scorer lscorer = this.subScorers[lchild];
        final LearnToRankClause lclause = this.ltrClauses[lchild];
        final int ldoc = lscorer.docID();
        int rdoc = Integer.MAX_VALUE;
        final int rchild = (var << 1) + 2;
        Scorer rscorer = null;
        LearnToRankClause rclause = null;
        if (rchild < this.numScorers) {
            rscorer = this.subScorers[rchild];
            rclause = this.ltrClauses[rchild];
            rdoc = rscorer.docID();
        }
        if (ldoc < doc) {
            if (rdoc < ldoc) {
                this.subScorers[var] = rscorer;
                this.ltrClauses[var] = rclause; //
                this.subScorers[rchild] = scorer;
                this.ltrClauses[rchild] = clause;//
                var = rchild;
            } else {
                this.subScorers[var] = lscorer;
                this.ltrClauses[var] = lclause; //
                this.subScorers[lchild] = scorer;
                this.ltrClauses[lchild] = clause; //
                var = lchild;
            }
        } else if (rdoc < doc) {
            this.subScorers[var] = rscorer;
            this.ltrClauses[var] = rclause; //
            this.subScorers[rchild] = scorer;
            this.ltrClauses[rchild] = clause; //
            var = rchild;
        } else {
            return;
        }
    }
}

From source file:de.uop.code.disambiguation.ltr.lucene.query.DisjunctionSumScorer.java

License:Apache License

public void afterNext() throws IOException {
    final Scorer sub = this.subScorers[0];
    this.doc = sub.docID();
    if (this.doc == NO_MORE_DOCS) {
        this.nrMatchers = Integer.MAX_VALUE; // stop looping
    } else {//from  www  . j  a va  2s . co m
        this.scoreVal = sub.score() * this.clauses[0].getWeight();
        this.clauses[0].addFeatureValue(this.docBase, this.doc, (float) this.scoreVal);
        this.nrMatchers = 1;
        this.countMatches(1);
        this.countMatches(2);
    }
}

From source file:doser.lucene.query.AbstractDisjunctionScorer.java

License:Apache License

/**
 * The subtree of subScorers at root is a min heap except possibly for its
 * root element. Bubble the root down as required to make the subtree a
 * heap.//from  w  ww .  j av a2  s .  com
 */
protected final void heapAdjust(final int root) {
    final Scorer scorer = subScorers[root];
    final LearnToRankClause clause = ltrClauses[root];
    final int doc = scorer.docID();
    int var = root;
    while (var <= ((numScorers >> 1) - 1)) {
        final int lchild = (var << 1) + 1;
        final Scorer lscorer = subScorers[lchild];
        final LearnToRankClause lclause = ltrClauses[lchild];
        final int ldoc = lscorer.docID();
        int rdoc = Integer.MAX_VALUE;
        final int rchild = (var << 1) + 2;
        Scorer rscorer = null;
        LearnToRankClause rclause = null;
        if (rchild < numScorers) {
            rscorer = subScorers[rchild];
            rclause = ltrClauses[rchild];
            rdoc = rscorer.docID();
        }
        if (ldoc < doc) {
            if (rdoc < ldoc) {
                subScorers[var] = rscorer;
                ltrClauses[var] = rclause; //
                subScorers[rchild] = scorer;
                ltrClauses[rchild] = clause;//
                var = rchild;
            } else {
                subScorers[var] = lscorer;
                ltrClauses[var] = lclause; //
                subScorers[lchild] = scorer;
                ltrClauses[lchild] = clause; //
                var = lchild;
            }
        } else if (rdoc < doc) {
            subScorers[var] = rscorer;
            ltrClauses[var] = rclause; //
            subScorers[rchild] = scorer;
            ltrClauses[rchild] = clause; //
            var = rchild;
        } else {
            return;
        }
    }
}

From source file:doser.lucene.query.DisjunctionSumScorer.java

License:Apache License

public void afterNext() throws IOException {
    final Scorer sub = subScorers[0];
    doc = sub.docID();
    if (doc == NO_MORE_DOCS) {
        nrMatchers = Integer.MAX_VALUE; // stop looping
    } else {/* w  w  w. j av  a  2  s . c  o m*/
        scoreVal = sub.score() * clauses[0].getWeight();
        clauses[0].addFeatureValue(docBase, doc, (float) scoreVal);
        nrMatchers = 1;
        countMatches(1);
        countMatches(2);
    }
}