Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-8893: Intervals.prefix() and Intervals.wildcard() should take BytesRef

Alan Woodward6966b49a6b64
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 25c874f9e05..877fcece9ac 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -50,6 +50,9 @@ API Changes
* LUCENE-8856: Intervals queries have moved from the sandbox to the queries
module. (Alan Woodward)
+* LUCENE-8893: Intervals.wildcard() and Intervals.prefix() methods now take
+ BytesRef rather than String. (Alan Woodward)
+
New Features
* LUCENE-8891: Snowball stemmer/analyzer for the Estonian language.
diff --git a/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java b/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java
index 3c1ee49d527..2f5570b8287 100644
--- a/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java
+++ b/lucene/queries/src/java/org/apache/lucene/queries/intervals/Intervals.java
@@ -146,7 +146,7 @@ public final class Intervals {
*
* @throws IllegalStateException if the prefix expands to more than 128 terms
*/
- public static IntervalsSource prefix(String prefix) {
+ public static IntervalsSource prefix(BytesRef prefix) {
return prefix(prefix, 128);
}
@@ -161,9 +161,9 @@ public final class Intervals {
*
* @throws IllegalStateException if the prefix expands to more than {@code maxExpansions} terms
*/
- public static IntervalsSource prefix(String prefix, int maxExpansions) {
- CompiledAutomaton ca = new CompiledAutomaton(PrefixQuery.toAutomaton(new BytesRef(prefix)));
- return new MultiTermIntervalsSource(ca, maxExpansions, prefix + "*");
+ public static IntervalsSource prefix(BytesRef prefix, int maxExpansions) {
+ CompiledAutomaton ca = new CompiledAutomaton(PrefixQuery.toAutomaton(prefix));
+ return new MultiTermIntervalsSource(ca, maxExpansions, prefix.utf8ToString() + "*");
}
/**
@@ -173,7 +173,7 @@ public final class Intervals {
*
* @see WildcardQuery for glob format
*/
- public static IntervalsSource wildcard(String wildcard) {
+ public static IntervalsSource wildcard(BytesRef wildcard) {
return wildcard(wildcard, 128);
}
@@ -190,9 +190,9 @@ public final class Intervals {
*
* @see WildcardQuery for glob format
*/
- public static IntervalsSource wildcard(String wildcard, int maxExpansions) {
+ public static IntervalsSource wildcard(BytesRef wildcard, int maxExpansions) {
CompiledAutomaton ca = new CompiledAutomaton(WildcardQuery.toAutomaton(new Term("", wildcard)));
- return new MultiTermIntervalsSource(ca, maxExpansions, wildcard);
+ return new MultiTermIntervalsSource(ca, maxExpansions, wildcard.utf8ToString());
}
/**
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervals.java b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervals.java
index a2d1eb8ccf5..e0c54a5a4c6 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervals.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/intervals/TestIntervals.java
@@ -38,6 +38,7 @@ import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchesIterator;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.AfterClass;
@@ -737,7 +738,7 @@ public class TestIntervals extends LuceneTestCase {
}
public void testPrefix() throws IOException {
- IntervalsSource source = Intervals.prefix("p");
+ IntervalsSource source = Intervals.prefix(new BytesRef("p"));
checkIntervals(source, "field1", 5, new int[][]{
{},
{ 0, 0, 1, 1, 3, 3, 4, 4, 6, 6, 7, 7, 10, 10, 27, 27 },
@@ -751,11 +752,11 @@ public class TestIntervals extends LuceneTestCase {
assertMatch(mi, 0, 0, 0, 5);
assertMatch(mi, 1, 1, 6, 14);
- IntervalsSource noSuch = Intervals.prefix("qqq");
+ IntervalsSource noSuch = Intervals.prefix(new BytesRef("qqq"));
checkIntervals(noSuch, "field1", 0, new int[][]{});
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
- IntervalsSource s = Intervals.prefix("p", 1);
+ IntervalsSource s = Intervals.prefix(new BytesRef("p"), 1);
for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
s.intervals("field1", ctx);
}
@@ -764,7 +765,7 @@ public class TestIntervals extends LuceneTestCase {
}
public void testWildcard() throws IOException {
- IntervalsSource source = Intervals.wildcard("?ot");
+ IntervalsSource source = Intervals.wildcard(new BytesRef("?ot"));
checkIntervals(source, "field1", 4, new int[][]{
{},
{ 2, 2, 10, 10, 17, 17, 27, 27 },
@@ -780,7 +781,7 @@ public class TestIntervals extends LuceneTestCase {
assertMatch(mi, 17, 17, 97, 100);
IllegalStateException e = expectThrows(IllegalStateException.class, () -> {
- IntervalsSource s = Intervals.wildcard("?ot", 1);
+ IntervalsSource s = Intervals.wildcard(new BytesRef("?ot"), 1);
for (LeafReaderContext ctx : searcher.getIndexReader().leaves()) {
s.intervals("field1", ctx);
}