Removing TermInSetQuery varargs ctor (#12837)

This commit is contained in:
Jakub Slowinski 2023-12-05 22:35:46 +00:00 committed by GitHub
parent 880d0ba1a8
commit b22a9b8998
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 86 additions and 68 deletions

View File

@ -70,6 +70,9 @@ API Changes
* GITHUB#12873: Expressions module now uses MethodHandles to define custom functions. Support for
custom classloaders was removed. (Uwe Schindler)
* GITHUB#12243: Remove TermInSetQuery ctors taking varargs param. SortedSetDocValuesField#newSlowSetQuery,
SortedDocValuesField#newSlowSetQuery, KeywordField#newSetQuery, KeywordField#newSetQuery now take a collection. (Jakub Slowinski)
New Features
---------------------
@ -150,7 +153,8 @@ Other
API Changes
---------------------
(No changes)
* GITHUB#12243: Mark TermInSetQuery ctors with varargs terms as @Deprecated. SortedSetDocValuesField#newSlowSetQuery,
SortedDocValuesField#newSlowSetQuery, KeywordField#newSetQuery now take a collection of terms as a param. (Jakub Slowinski)
New Features
---------------------

View File

@ -16,6 +16,7 @@
*/
package org.apache.lucene.document;
import java.util.Collection;
import java.util.Objects;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexOptions;
@ -171,7 +172,7 @@ public class KeywordField extends Field {
* @throws NullPointerException if {@code field} is null.
* @return a query matching documents with this exact value
*/
public static Query newSetQuery(String field, BytesRef... values) {
public static Query newSetQuery(String field, Collection<BytesRef> values) {
Objects.requireNonNull(field, "field must not be null");
Objects.requireNonNull(values, "values must not be null");
Query indexQuery = new TermInSetQuery(field, values);

View File

@ -16,6 +16,7 @@
*/
package org.apache.lucene.document;
import java.util.Collection;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MultiTermQuery;
@ -99,7 +100,7 @@ public class SortedDocValuesField extends Field {
* in an {@link IndexOrDocValuesQuery}, alongside a set query that executes on postings, such as
* {@link TermInSetQuery}.
*/
public static Query newSlowSetQuery(String field, BytesRef... values) {
public static Query newSlowSetQuery(String field, Collection<BytesRef> values) {
return new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, field, values);
}
}

View File

@ -16,6 +16,7 @@
*/
package org.apache.lucene.document;
import java.util.Collection;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.MultiTermQuery;
@ -103,7 +104,7 @@ public class SortedSetDocValuesField extends Field {
* in an {@link IndexOrDocValuesQuery}, alongside a set query that executes on postings, such as
* {@link TermInSetQuery}.
*/
public static Query newSlowSetQuery(String field, BytesRef... values) {
public static Query newSlowSetQuery(String field, Collection<BytesRef> values) {
return new TermInSetQuery(MultiTermQuery.DOC_VALUES_REWRITE, field, values);
}
}

View File

@ -18,7 +18,6 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.SortedSet;
@ -83,10 +82,6 @@ public class TermInSetQuery extends MultiTermQuery implements Accountable {
this(field, packTerms(field, terms));
}
public TermInSetQuery(String field, BytesRef... terms) {
this(field, packTerms(field, Arrays.asList(terms)));
}
/** Creates a new {@link TermInSetQuery} from the given collection of terms. */
public TermInSetQuery(RewriteMethod rewriteMethod, String field, Collection<BytesRef> terms) {
super(field, rewriteMethod);
@ -95,11 +90,6 @@ public class TermInSetQuery extends MultiTermQuery implements Accountable {
termDataHashCode = termData.hashCode();
}
/** Creates a new {@link TermInSetQuery} from the given array of terms. */
public TermInSetQuery(RewriteMethod rewriteMethod, String field, BytesRef... terms) {
this(rewriteMethod, field, Arrays.asList(terms));
}
private TermInSetQuery(String field, PrefixCodedTerms termData) {
super(field, MultiTermQuery.CONSTANT_SCORE_BLENDED_REWRITE);
this.field = field;

View File

@ -17,7 +17,9 @@
package org.apache.lucene.document;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
@ -125,15 +127,15 @@ public class TestKeywordField extends LuceneTestCase {
}
public void testValueClone() {
BytesRef[] values = new BytesRef[100];
List<BytesRef> values = new ArrayList<>(100);
for (int i = 0; i < 100; i++) {
String s = TestUtil.randomSimpleString(random(), 10, 20);
values[i] = new BytesRef(s);
values.add(new BytesRef(s));
}
// Make sure we don't modify the input values array.
BytesRef[] expected = values.clone();
List<BytesRef> expected = new ArrayList<>(values);
KeywordField.newSetQuery("f", values);
assertArrayEquals(expected, values);
assertEquals(expected, values);
}
}

View File

@ -61,29 +61,20 @@ public class TestSortedSetDocValuesSetQuery extends LuceneTestCase {
List<BytesRef> terms = new ArrayList<>();
terms.add(new BytesRef("5"));
results =
searcher.search(
SortedDocValuesField.newSlowSetQuery(fieldName, terms.toArray(new BytesRef[0])),
numDocs)
.scoreDocs;
searcher.search(SortedDocValuesField.newSlowSetQuery(fieldName, terms), numDocs).scoreDocs;
assertEquals("Must match nothing", 0, results.length);
terms = new ArrayList<>();
terms.add(new BytesRef("10"));
results =
searcher.search(
SortedDocValuesField.newSlowSetQuery(fieldName, terms.toArray(new BytesRef[0])),
numDocs)
.scoreDocs;
searcher.search(SortedDocValuesField.newSlowSetQuery(fieldName, terms), numDocs).scoreDocs;
assertEquals("Must match 1", 1, results.length);
terms = new ArrayList<>();
terms.add(new BytesRef("10"));
terms.add(new BytesRef("20"));
results =
searcher.search(
SortedDocValuesField.newSlowSetQuery(fieldName, terms.toArray(new BytesRef[0])),
numDocs)
.scoreDocs;
searcher.search(SortedDocValuesField.newSlowSetQuery(fieldName, terms), numDocs).scoreDocs;
assertEquals("Must match 2", 2, results.length);
reader.close();
@ -91,21 +82,39 @@ public class TestSortedSetDocValuesSetQuery extends LuceneTestCase {
}
public void testEquals() {
List<BytesRef> bar = new ArrayList<>();
bar.add(new BytesRef("bar"));
List<BytesRef> barbar = new ArrayList<>();
barbar.add(new BytesRef("bar"));
barbar.add(new BytesRef("bar"));
List<BytesRef> barbaz = new ArrayList<>();
barbaz.add(new BytesRef("bar"));
barbaz.add(new BytesRef("baz"));
List<BytesRef> bazbar = new ArrayList<>();
bazbar.add(new BytesRef("baz"));
bazbar.add(new BytesRef("bar"));
List<BytesRef> baz = new ArrayList<>();
baz.add(new BytesRef("baz"));
assertEquals(
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar")),
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar")));
SortedDocValuesField.newSlowSetQuery("foo", bar),
SortedDocValuesField.newSlowSetQuery("foo", bar));
assertEquals(
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar")),
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar"), new BytesRef("bar")));
SortedDocValuesField.newSlowSetQuery("foo", bar),
SortedDocValuesField.newSlowSetQuery("foo", barbar));
assertEquals(
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar"), new BytesRef("baz")),
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("baz"), new BytesRef("bar")));
assertFalse(
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar"))
.equals(SortedDocValuesField.newSlowSetQuery("foo2", new BytesRef("bar"))));
assertFalse(
SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("bar"))
.equals(SortedDocValuesField.newSlowSetQuery("foo", new BytesRef("baz"))));
SortedDocValuesField.newSlowSetQuery("foo", barbaz),
SortedDocValuesField.newSlowSetQuery("foo", bazbar));
assertNotEquals(
SortedDocValuesField.newSlowSetQuery("foo", bar),
SortedDocValuesField.newSlowSetQuery("foo2", bar));
assertNotEquals(
SortedDocValuesField.newSlowSetQuery("foo", bar),
SortedDocValuesField.newSlowSetQuery("foo", baz));
}
public void testDuelTermsQuery() throws IOException {
@ -159,9 +168,7 @@ public class TestSortedSetDocValuesSetQuery extends LuceneTestCase {
bytesTerms.add(term.bytes());
}
final Query q2 =
new BoostQuery(
SortedDocValuesField.newSlowSetQuery("f", bytesTerms.toArray(new BytesRef[0])),
boost);
new BoostQuery(SortedDocValuesField.newSlowSetQuery("f", bytesTerms), boost);
assertSameMatches(searcher, q1, q2, true);
}
@ -221,9 +228,7 @@ public class TestSortedSetDocValuesSetQuery extends LuceneTestCase {
bytesTerms.add(term.bytes());
}
final Query q2 =
new BoostQuery(
SortedDocValuesField.newSlowSetQuery("f", bytesTerms.toArray(new BytesRef[0])),
boost);
new BoostQuery(SortedDocValuesField.newSlowSetQuery("f", bytesTerms), boost);
BooleanQuery.Builder bq1 = new BooleanQuery.Builder();
bq1.add(q1, Occur.MUST);

View File

@ -193,40 +193,42 @@ public class TestTermInSetQuery extends LuceneTestCase {
}
}
TermInSetQuery tq1 = new TermInSetQuery("thing", newBytesRef("apple"));
TermInSetQuery tq2 = new TermInSetQuery("thing", newBytesRef("orange"));
TermInSetQuery tq1 = new TermInSetQuery("thing", List.of(newBytesRef("apple")));
TermInSetQuery tq2 = new TermInSetQuery("thing", List.of(newBytesRef("orange")));
assertFalse(tq1.hashCode() == tq2.hashCode());
// different fields with the same term should have differing hashcodes
tq1 = new TermInSetQuery("thing", newBytesRef("apple"));
tq2 = new TermInSetQuery("thing2", newBytesRef("apple"));
tq1 = new TermInSetQuery("thing", List.of(newBytesRef("apple")));
tq2 = new TermInSetQuery("thing2", List.of(newBytesRef("apple")));
assertFalse(tq1.hashCode() == tq2.hashCode());
}
public void testSimpleEquals() {
// Two terms with the same hash code
assertEquals("AaAaBB".hashCode(), "BBBBBB".hashCode());
TermInSetQuery left = new TermInSetQuery("id", newBytesRef("AaAaAa"), newBytesRef("AaAaBB"));
TermInSetQuery right = new TermInSetQuery("id", newBytesRef("AaAaAa"), newBytesRef("BBBBBB"));
TermInSetQuery left =
new TermInSetQuery("id", List.of(newBytesRef("AaAaAa"), newBytesRef("AaAaBB")));
TermInSetQuery right =
new TermInSetQuery("id", List.of(newBytesRef("AaAaAa"), newBytesRef("BBBBBB")));
assertFalse(left.equals(right));
}
public void testToString() {
TermInSetQuery termsQuery =
new TermInSetQuery("field1", newBytesRef("a"), newBytesRef("b"), newBytesRef("c"));
new TermInSetQuery("field1", List.of(newBytesRef("a"), newBytesRef("b"), newBytesRef("c")));
assertEquals("field1:(a b c)", termsQuery.toString());
}
public void testDedup() {
Query query1 = new TermInSetQuery("foo", newBytesRef("bar"));
Query query2 = new TermInSetQuery("foo", newBytesRef("bar"), newBytesRef("bar"));
Query query1 = new TermInSetQuery("foo", List.of(newBytesRef("bar")));
Query query2 = new TermInSetQuery("foo", List.of(newBytesRef("bar"), newBytesRef("bar")));
QueryUtils.checkEqual(query1, query2);
}
public void testOrderDoesNotMatter() {
// order of terms if different
Query query1 = new TermInSetQuery("foo", newBytesRef("bar"), newBytesRef("baz"));
Query query2 = new TermInSetQuery("foo", newBytesRef("baz"), newBytesRef("bar"));
Query query1 = new TermInSetQuery("foo", List.of(newBytesRef("bar"), newBytesRef("baz")));
Query query2 = new TermInSetQuery("foo", List.of(newBytesRef("baz"), newBytesRef("bar")));
QueryUtils.checkEqual(query1, query2);
}
@ -346,12 +348,13 @@ public class TestTermInSetQuery extends LuceneTestCase {
public void testBinaryToString() {
TermInSetQuery query =
new TermInSetQuery("field", newBytesRef(new byte[] {(byte) 0xff, (byte) 0xfe}));
new TermInSetQuery("field", List.of(newBytesRef(new byte[] {(byte) 0xff, (byte) 0xfe})));
assertEquals("field:([ff fe])", query.toString());
}
public void testIsConsideredCostlyByQueryCache() throws IOException {
TermInSetQuery query = new TermInSetQuery("foo", newBytesRef("bar"), newBytesRef("baz"));
TermInSetQuery query =
new TermInSetQuery("foo", List.of(newBytesRef("bar"), newBytesRef("baz")));
UsageTrackingQueryCachingPolicy policy = new UsageTrackingQueryCachingPolicy();
assertFalse(policy.shouldCache(query));
policy.onUse(query);
@ -362,7 +365,7 @@ public class TestTermInSetQuery extends LuceneTestCase {
public void testVisitor() {
// singleton reports back to consumeTerms()
TermInSetQuery singleton = new TermInSetQuery("field", newBytesRef("term1"));
TermInSetQuery singleton = new TermInSetQuery("field", List.of(newBytesRef("term1")));
singleton.visit(
new QueryVisitor() {
@Override

View File

@ -17,6 +17,7 @@
package org.apache.lucene.monitor;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@ -125,7 +126,7 @@ public class MultipassTermFilteredPresearcher extends TermFilteredPresearcher {
BooleanQuery.Builder child = new BooleanQuery.Builder();
for (String field : terms.keySet()) {
child.add(
new TermInSetQuery(field(field, i), collectedTerms.get(field)),
new TermInSetQuery(field(field, i), Arrays.asList(collectedTerms.get(field))),
BooleanClause.Occur.SHOULD);
}
parent.add(child.build(), BooleanClause.Occur.MUST);

View File

@ -18,6 +18,7 @@
package org.apache.lucene.monitor;
import java.io.IOException;
import java.util.List;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
@ -111,25 +112,33 @@ public class TestMultipassPresearcher extends PresearcherTestBase {
must(
new BooleanQuery.Builder()
.add(
should(new TermInSetQuery("f_0", new BytesRef("test"))))
should(
new TermInSetQuery(
"f_0", List.of(new BytesRef("test")))))
.build()))
.add(
must(
new BooleanQuery.Builder()
.add(
should(new TermInSetQuery("f_1", new BytesRef("test"))))
should(
new TermInSetQuery(
"f_1", List.of(new BytesRef("test")))))
.build()))
.add(
must(
new BooleanQuery.Builder()
.add(
should(new TermInSetQuery("f_2", new BytesRef("test"))))
should(
new TermInSetQuery(
"f_2", List.of(new BytesRef("test")))))
.build()))
.add(
must(
new BooleanQuery.Builder()
.add(
should(new TermInSetQuery("f_3", new BytesRef("test"))))
should(
new TermInSetQuery(
"f_3", List.of(new BytesRef("test")))))
.build()))
.build()))
.add(should(new TermQuery(new Term("__anytokenfield", "__ANYTOKEN__"))))

View File

@ -19,6 +19,7 @@ package org.apache.lucene.monitor;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.analysis.core.KeywordAnalyzer;
import org.apache.lucene.index.DirectoryReader;
@ -144,7 +145,7 @@ public class TestTermPresearcher extends PresearcherTestBase {
.add(
should(
new BooleanQuery.Builder()
.add(should(new TermInSetQuery("f", new BytesRef("test"))))
.add(should(new TermInSetQuery("f", List.of(new BytesRef("test")))))
.build()))
.add(should(new TermQuery(new Term("__anytokenfield", "__ANYTOKEN__"))))
.build();