diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index d7e1b3902bf..d00972b4d0e 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -84,6 +84,10 @@ Bug Fixes that led to IllegalStateException being thrown when nothing was wrong. (David Smiley, yonik) +* LUCENE-7219: Make queryparser/xml (Point|LegacyNumeric)RangeQuery builders + match the underlying queries' (lower|upper)Term optionality logic. + (Kaneshanathan Srivisagan, Christine Poerschke) + Documentation * LUCENE-7223: Improve XXXPoint javadocs to make it clear that you diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java index e19596430e2..f7aef3f477b 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/LegacyNumericRangeQueryBuilder.java @@ -45,14 +45,14 @@ import org.w3c.dom.Element; * * lowerTerm * Specified by type - * Yes - * N/A + * No + * Null * * * upperTerm * Specified by type - * Yes - * N/A + * No + * Null * * * type @@ -91,8 +91,8 @@ public class LegacyNumericRangeQueryBuilder implements QueryBuilder { @Override public Query getQuery(Element e) throws ParserException { String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); - String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm"); - String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm"); + final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null); + final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null); boolean lowerInclusive = DOMUtils.getAttribute(e, "includeLower", true); boolean upperInclusive = DOMUtils.getAttribute(e, "includeUpper", true); int precisionStep = DOMUtils.getAttribute(e, "precisionStep", LegacyNumericUtils.PRECISION_STEP_DEFAULT); @@ -101,20 +101,28 @@ public class LegacyNumericRangeQueryBuilder implements QueryBuilder { try { Query filter; if (type.equalsIgnoreCase("int")) { - filter = LegacyNumericRangeQuery.newIntRange(field, precisionStep, Integer - .valueOf(lowerTerm), Integer.valueOf(upperTerm), lowerInclusive, + filter = LegacyNumericRangeQuery.newIntRange(field, precisionStep, + (lowerTerm == null ? null : Integer.valueOf(lowerTerm)), + (upperTerm == null ? null : Integer.valueOf(upperTerm)), + lowerInclusive, upperInclusive); } else if (type.equalsIgnoreCase("long")) { - filter = LegacyNumericRangeQuery.newLongRange(field, precisionStep, Long - .valueOf(lowerTerm), Long.valueOf(upperTerm), lowerInclusive, + filter = LegacyNumericRangeQuery.newLongRange(field, precisionStep, + (lowerTerm == null ? null : Long.valueOf(lowerTerm)), + (upperTerm == null ? null : Long.valueOf(upperTerm)), + lowerInclusive, upperInclusive); } else if (type.equalsIgnoreCase("double")) { - filter = LegacyNumericRangeQuery.newDoubleRange(field, precisionStep, Double - .valueOf(lowerTerm), Double.valueOf(upperTerm), lowerInclusive, + filter = LegacyNumericRangeQuery.newDoubleRange(field, precisionStep, + (lowerTerm == null ? null : Double.valueOf(lowerTerm)), + (upperTerm == null ? null : Double.valueOf(upperTerm)), + lowerInclusive, upperInclusive); } else if (type.equalsIgnoreCase("float")) { - filter = LegacyNumericRangeQuery.newFloatRange(field, precisionStep, Float - .valueOf(lowerTerm), Float.valueOf(upperTerm), lowerInclusive, + filter = LegacyNumericRangeQuery.newFloatRange(field, precisionStep, + (lowerTerm == null ? null : Float.valueOf(lowerTerm)), + (upperTerm == null ? null : Float.valueOf(upperTerm)), + lowerInclusive, upperInclusive); } else { throw new ParserException("type attribute must be one of: [long, int, double, float]"); diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java index 45483168bac..82f7039f0be 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/xml/builders/PointRangeQueryBuilder.java @@ -46,14 +46,14 @@ import org.w3c.dom.Element; * * lowerTerm * Specified by type - * Yes - * N/A + * No + * Integer.MIN_VALUE Long.MIN_VALUE Float.NEGATIVE_INFINITY Double.NEGATIVE_INFINITY * * * upperTerm * Specified by type - * Yes - * N/A + * No + * Integer.MAX_VALUE Long.MAX_VALUE Float.POSITIVE_INFINITY Double.POSITIVE_INFINITY * * * type @@ -72,19 +72,27 @@ public class PointRangeQueryBuilder implements QueryBuilder { @Override public Query getQuery(Element e) throws ParserException { String field = DOMUtils.getAttributeWithInheritanceOrFail(e, "fieldName"); - String lowerTerm = DOMUtils.getAttributeOrFail(e, "lowerTerm"); - String upperTerm = DOMUtils.getAttributeOrFail(e, "upperTerm"); + final String lowerTerm = DOMUtils.getAttribute(e, "lowerTerm", null); + final String upperTerm = DOMUtils.getAttribute(e, "upperTerm", null); String type = DOMUtils.getAttribute(e, "type", "int"); try { if (type.equalsIgnoreCase("int")) { - return IntPoint.newRangeQuery(field, Integer.valueOf(lowerTerm), Integer.valueOf(upperTerm)); + return IntPoint.newRangeQuery(field, + (lowerTerm == null ? Integer.MIN_VALUE : Integer.valueOf(lowerTerm)), + (upperTerm == null ? Integer.MAX_VALUE : Integer.valueOf(upperTerm))); } else if (type.equalsIgnoreCase("long")) { - return LongPoint.newRangeQuery(field, Long.valueOf(lowerTerm), Long.valueOf(upperTerm)); + return LongPoint.newRangeQuery(field, + (lowerTerm == null ? Long.MIN_VALUE : Long.valueOf(lowerTerm)), + (upperTerm == null ? Long.MAX_VALUE : Long.valueOf(upperTerm))); } else if (type.equalsIgnoreCase("double")) { - return DoublePoint.newRangeQuery(field, Double.valueOf(lowerTerm), Double.valueOf(upperTerm)); + return DoublePoint.newRangeQuery(field, + (lowerTerm == null ? Double.NEGATIVE_INFINITY : Double.valueOf(lowerTerm)), + (upperTerm == null ? Double.POSITIVE_INFINITY : Double.valueOf(upperTerm))); } else if (type.equalsIgnoreCase("float")) { - return FloatPoint.newRangeQuery(field, Float.valueOf(lowerTerm), Float.valueOf(upperTerm)); + return FloatPoint.newRangeQuery(field, + (lowerTerm == null ? Float.NEGATIVE_INFINITY : Float.valueOf(lowerTerm)), + (upperTerm == null ? Float.POSITIVE_INFINITY : Float.valueOf(upperTerm))); } else { throw new ParserException("type attribute must be one of: [long, int, double, float]"); } diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutLowerTerm.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutLowerTerm.xml new file mode 100644 index 00000000000..54de55fd0f3 --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutLowerTerm.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutRange.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutRange.xml new file mode 100644 index 00000000000..764f6548d76 --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutRange.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutUpperTerm.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutUpperTerm.xml new file mode 100644 index 00000000000..d3a61c183b3 --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/LegacyNumericRangeQueryWithoutUpperTerm.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutLowerTerm.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutLowerTerm.xml new file mode 100644 index 00000000000..2159c2cee05 --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutLowerTerm.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutRange.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutRange.xml new file mode 100644 index 00000000000..dc18953358f --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutRange.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutUpperTerm.xml b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutUpperTerm.xml new file mode 100644 index 00000000000..eca8573265f --- /dev/null +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/PointRangeQueryWithoutUpperTerm.xml @@ -0,0 +1,31 @@ + + + + + merger + + + sumitomo + + + bank + + + + + diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java index 82426d06076..4cf5fcffe2f 100644 --- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/xml/TestCoreParser.java @@ -132,12 +132,42 @@ public class TestCoreParser extends LuceneTestCase { Query q = parse("LegacyNumericRangeQuery.xml"); dumpResults("LegacyNumericRangeQuery", q, 5); } + + public void testNumericRangeQueryXMLWithoutLowerTerm() throws ParserException, IOException { + Query q = parse("LegacyNumericRangeQueryWithoutLowerTerm.xml"); + dumpResults("LegacyNumericRangeQueryWithoutLowerTerm", q, 5); + } + + public void testNumericRangeQueryXMLWithoutUpperTerm() throws ParserException, IOException { + Query q = parse("LegacyNumericRangeQueryWithoutUpperTerm.xml"); + dumpResults("LegacyNumericRangeQueryWithoutUpperTerm", q, 5); + } + + public void testNumericRangeQueryXMLWithoutRange() throws ParserException, IOException { + Query q = parse("LegacyNumericRangeQueryWithoutRange.xml"); + dumpResults("LegacyNumericRangeQueryWithoutRange", q, 5); + } public void testPointRangeQuery() throws ParserException, IOException { Query q = parse("PointRangeQuery.xml"); dumpResults("PointRangeQuery", q, 5); } + public void testPointRangeQueryWithoutLowerTerm() throws ParserException, IOException { + Query q = parse("PointRangeQueryWithoutLowerTerm.xml"); + dumpResults("PointRangeQueryWithoutLowerTerm", q, 5); + } + + public void testPointRangeQueryWithoutUpperTerm() throws ParserException, IOException { + Query q = parse("PointRangeQueryWithoutUpperTerm.xml"); + dumpResults("PointRangeQueryWithoutUpperTerm", q, 5); + } + + public void testPointRangeQueryWithoutRange() throws ParserException, IOException { + Query q = parse("PointRangeQueryWithoutRange.xml"); + dumpResults("PointRangeQueryWithoutRange", q, 5); + } + //================= Helper methods =================================== protected String defaultField() {