You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucene.apache.org by GitBox <gi...@apache.org> on 2019/07/09 10:14:23 UTC

[GitHub] [lucene-solr] mikemccand commented on a change in pull request #754: LUCENE-8875: Introduce Optimized Collector For Large Number Of Hits

mikemccand commented on a change in pull request #754: LUCENE-8875: Introduce Optimized Collector For Large Number Of Hits
URL: https://github.com/apache/lucene-solr/pull/754#discussion_r301505938
 
 

 ##########
 File path: lucene/sandbox/src/java/org/apache/lucene/search/LargeNumHitsTopDocsCollector.java
 ##########
 @@ -0,0 +1,159 @@
+/*
+ * 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 java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.apache.lucene.index.LeafReaderContext;
+
+import static org.apache.lucene.search.TopDocsCollector.EMPTY_TOPDOCS;
+
+/**
+ * Optimized collector for large number of hits.
+ * The collector maintains an ArrayList of hits until it accumulates
+ * the requested number of hits. Post that, it builds a Priority Queue
+ * and starts filtering further hits based on the minimum competitive
+ * score.
+ */
+public final class LargeNumHitsTopDocsCollector implements Collector {
+  private final int numHits;
+  private List<ScoreDoc> hits = new ArrayList<>();
+  /** Whether {@link #totalHits} is exact or a lower bound. */
+  private TotalHits.Relation totalHitsRelation = TotalHits.Relation.EQUAL_TO;
+  // package private for testing
+  HitQueue pq;
+  ScoreDoc pqTop;
+  int totalHits;
+
+  public LargeNumHitsTopDocsCollector(int numHits) {
+    this.numHits = numHits;
+    this.totalHits = 0;
+  }
+
+  // We always return COMPLETE since this collector should ideally
+  // be used only with large number of hits case
+  @Override
+  public ScoreMode scoreMode() {
+    return ScoreMode.COMPLETE;
+  }
+
+  @Override
+  public LeafCollector getLeafCollector(LeafReaderContext context) {
+    final int docBase = context.docBase;
+    return new TopScoreDocCollector.ScorerLeafCollector() {
+
+      @Override
+      public void setScorer(Scorable scorer) throws IOException {
+        super.setScorer(scorer);
+      }
+
+      @Override
+      public void collect(int doc) throws IOException {
+        float score = scorer.score();
+
+        // This collector relies on the fact that scorers produce positive values:
+        assert score >= 0; // NOTE: false for NaN
+
+        if (totalHits < numHits) {
+          hits.add(new ScoreDoc(doc, score));
+          totalHits++;
+          return;
+        } else if (totalHits == numHits) {
+          // Convert the list to a priority queue
+
+          // We should get here only when priority queue
+          // has been built
 
 Review comment:
   has `not yet` been built you mean?

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@lucene.apache.org
For additional commands, e-mail: dev-help@lucene.apache.org