forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent flattening of ordered and unordered interval sources (elastic…
…#114234) This PR applies a temporary patch to fix an issue with ordered and unordered intervals source. The flattening that is applied in Lucene modifies the final gap preventing valid queries to match. The fix already exists in Lucene but will be released in Lucene 10.x later this year. Since the bug prevents the combination of ordered and unordered intervals with gaps, this change applies a workaround to ensure that the bug is fixed in Elasticsearch 8x. Relates elastic#113554
- Loading branch information
Showing
6 changed files
with
180 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 114234 | ||
summary: Prevent flattening of ordered and unordered interval sources | ||
area: Search | ||
type: bug | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
server/src/main/java/org/elasticsearch/index/query/XIntervals.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
package org.elasticsearch.index.query; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.queries.intervals.IntervalIterator; | ||
import org.apache.lucene.queries.intervals.IntervalMatchesIterator; | ||
import org.apache.lucene.queries.intervals.Intervals; | ||
import org.apache.lucene.queries.intervals.IntervalsSource; | ||
import org.apache.lucene.search.QueryVisitor; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Copy of {@link Intervals} that exposes versions of {@link Intervals#ordered} and {@link Intervals#unordered} | ||
* that preserve their inner gaps. | ||
* NOTE: Remove this hack when a version of Lucene with https://github.com/apache/lucene/pull/13819 is used (10.1.0). | ||
*/ | ||
public final class XIntervals { | ||
|
||
/** | ||
* Create an ordered {@link IntervalsSource} | ||
* | ||
* <p>Returns intervals in which the subsources all appear in the given order | ||
* | ||
* @param subSources an ordered set of {@link IntervalsSource} objects | ||
*/ | ||
public static IntervalsSource ordered(IntervalsSource... subSources) { | ||
return new DelegateIntervalsSource(Intervals.ordered(subSources)); | ||
} | ||
|
||
/** | ||
* Create an ordered {@link IntervalsSource} | ||
* | ||
* <p>Returns intervals in which the subsources all appear in the given order | ||
* | ||
* @param subSources an ordered set of {@link IntervalsSource} objects | ||
*/ | ||
public static IntervalsSource unordered(IntervalsSource... subSources) { | ||
return new DelegateIntervalsSource(Intervals.unordered(subSources)); | ||
} | ||
|
||
/** | ||
* Wraps a source to avoid aggressive flattening of the ordered and unordered sources. | ||
* The flattening modifies the final gap and is removed in the latest unreleased version of Lucene (10.1). | ||
*/ | ||
private static class DelegateIntervalsSource extends IntervalsSource { | ||
private final IntervalsSource delegate; | ||
|
||
private DelegateIntervalsSource(IntervalsSource delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public IntervalIterator intervals(String field, LeafReaderContext ctx) throws IOException { | ||
return delegate.intervals(field, ctx); | ||
} | ||
|
||
@Override | ||
public IntervalMatchesIterator matches(String field, LeafReaderContext ctx, int doc) throws IOException { | ||
return delegate.matches(field, ctx, doc); | ||
} | ||
|
||
@Override | ||
public void visit(String field, QueryVisitor visitor) { | ||
delegate.visit(field, visitor); | ||
} | ||
|
||
@Override | ||
public int minExtent() { | ||
return delegate.minExtent(); | ||
} | ||
|
||
@Override | ||
public Collection<IntervalsSource> pullUpDisjunctions() { | ||
return delegate.pullUpDisjunctions(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DelegateIntervalsSource that = (DelegateIntervalsSource) o; | ||
return Objects.equals(delegate, that.delegate); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(delegate); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return delegate.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters