javanna commented on code in PR #13733: URL: https://github.com/apache/lucene/pull/13733#discussion_r1746991600
########## lucene/facet/src/java/org/apache/lucene/facet/FacetsCollectorManager.java: ########## @@ -54,4 +79,135 @@ public ReducedFacetsCollector(final Collection<FacetsCollector> facetsCollectors facetsCollector -> matchingDocs.addAll(facetsCollector.getMatchingDocs())); } } + + /** Utility method, to search and also collect all hits into the provided {@link Collector}. */ + public static FacetsResult search( + IndexSearcher searcher, Query q, int n, FacetsCollectorManager fcm) throws IOException { + return doSearch(searcher, null, q, n, null, false, fcm); + } + + /** Utility method, to search and also collect all hits into the provided {@link Collector}. */ + public static FacetsResult search( + IndexSearcher searcher, Query q, int n, Sort sort, FacetsCollectorManager fcm) + throws IOException { + if (sort == null) { + throw new IllegalArgumentException("sort must not be null"); + } + return doSearch(searcher, null, q, n, sort, false, fcm); + } + + /** Utility method, to search and also collect all hits into the provided {@link Collector}. */ + public static FacetsResult search( + IndexSearcher searcher, + Query q, + int n, + Sort sort, + boolean doDocScores, + FacetsCollectorManager fcm) + throws IOException { + if (sort == null) { + throw new IllegalArgumentException("sort must not be null"); + } + return doSearch(searcher, null, q, n, sort, doDocScores, fcm); + } + + /** Utility method, to search and also collect all hits into the provided {@link Collector}. */ + public static FacetsResult searchAfter( + IndexSearcher searcher, ScoreDoc after, Query q, int n, FacetsCollectorManager fcm) + throws IOException { + return doSearch(searcher, after, q, n, null, false, fcm); + } + + /** Utility method, to search and also collect all hits into the provided {@link Collector}. */ + public static FacetsResult searchAfter( + IndexSearcher searcher, ScoreDoc after, Query q, int n, Sort sort, FacetsCollectorManager fcm) + throws IOException { + if (sort == null) { + throw new IllegalArgumentException("sort must not be null"); + } + return doSearch(searcher, after, q, n, sort, false, fcm); + } + + /** Utility method, to search and also collect all hits into the provided {@link Collector}. */ + public static FacetsResult searchAfter( + IndexSearcher searcher, + ScoreDoc after, + Query q, + int n, + Sort sort, + boolean doDocScores, + FacetsCollectorManager fcm) + throws IOException { + if (sort == null) { + throw new IllegalArgumentException("sort must not be null"); + } + return doSearch(searcher, after, q, n, sort, doDocScores, fcm); + } + + private static FacetsResult doSearch( + IndexSearcher searcher, + ScoreDoc after, + Query q, + int n, + Sort sort, + boolean doDocScores, + FacetsCollectorManager fcm) + throws IOException { + + int limit = searcher.getIndexReader().maxDoc(); + if (limit == 0) { + limit = 1; + } + n = Math.min(n, limit); + + if (after != null && after.doc >= limit) { + throw new IllegalArgumentException( + "after.doc exceeds the number of documents in the reader: after.doc=" + + after.doc + + " limit=" + + limit); + } + + final TopDocs topDocs; + final FacetsCollector facetsCollector; + if (n == 0) { + TotalHitCountCollectorManager hitCountCollectorManager = new TotalHitCountCollectorManager(); + MultiCollectorManager multiCollectorManager = + new MultiCollectorManager(hitCountCollectorManager, fcm); + Object[] result = searcher.search(q, multiCollectorManager); + topDocs = + new TopDocs( + new TotalHits((Integer) result[0], TotalHits.Relation.EQUAL_TO), new ScoreDoc[0]); + facetsCollector = (FacetsCollector) result[1]; + } else { + final MultiCollectorManager multiCollectorManager; + if (sort != null) { + if (after != null && !(after instanceof FieldDoc)) { + // TODO: if we fix type safety of TopFieldDocs we can + // remove this + throw new IllegalArgumentException("after must be a FieldDoc; got " + after); + } + TopFieldCollectorManager topFieldCollectorManager = + new TopFieldCollectorManager(sort, n, (FieldDoc) after, Integer.MAX_VALUE, true); + multiCollectorManager = new MultiCollectorManager(topFieldCollectorManager, fcm); + } else { + TopScoreDocCollectorManager topScoreDocCollectorManager = + new TopScoreDocCollectorManager(n, after, Integer.MAX_VALUE, true); + multiCollectorManager = new MultiCollectorManager(topScoreDocCollectorManager, fcm); + } + Object[] result = searcher.search(q, multiCollectorManager); + topDocs = (TopDocs) result[0]; + if (doDocScores) { + TopFieldCollector.populateScores(topDocs.scoreDocs, searcher, q); + } + facetsCollector = (FacetsCollector) result[1]; + } + return new FacetsResult(topDocs, facetsCollector); + } + + /** + * Holds results of a search run via static utility methods exposed by this class. Those include + * {@link TopDocs} as well as facets result included in the returned {@link FacetsCollector} + */ + public record FacetsResult(TopDocs topDocs, FacetsCollector facetsCollector) {} Review Comment: I think that using a collector to expose results is a bit of a hack, but that is the case for a while already, so perhaps it's not something to address in this PR. -- 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