Skip to content

Commit ba7caad

Browse files
authored
Avoid over-expanding unordered intervals over fixed-width disjunctions (#17084)
Unordered-near intervals over disjunctions currently rewrite themselves to a disjunction over the cartesian product of all possible unordered- near combinations so that minimization does not result in missing genuine matches. We have special handling for disjunctions where all the sub-intervals have a width of 1 which avoids this rewriting. This commit expands this handling to all sub-intervals of fixed width. IntervalsSource gains a new abstract maxExtent() method, which reports the widest possible extent of an interval returned by the source. Fixed-width intervals will return an identical minExtent() and maxExtent().
1 parent cae8a4c commit ba7caad

25 files changed

Lines changed: 429 additions & 26 deletions

‎lucene/CHANGES.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ API Changes
7979
* GITHUB#16741: Lower the minimum supported major version from 10 to 9. Indexes created with 9.x
8080
remain fully supported in Lucene 11, no reindexing required. (Luca Cavanna)
8181

82+
* GITHUB#17084: IntervalsSource has a new abstract method maxExtent(), the counterpart of
83+
minExtent(). Custom IntervalsSource implementations must implement it; return Integer.MAX_VALUE
84+
if the width of the intervals is not bounded. (Alan Woodward)
85+
8286
* GITHUB#14399: Sort pruning decisions are now entirely made through the Pruning
8387
enum. The various different locations that made these decisions are consolidated
8488
into FieldValueHitQueue, and early termination through examining index sorts is
@@ -330,6 +334,10 @@ Improvements
330334

331335
Optimizations
332336
---------------------
337+
* GITHUB#17084: Unordered, ordered and maxgaps intervals over a disjunction no longer expand
338+
alternatives that always match the same fixed width (such as single terms, wildcards and
339+
phrases of equal length) into a cross product of separate sources. (Alan Woodward)
340+
333341
* GITHUB#16145: Bound the prefetch backoff in MemorySegmentIndexInput. The gap between page-cache probes used to
334342
grow without limit, so under memory pressure prefetch effectively stopped working. A file now probes on every
335343
prefetch call until 1024 consecutive hits, then every 256th call; a miss goes back to probing on every call.

‎lucene/MIGRATE.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ with `VisitedSet.create(expectedVisitedNodes, graphSize)`.
2828

2929
## Migration from Lucene 10.x to Lucene 11.0
3030

31+
### IntervalsSource.maxExtent() is abstract (GITHUB#17084)
32+
33+
`IntervalsSource` has a new abstract method `maxExtent()`, the counterpart of `minExtent()`. It
34+
returns the largest width of any interval the source can return. Disjunctions use it to combine
35+
alternatives that always have the same width, which avoids expanding them into a cross product
36+
when they sit under an unordered, ordered or `maxgaps` source.
37+
38+
Custom `IntervalsSource` subclasses must implement it. If the width of your intervals is not
39+
bounded, or you are not sure, return `Integer.MAX_VALUE`, which is always safe. Only return a
40+
value equal to `minExtent()` if every interval your source returns has exactly that width in
41+
every document, including at the start and end of a field.
42+
3143
### Monitor's QueryTree subclasses implement collectTerms(TermCollector) (GITHUB#16997)
3244

3345
`QueryTree#collectTerms(TermCollector)` is now the abstract method, and

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/BlockIntervalsSource.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public int minExtent() {
6565
return minExtent;
6666
}
6767

68+
@Override
69+
public int maxExtent() {
70+
long maxExtent = 0;
71+
for (IntervalsSource subSource : subSources) {
72+
maxExtent += subSource.maxExtent();
73+
}
74+
return (int) Math.min(maxExtent, Integer.MAX_VALUE);
75+
}
76+
6877
@Override
6978
public Collection<IntervalsSource> pullUpDisjunctions() {
7079
return Collections.singletonList(this); // Disjunctions already pulled up in build()

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/ContainedByIntervalsSource.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public int minExtent() {
7979
return small.minExtent();
8080
}
8181

82+
@Override
83+
public int maxExtent() {
84+
return small.maxExtent();
85+
}
86+
8287
@Override
8388
public Collection<IntervalsSource> pullUpDisjunctions() {
8489
return Disjunctions.pullUp(big, s -> new ContainedByIntervalsSource(small, s));

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/ContainingIntervalsSource.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public int minExtent() {
7070
return big.minExtent();
7171
}
7272

73+
@Override
74+
public int maxExtent() {
75+
return big.maxExtent();
76+
}
77+
7378
@Override
7479
public Collection<IntervalsSource> pullUpDisjunctions() {
7580
return Disjunctions.pullUp(big, s -> new ContainingIntervalsSource(s, small));

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/DifferenceIntervalsSource.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,9 @@ public void visit(String field, QueryVisitor visitor) {
7575
public int minExtent() {
7676
return minuend.minExtent();
7777
}
78+
79+
@Override
80+
public int maxExtent() {
81+
return minuend.maxExtent();
82+
}
7883
}

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/DisjunctionIntervalsSource.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ public int minExtent() {
143143
return minExtent;
144144
}
145145

146+
@Override
147+
public int maxExtent() {
148+
int maxExtent = 0;
149+
for (IntervalsSource subSource : subSources) {
150+
maxExtent = Math.max(maxExtent, subSource.maxExtent());
151+
}
152+
return maxExtent;
153+
}
154+
146155
@Override
147156
public Collection<IntervalsSource> pullUpDisjunctions() {
148157
if (pullUpDisjunctions) {

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/Disjunctions.java‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.Collections;
22+
import java.util.LinkedHashMap;
2223
import java.util.List;
24+
import java.util.Map;
2325
import java.util.function.Function;
2426
import org.apache.lucene.search.IndexSearcher;
2527

@@ -73,24 +75,26 @@ public static List<IntervalsSource> pullUp(
7375
}
7476

7577
// Separate out disjunctions into individual sources
76-
// Clauses that have a minExtent of 1 are grouped together and treated as a single
77-
// source, as any overlapping intervals of length 1 can be treated as identical,
78-
// and we know that all combinatorial sources have a minExtent > 1
78+
// Clauses that always match intervals of the same fixed width are grouped together by width
79+
// and treated as a single source, as intervals of equal width can never strictly contain one
80+
// another, so minimization cannot cause one to hide another. This covers single terms (width 1)
81+
// and, for example, phrases of the same length. Variable-width clauses are kept separate.
7982
private static List<IntervalsSource> splitDisjunctions(IntervalsSource source) {
80-
List<IntervalsSource> singletons = new ArrayList<>();
81-
List<IntervalsSource> nonSingletons = new ArrayList<>();
83+
Map<Integer, List<IntervalsSource>> fixedByWidth = new LinkedHashMap<>();
84+
List<IntervalsSource> variableWidth = new ArrayList<>();
8285
for (IntervalsSource disj : source.pullUpDisjunctions()) {
83-
if (disj.minExtent() == 1) {
84-
singletons.add(disj);
86+
int width = disj.minExtent();
87+
if (width == disj.maxExtent()) {
88+
fixedByWidth.computeIfAbsent(width, _ -> new ArrayList<>()).add(disj);
8589
} else {
86-
nonSingletons.add(disj);
90+
variableWidth.add(disj);
8791
}
8892
}
8993
List<IntervalsSource> split = new ArrayList<>();
90-
if (singletons.size() > 0) {
91-
split.add(Intervals.or(singletons.toArray(IntervalsSource[]::new)));
94+
for (List<IntervalsSource> group : fixedByWidth.values()) {
95+
split.add(group.size() == 1 ? group.get(0) : Intervals.or(group));
9296
}
93-
split.addAll(nonSingletons);
97+
split.addAll(variableWidth);
9498
return split;
9599
}
96100
}

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/ExtendedIntervalsSource.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ public int minExtent() {
131131
return minExtent;
132132
}
133133

134+
@Override
135+
public int maxExtent() {
136+
if (before > 0) {
137+
// the start is clamped to 0, so intervals near the start of a document can be shorter
138+
return Integer.MAX_VALUE;
139+
}
140+
long maxExtent = (long) before + source.maxExtent() + after;
141+
return (int) Math.max(0, Math.min(maxExtent, Integer.MAX_VALUE));
142+
}
143+
134144
@Override
135145
public Collection<IntervalsSource> pullUpDisjunctions() {
136146
Collection<IntervalsSource> inner = source.pullUpDisjunctions();

‎lucene/queries/src/java/org/apache/lucene/queries/intervals/FilteredIntervalsSource.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,14 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.Objects;
24-
import java.util.stream.Collectors;
2524
import org.apache.lucene.index.LeafReaderContext;
2625
import org.apache.lucene.search.QueryVisitor;
2726

2827
/** An IntervalsSource that filters the intervals from another IntervalsSource */
2928
public abstract class FilteredIntervalsSource extends IntervalsSource {
3029

3130
public static IntervalsSource maxGaps(IntervalsSource in, int maxGaps) {
32-
return Intervals.or(
33-
in.pullUpDisjunctions().stream()
34-
.map(s -> new MaxGaps(s, maxGaps))
35-
.collect(Collectors.toList()));
31+
return Intervals.or(Disjunctions.pullUp(in, s -> new MaxGaps(s, maxGaps)));
3632
}
3733

3834
private static class MaxGaps extends FilteredIntervalsSource {
@@ -68,6 +64,11 @@ protected boolean accept(IntervalIterator it) {
6864
return (it.end() - it.start()) + 1 <= maxWidth;
6965
}
7066

67+
@Override
68+
public int maxExtent() {
69+
return Math.min(in.maxExtent(), maxWidth);
70+
}
71+
7172
@Override
7273
public Collection<IntervalsSource> pullUpDisjunctions() {
7374
return Disjunctions.pullUp(in, s -> new MaxWidth(s, maxWidth));
@@ -129,6 +130,11 @@ public int minExtent() {
129130
return in.minExtent();
130131
}
131132

133+
@Override
134+
public int maxExtent() {
135+
return in.maxExtent();
136+
}
137+
132138
@Override
133139
public Collection<IntervalsSource> pullUpDisjunctions() {
134140
return Collections.singletonList(this);

0 commit comments

Comments
 (0)