javanna commented on code in PR #15660: URL: https://github.com/apache/lucene/pull/15660#discussion_r3173037439
########## lucene/CHANGES.txt: ########## @@ -89,6 +89,8 @@ Improvements * GITHUB#15453: Avoid unnecessary sorting and instantiations in readMapOfStrings. (Benjamin Lerer) +* GITHUB#15660: Introduce LargeNumHitsTopDocsCollectorManager to parallelize search when using LargeNumHitsTopDocsCollector. (Binlong Gao) Review Comment: can you move this entry to the 10.5 improvements section? ########## lucene/sandbox/src/java/org/apache/lucene/sandbox/search/LargeNumHitsTopDocsCollectorManager.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.sandbox.search; + +import java.io.IOException; +import java.util.Collection; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.TopDocs; + +/** + * CollectorManager for {@link LargeNumHitsTopDocsCollector} that enables concurrent collection of + * top docs across multiple segments. + */ +public class LargeNumHitsTopDocsCollectorManager + implements CollectorManager<LargeNumHitsTopDocsCollector, TopDocs> { + private final int numHits; + + /** + * Creates a new {@link LargeNumHitsTopDocsCollectorManager} given the number of hits to collect. + * + * @param numHits the number of results to collect. + */ + public LargeNumHitsTopDocsCollectorManager(int numHits) { + if (numHits <= 0) { + throw new IllegalArgumentException("numHits must be > 0, got " + numHits); + } + this.numHits = numHits; + } + + @Override + public LargeNumHitsTopDocsCollector newCollector() { + return new LargeNumHitsTopDocsCollector(numHits); + } + + @Override + public TopDocs reduce(Collection<LargeNumHitsTopDocsCollector> collectors) throws IOException { + final TopDocs[] topDocs = new TopDocs[collectors.size()]; + int i = 0; + for (LargeNumHitsTopDocsCollector collector : collectors) { + topDocs[i++] = collector.topDocs(); Review Comment: I believe we also need to handle the case where a collector returns 0 hits. This is really an existing problem with the collector impl. `topDocs` should perhaps return empty top docs in that case instead of throwing. We need a test for that scenario as well. ########## lucene/sandbox/src/java/org/apache/lucene/sandbox/search/LargeNumHitsTopDocsCollectorManager.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.sandbox.search; + +import java.io.IOException; +import java.util.Collection; +import org.apache.lucene.search.CollectorManager; +import org.apache.lucene.search.TopDocs; + +/** + * CollectorManager for {@link LargeNumHitsTopDocsCollector} that enables concurrent collection of + * top docs across multiple segments. + */ +public class LargeNumHitsTopDocsCollectorManager + implements CollectorManager<LargeNumHitsTopDocsCollector, TopDocs> { + private final int numHits; + + /** + * Creates a new {@link LargeNumHitsTopDocsCollectorManager} given the number of hits to collect. + * + * @param numHits the number of results to collect. + */ + public LargeNumHitsTopDocsCollectorManager(int numHits) { + if (numHits <= 0) { + throw new IllegalArgumentException("numHits must be > 0, got " + numHits); + } + this.numHits = numHits; + } + + @Override + public LargeNumHitsTopDocsCollector newCollector() { + return new LargeNumHitsTopDocsCollector(numHits); + } + + @Override + public TopDocs reduce(Collection<LargeNumHitsTopDocsCollector> collectors) throws IOException { + final TopDocs[] topDocs = new TopDocs[collectors.size()]; + int i = 0; + for (LargeNumHitsTopDocsCollector collector : collectors) { + topDocs[i++] = collector.topDocs(); Review Comment: This will throw if numHits is 0. It's an edge case. Also an existing small issue when interacting directly with the collector. Should we reject 0 and negative values in the constructor? Would make sense to do the same in the collector as well, perhaps as a follow-up. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
