Skip to content

Apache Lucene open-source search software

Prevent flattening of ordered and unordered interval sources (#13819)

Jim Ferenczi82b08045fd3c
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/BlockIntervalsSource.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/BlockIntervalsSource.java
index 91a5e94d5f2..efb03b154ef 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/BlockIntervalsSource.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/BlockIntervalsSource.java
@@ -38,6 +38,7 @@ class BlockIntervalsSource extends ConjunctionIntervalsSource {
List<IntervalsSource> flattened = new ArrayList<>();
for (IntervalsSource s : sources) {
if (s instanceof BlockIntervalsSource) {
+ // Block sources can be flattened because they do not increase the gap (gap = 0)
flattened.addAll(((BlockIntervalsSource) s).subSources);
} else {
flattened.add(s);
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java
index 65fa6d03395..a89df974b66 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/OrderedIntervalsSource.java
@@ -30,25 +30,13 @@ class OrderedIntervalsSource extends MinimizingConjunctionIntervalsSource {
if (sources.size() == 1) {
return sources.get(0);
}
- List<IntervalsSource> rewritten = deduplicate(flatten(sources));
+ List<IntervalsSource> rewritten = deduplicate(sources);
if (rewritten.size() == 1) {
return rewritten.get(0);
}
return new OrderedIntervalsSource(rewritten);
}
- private static List<IntervalsSource> flatten(List<IntervalsSource> sources) {
- List<IntervalsSource> flattened = new ArrayList<>();
- for (IntervalsSource s : sources) {
- if (s instanceof OrderedIntervalsSource) {
- flattened.addAll(((OrderedIntervalsSource) s).subSources);
- } else {
- flattened.add(s);
- }
- }
- return flattened;
- }
-
private static List<IntervalsSource> deduplicate(List<IntervalsSource> sources) {
List<IntervalsSource> deduplicated = new ArrayList<>();
List<IntervalsSource> current = new ArrayList<>();
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/UnorderedIntervalsSource.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/UnorderedIntervalsSource.java
index 132ab4b3976..d2c708b4fee 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/UnorderedIntervalsSource.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/UnorderedIntervalsSource.java
@@ -33,7 +33,7 @@ class UnorderedIntervalsSource extends MinimizingConjunctionIntervalsSource {
if (sources.size() == 1) {
return sources.get(0);
}
- List<IntervalsSource> rewritten = deduplicate(flatten(sources));
+ List<IntervalsSource> rewritten = deduplicate(sources);
if (rewritten.size() == 1) {
return rewritten.get(0);
}
@@ -55,18 +55,6 @@ class UnorderedIntervalsSource extends MinimizingConjunctionIntervalsSource {
return deduplicated;
}
- private static List<IntervalsSource> flatten(List<IntervalsSource> sources) {
- List<IntervalsSource> flattened = new ArrayList<>();
- for (IntervalsSource s : sources) {
- if (s instanceof UnorderedIntervalsSource) {
- flattened.addAll(((UnorderedIntervalsSource) s).subSources);
- } else {
- flattened.add(s);
- }
- }
- return flattened;
- }
-
private UnorderedIntervalsSource(List<IntervalsSource> sources) {
super(sources);
}
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java
index af2ec0230c2..a0539a0f852 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervalQuery.java
@@ -338,6 +338,18 @@ public class TestIntervalQuery extends LuceneTestCase {
checkHits(q, new int[] {6, 7});
}
+ public void testUnorderedWithNoGap() throws IOException {
+ Query q =
+ new IntervalQuery(
+ field,
+ Intervals.maxgaps(
+ 0,
+ Intervals.unordered(
+ Intervals.term("w3"),
+ Intervals.unordered(Intervals.term("w1"), Intervals.term("w5")))));
+ checkHits(q, new int[] {0});
+ }
+
public void testOrderedWithGaps() throws IOException {
Query q =
new IntervalQuery(
@@ -360,6 +372,18 @@ public class TestIntervalQuery extends LuceneTestCase {
checkHits(q, new int[] {12});
}
+ public void testOrderedWithNoGap() throws IOException {
+ Query q =
+ new IntervalQuery(
+ field,
+ Intervals.maxgaps(
+ 0,
+ Intervals.ordered(
+ Intervals.ordered(Intervals.term("w1"), Intervals.term("w4")),
+ Intervals.term("w5"))));
+ checkHits(q, new int[] {0});
+ }
+
public void testNestedOrInContainedBy() throws IOException {
Query q =
new IntervalQuery(