stefanvodita commented on code in PR #13568: URL: https://github.com/apache/lucene/pull/13568#discussion_r1679392013
########## lucene/core/src/java/org/apache/lucene/search/CollectorOwner.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.List; + +/** + * Like {@link CollectorManager}, but it owns the collectors its manager creates. Benefit is that Review Comment: I don't think it's terrible that we have this new owner class, but maybe it's one of those things where the manager would do it if we were building it from scratch. ########## lucene/core/src/java/org/apache/lucene/search/CollectorOwner.java: ########## @@ -0,0 +1,86 @@ +/* + * 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.List; + +/** + * Like {@link CollectorManager}, but it owns the collectors its manager creates. Benefit is that + * clients of the class don't have to worry about keeping the list of collectors, as well as about + * making the collectors type (C) compatible when reduce is called. + * + * @lucene.experimental + */ +public final class CollectorOwner<C extends Collector, T> { + + private final CollectorManager<C, T> manager; + + private T result; + private boolean reduced; + + // TODO: Normally, for IndexSearcher, we don't need parallelized write access to the list + // because we create new collectors sequentially. But drill sideways creates new collectors in + // DrillSidewaysQuery#Weight#bulkScorer which is already called concurrently. + // I think making the list sychronized here is not a huge concern, at the same time, do we want + // to do something about it? + // e.g. have boolean property in constructor that makes it threads friendly when set? + private final List<C> collectors = Collections.synchronizedList(new ArrayList<>()); + + public CollectorOwner(CollectorManager<C, T> manager) { + this.manager = manager; + } + + /** Return a new {@link Collector}. This must return a different instance on each call. */ + public C newCollector() throws IOException { + C collector = manager.newCollector(); + collectors.add(collector); + return collector; + } + + public C getCollector(int i) { + return collectors.get(i); + } + + /** + * Reduce the results of individual collectors into a meaningful result. For instance a {@link + * TopDocsCollector} would compute the {@link TopDocsCollector#topDocs() top docs} of each + * collector and then merge them using {@link TopDocs#merge(int, TopDocs[])}. This method must be + * called after collection is finished on all provided collectors. + */ + public T reduce() throws IOException { + result = manager.reduce(collectors); + reduced = true; + return result; + } + + public static <C extends Collector, T> CollectorOwner<C, T> hire(CollectorManager<C, T> manager) { Review Comment: I kind of like it too, if just for the analogy, but if it's not contributing anything, I'm tempted to say we should just call the constructor. Up to you though. ########## lucene/sandbox/src/test/org/apache/lucene/sandbox/facet/SandboxFacetTestCase.java: ########## @@ -0,0 +1,407 @@ +/* + * 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.facet; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.lucene.facet.FacetResult; +import org.apache.lucene.facet.FacetsCollector; +import org.apache.lucene.facet.FacetsCollector.MatchingDocs; +import org.apache.lucene.facet.FacetsConfig; +import org.apache.lucene.facet.LabelAndValue; +import org.apache.lucene.facet.taxonomy.FacetLabel; +import org.apache.lucene.facet.taxonomy.TaxonomyFacetLabels; +import org.apache.lucene.facet.taxonomy.TaxonomyFacetLabels.FacetLabelReader; +import org.apache.lucene.facet.taxonomy.TaxonomyReader; +import org.apache.lucene.sandbox.facet.abstracts.OrdLabelBiMap; +import org.apache.lucene.sandbox.facet.abstracts.OrdToComparable; +import org.apache.lucene.sandbox.facet.abstracts.OrdinalIterator; +import org.apache.lucene.sandbox.facet.ordinal_iterators.TopnOrdinalIterator; +import org.apache.lucene.sandbox.facet.recorders.CountFacetRecorder; +import org.apache.lucene.sandbox.facet.taxonomy.TaxonomyChildrenOrdinalIterator; +import org.apache.lucene.sandbox.facet.taxonomy.TaxonomyOrdLabelBiMap; +import org.apache.lucene.search.DocIdSetIterator; +import org.apache.lucene.tests.util.LuceneTestCase; +import org.apache.lucene.tests.util.TestUtil; +import org.apache.lucene.util.BytesRef; + +public abstract class SandboxFacetTestCase extends LuceneTestCase { + // we don't have access to overall count for all facets from count recorder, + // and we can't compute it as a SUM of values for each facet ID because we need to respect cases + // where + // the same doc belongs to multiple facets (e.g. overlapping ranges and + // multi value fields). We can add an extra range that includes everything, + // or consider supporting overall count in CountFacetRecorder. But it is not exactly the value + // we can get now, as this value wouldn't respect top-n cutoff. Is this value a must have facets Review Comment: Agree, let's postpone as much as we reasonably can, not to make this PR any larger. -- 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