jpountz commented on a change in pull request #101:
URL: https://github.com/apache/lucene/pull/101#discussion_r625979068



##########
File path: 
lucene/core/src/java/org/apache/lucene/search/BlockMaxMaxscoreScorer.java
##########
@@ -0,0 +1,339 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.search;
+
+import static org.apache.lucene.search.ScorerUtil.costWithMinShouldMatch;
+
+import java.io.IOException;
+import java.util.*;
+
+/** Scorer implementing Block-Max Maxscore algorithm */
+public class BlockMaxMaxscoreScorer extends Scorer {
+  private final ScoreMode scoreMode;
+  private final int scalingFactor;
+
+  // current doc ID of the leads
+  private int doc;
+
+  // doc id boundary that all scorers maxScore are valid
+  private int upTo = -1;
+
+  // heap of scorers ordered by doc ID
+  private final DisiPriorityQueue essentialsScorers;
+
+  // list of scorers whose sum of maxScore is less than minCompetitiveScore, 
ordered by maxScore
+  private final List<DisiWrapper> nonEssentialScorers;
+
+  // sum of max scores of scorers in nonEssentialScorers list
+  private long nonEssentialMaxScoreSum;
+
+  // sum of score of scorers in essentialScorers list that are positioned on 
matching doc
+  private long matchedDocScoreSum;
+
+  private long cost;
+
+  private final MaxScoreSumPropagator maxScoreSumPropagator;
+
+  private final List<Scorer> scorers;
+
+  // scaled min competitive score
+  private long minCompetitiveScore = 0;
+
+  /**
+   * Constructs a Scorer
+   *
+   * @param weight The weight to be used.
+   * @param scorers The sub scorers this Scorer should iterate on for optional 
clauses
+   * @param scoreMode The scoreMode
+   */
+  public BlockMaxMaxscoreScorer(Weight weight, List<Scorer> scorers, ScoreMode 
scoreMode)
+      throws IOException {
+    super(weight);
+    assert scoreMode == ScoreMode.TOP_SCORES;
+
+    this.scoreMode = scoreMode;
+    this.doc = -1;
+    this.scorers = scorers;
+    this.cost =
+        costWithMinShouldMatch(
+            
scorers.stream().map(Scorer::iterator).mapToLong(DocIdSetIterator::cost),
+            scorers.size(),
+            1);
+
+    essentialsScorers = new DisiPriorityQueue(scorers.size());
+    nonEssentialScorers = new LinkedList<>();
+
+    scalingFactor = WANDScorer.getScalingFactor(scorers);
+    maxScoreSumPropagator = new MaxScoreSumPropagator(scorers);
+
+    for (Scorer scorer : scorers) {
+      nonEssentialScorers.add(new DisiWrapper(scorer));
+    }
+  }
+
+  @Override
+  public DocIdSetIterator iterator() {
+    return TwoPhaseIterator.asDocIdSetIterator(twoPhaseIterator());
+  }
+
+  @Override
+  public TwoPhaseIterator twoPhaseIterator() {
+    DocIdSetIterator approximation =
+        new DocIdSetIterator() {
+          private long lastMinCompetitiveScore;
+
+          @Override
+          public int docID() {
+            return doc;
+          }
+
+          @Override
+          public int nextDoc() throws IOException {
+            return advance(doc + 1);
+          }
+
+          @Override
+          public int advance(int target) throws IOException {
+            doAdvance(target);
+
+            while (doc != DocIdSetIterator.NO_MORE_DOCS
+                && nonEssentialMaxScoreSum + matchedDocScoreSum < 
minCompetitiveScore) {
+              doAdvance(doc + 1);
+            }
+
+            return doc;
+          }
+
+          private void doAdvance(int target) throws IOException {
+            matchedDocScoreSum = 0;
+            // Find next smallest doc id that is larger than or equal to 
target from the essential
+            // scorers
+
+            // If the next candidate doc id is still within interval boundary,
+            if (lastMinCompetitiveScore == minCompetitiveScore && target <= 
upTo) {
+              while (essentialsScorers.top().doc < target) {
+                DisiWrapper w = essentialsScorers.pop();
+                w.doc = w.iterator.advance(target);
+                essentialsScorers.add(w);

Review comment:
       can you use updateTop instead? It's usually faster than pop+add




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to