diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 163c09c3db2..2d0b8663a6c 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -222,6 +222,9 @@ Improvements * LUCENE-9455: ExitableTermsEnum should sample timeout and interruption check before calling next(). (Zach Chen via Bruno Roustant) +* LUCENE-9023: GlobalOrdinalsWithScore should not compute occurrences when the + provided min is 1. (Jim Ferenczi) + Optimizations --------------------- diff --git a/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreCollector.java b/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreCollector.java index ee9731fa5cd..56c6991c370 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreCollector.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreCollector.java @@ -47,7 +47,7 @@ abstract class GlobalOrdinalsWithScoreCollector implements Collector { throw new IllegalStateException("Can't collect more than [" + Integer.MAX_VALUE + "] ids"); } this.field = field; - this.doMinMax = !(min <= 0 && max == Integer.MAX_VALUE); + this.doMinMax = min > 1 || max < Integer.MAX_VALUE; this.min = min; this.max = max;; this.ordinalMap = ordinalMap; diff --git a/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java b/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java index 183bca19ca9..4a7ed927861 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/GlobalOrdinalsWithScoreQuery.java @@ -86,7 +86,7 @@ final class GlobalOrdinalsWithScoreQuery extends Query implements Accountable { if (searcher.getTopReaderContext().id() != indexReaderContextId) { throw new IllegalStateException("Creating the weight against a different index reader than this query has been built for."); } - boolean doNoMinMax = min <= 0 && max == Integer.MAX_VALUE; + boolean doNoMinMax = min <= 1 && max == Integer.MAX_VALUE; if (scoreMode.needsScores() == false && doNoMinMax) { // We don't need scores then quickly change the query to not uses the scores: GlobalOrdinalsQuery globalOrdinalsQuery = new GlobalOrdinalsQuery(collector.collectedOrds, joinField, globalOrds, diff --git a/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java b/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java index 0e933c04e28..b9d0a4369db 100644 --- a/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java +++ b/lucene/join/src/java/org/apache/lucene/search/join/JoinUtil.java @@ -496,7 +496,7 @@ public final class JoinUtil { globalOrdinalsWithScoreCollector = new GlobalOrdinalsWithScoreCollector.Avg(joinField, ordinalMap, valueCount, min, max); break; case None: - if (min <= 0 && max == Integer.MAX_VALUE) { + if (min <= 1 && max == Integer.MAX_VALUE) { GlobalOrdinalsCollector globalOrdinalsCollector = new GlobalOrdinalsCollector(joinField, ordinalMap, valueCount); searcher.search(rewrittenFromQuery, globalOrdinalsCollector); return new GlobalOrdinalsQuery(globalOrdinalsCollector.getCollectorOrdinals(), joinField, ordinalMap, rewrittenToQuery, diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java index 4d72b134daf..1803f03552d 100644 --- a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java +++ b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java @@ -640,7 +640,6 @@ public class TestJoinUtil extends LuceneTestCase { } assertEquals(expectedCount, collector.getTotalHits()); } - searcher.getIndexReader().close(); dir.close(); } @@ -657,8 +656,19 @@ public class TestJoinUtil extends LuceneTestCase { IndexReader reader = w.getReader(); IndexSearcher searcher = newSearcher(reader); OrdinalMap ordMap = OrdinalMap.build(null, new SortedDocValues[0], 0f); - Query joinQuery = JoinUtil.createJoinQuery("join_field", new MatchNoDocsQuery(), new MatchNoDocsQuery(), searcher, RandomPicks.randomFrom(random(), ScoreMode.values()), ordMap, 0, Integer.MAX_VALUE); - searcher.search(joinQuery, 1); // no exception due to missing rewrites + { + Query joinQuery = JoinUtil.createJoinQuery("join_field", new MatchNoDocsQuery(), + new MatchNoDocsQuery(), searcher, RandomPicks.randomFrom(random(), ScoreMode.values()), ordMap, 0, Integer.MAX_VALUE); + searcher.search(joinQuery, 1); // no exception due to missing rewrites + } + { + Query joinQuery = JoinUtil.createJoinQuery("join_field", new MatchNoDocsQuery(), + new MatchNoDocsQuery(), searcher, ScoreMode.None, ordMap, 1, Integer.MAX_VALUE); + Query rewritten = searcher.rewrite(joinQuery); + // should simplify to GlobalOrdinalsQuery since min is set to 1 + assertTrue(rewritten instanceof GlobalOrdinalsQuery); + searcher.search(joinQuery, 1); // no exception due to missing rewrites + } reader.close(); w.close(); dir.close();