mirror of https://github.com/apache/lucene.git
LUCENE-8918: Disallow null terms in PhraseQuery
This commit is contained in:
parent
241c44a82d
commit
ee09208293
|
@ -19,6 +19,9 @@ Improvements
|
|||
* LUCENE-8914: move the logic for discarding inner modes to the IntersectVisitor so we take advantage
|
||||
of the change introduced in LUCENE-7862. (Ignacio Vera)
|
||||
|
||||
* LUCENE-8918: PhraseQuery throws exceptions at construction time if it is passed
|
||||
null arguments. (Alan Woodward)
|
||||
|
||||
Other
|
||||
|
||||
* LUCENE-8778 LUCENE-8911: Define analyzer SPI names as static final fields and document the names in Javadocs.
|
||||
|
|
|
@ -22,6 +22,7 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat;
|
||||
|
@ -111,6 +112,7 @@ public class PhraseQuery extends Query {
|
|||
* all of them.
|
||||
*/
|
||||
public Builder add(Term term, int position) {
|
||||
Objects.requireNonNull(term, "Cannot add a null term to PhraseQuery");
|
||||
if (position < 0) {
|
||||
throw new IllegalArgumentException("Positions must be >= 0, got " + position);
|
||||
}
|
||||
|
@ -154,6 +156,9 @@ public class PhraseQuery extends Query {
|
|||
if (slop < 0) {
|
||||
throw new IllegalArgumentException("Slop must be >= 0, got " + slop);
|
||||
}
|
||||
for (Term term : terms) {
|
||||
Objects.requireNonNull(term, "Cannot add a null term to PhraseQuery");
|
||||
}
|
||||
for (int i = 1; i < terms.length; ++i) {
|
||||
if (terms[i-1].field().equals(terms[i].field()) == false) {
|
||||
throw new IllegalArgumentException("All terms should have the same field");
|
||||
|
@ -187,6 +192,7 @@ public class PhraseQuery extends Query {
|
|||
private static Term[] toTerms(String field, String... termStrings) {
|
||||
Term[] terms = new Term[termStrings.length];
|
||||
for (int i = 0; i < terms.length; ++i) {
|
||||
Objects.requireNonNull(termStrings[i], "Cannot add a null term to PhraseQuery");
|
||||
terms[i] = new Term(field, termStrings[i]);
|
||||
}
|
||||
return terms;
|
||||
|
@ -195,6 +201,7 @@ public class PhraseQuery extends Query {
|
|||
private static Term[] toTerms(String field, BytesRef... termBytes) {
|
||||
Term[] terms = new Term[termBytes.length];
|
||||
for (int i = 0; i < terms.length; ++i) {
|
||||
Objects.requireNonNull(termBytes[i], "Cannot add a null term to PhraseQuery");
|
||||
terms[i] = new Term(field, termBytes[i]);
|
||||
}
|
||||
return terms;
|
||||
|
|
|
@ -1071,4 +1071,15 @@ public class TestPhraseQuery extends LuceneTestCase {
|
|||
reader.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
public void testNullTerm() {
|
||||
NullPointerException e = expectThrows(NullPointerException.class, () -> new PhraseQuery.Builder().add(null));
|
||||
assertEquals("Cannot add a null term to PhraseQuery", e.getMessage());
|
||||
|
||||
e = expectThrows(NullPointerException.class, () -> new PhraseQuery("field", (BytesRef)null));
|
||||
assertEquals("Cannot add a null term to PhraseQuery", e.getMessage());
|
||||
|
||||
e = expectThrows(NullPointerException.class, () -> new PhraseQuery("field", (String)null));
|
||||
assertEquals("Cannot add a null term to PhraseQuery", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue