zacharymorn commented on a change in pull request #240: URL: https://github.com/apache/lucene/pull/240#discussion_r697150848
########## File path: lucene/core/src/java/org/apache/lucene/search/TopScoreDocCollectorManager.java ########## @@ -0,0 +1,142 @@ +/* + * 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.Collection; + +/** + * Create a TopScoreDocCollectorManager which uses a shared hit counter to maintain number of hits + * and a shared {@link MaxScoreAccumulator} to propagate the minimum score across segments + * + * <p>Note that a new collectorManager should be created for each search due to its internal states. + */ +public class TopScoreDocCollectorManager + implements CollectorManager<TopScoreDocCollector, TopDocs> { + private final int numHits; + private final ScoreDoc after; + private final HitsThresholdChecker hitsThresholdChecker; + private final MaxScoreAccumulator minScoreAcc; + + /** + * Creates a new {@link TopScoreDocCollectorManager} given the number of hits to collect and the + * number of hits to count accurately. + * + * <p><b>NOTE</b>: If the total hit count of the top docs is less than or exactly {@code + * totalHitsThreshold} then this value is accurate. On the other hand, if the {@link + * TopDocs#totalHits} value is greater than {@code totalHitsThreshold} then its value is a lower + * bound of the hit count. A value of {@link Integer#MAX_VALUE} will make the hit count accurate + * but will also likely make query processing slower. + * + * <p><b>NOTE</b>: The instances returned by this method pre-allocate a full array of length + * <code>numHits</code>, and fill the array with sentinel objects. + * + * @param numHits the number of results to collect. + * @param after the previous doc after which matching docs will be collected. + * @param totalHitsThreshold the number of docs to count accurately. If the query matches more + * than {@code totalHitsThreshold} hits then its hit count will be a lower bound. On the other + * hand if the query matches less than or exactly {@code totalHitsThreshold} hits then the hit + * count of the result will be accurate. {@link Integer#MAX_VALUE} may be used to make the hit + * count accurate, but this will also make query processing slower. + * @param supportsConcurrency to use thread-safe and slower internal states for count tracking. + */ + public TopScoreDocCollectorManager( + int numHits, ScoreDoc after, int totalHitsThreshold, boolean supportsConcurrency) { Review comment: I think for the purpose of ensuring correct / thread-safe usage of Lucene provided `TopXXXCollectorManager` classes & `IndexSearch#search` API, this would probably be the simplest and yet effective solution, without any special change needed from users. The other scenarios you are considering, such as creating multiple collectors to run sequentially, will probably happen when users need to do something special about these classes and API. For that, users can still achieve it via various means, such as providing their custom implementations of `CollectorManager`, overriding the existing method from one of the `TopXXXCollectorManager` to remove the check, or simply just using `TopXXXCollector` instead of `CollectorManager` and also skipping the `reduce` step. So users should still have the freedom to customize these APIs to tailor their special needs? -- 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. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org 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