Apache Lucene open-source search software
Fix AbstractRangeQueryNode#toQueryString (#14697)
phb-ig17ca89d52fa9
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex e82318e4691..263555d02ab 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -22,6 +22,10 @@ API Changes* GITHUB#14291: Remove IOException from ScorerSupplier#setTopLevelScoringClause signature (Luca Cavanna)+* GITHUB#7865: Added public CharSequence getTermEscaped(EscapeQuerySyntax)+ to ValueQueryNode. Made existing implementations public in all classes which+ implement ValueQueryNode. (Peter Barna, Adam Schwartz)+New Features---------------------* GITHUB#14097: Binary partitioning merge policy over float-valued vector field. (Mike Sokolov)@@ -49,6 +53,10 @@ Bug Fixes* GITHUB#14075: Remove duplicate and add missing entry on brazilian portuguese stopwords list. (Arthur Caccavo)+* GITHUB#7865 AbstractRangeQueryNode#toQueryString(EscapeQuerySyntax) now+ returns a string which can be parsed back into the original node.+ (Peter Barna, Adam Schwartz)+Changes in Runtime Behavior---------------------* GITHUB#14187: The query cache is now disabled by default. (Adrien Grand)diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/FieldQueryNode.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/FieldQueryNode.javaindex db9c17eb2a9..1bdaa739e9d 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/FieldQueryNode.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/FieldQueryNode.java@@ -52,7 +52,8 @@ public class FieldQueryNode extends QueryNodeImplthis.setLeaf(true);}- protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {+ @Override+ public CharSequence getTermEscaped(EscapeQuerySyntax escaper) {return escaper.escape(this.text, Locale.getDefault(), EscapeQuerySyntax.Type.NORMAL);}diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ValueQueryNode.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ValueQueryNode.javaindex b9eda590c0f..862625f2d68 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ValueQueryNode.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/core/nodes/ValueQueryNode.java@@ -16,10 +16,31 @@*/package org.apache.lucene.queryparser.flexible.core.nodes;+import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax;+/** This interface should be implemented by {@link QueryNode} that holds an arbitrary value. */public interface ValueQueryNode<T extends Object> extends QueryNode {public void setValue(T value);public T getValue();++ /**+ * This method is used to get the value converted to {@link String} and escaped using the given+ * {@link EscapeQuerySyntax}. For example:+ *+ * <pre>+ * new FieldQueryNode("FIELD", "(literal parens)", 0, 0).getTermEscaped(escaper);+ * </pre>+ *+ * <p>returns+ *+ * <pre>+ * \(literal\ parens\)+ * </pre>+ *+ * @param escaper the {@link EscapeQuerySyntax} used to escape the value {@link String}+ * @return the value converted to {@link String} and escaped+ */+ public CharSequence getTermEscaped(EscapeQuerySyntax escaper);}diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/AbstractRangeQueryNode.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/AbstractRangeQueryNode.javaindex 4de8fe0d575..6780711d9d6 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/AbstractRangeQueryNode.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/AbstractRangeQueryNode.java@@ -169,6 +169,8 @@ public class AbstractRangeQueryNode<T extends FieldValuePairQueryNode<?>> extendT lower = getLowerBound();T upper = getUpperBound();+ sb.append(getField()).append(":");+if (lowerInclusive) {sb.append('[');@@ -177,16 +179,16 @@ public class AbstractRangeQueryNode<T extends FieldValuePairQueryNode<?>> extend}if (lower != null) {- sb.append(lower.toQueryString(escapeSyntaxParser));+ sb.append(lower.getTermEscaped(escapeSyntaxParser));} else {sb.append("...");}- sb.append(' ');+ sb.append(" TO ");if (upper != null) {- sb.append(upper.toQueryString(escapeSyntaxParser));+ sb.append(upper.getTermEscaped(escapeSyntaxParser));} else {sb.append("...");diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/PointQueryNode.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/PointQueryNode.javaindex 51ba96d8522..6b375738d83 100644--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/PointQueryNode.java+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/flexible/standard/nodes/PointQueryNode.java@@ -76,14 +76,8 @@ public class PointQueryNode extends QueryNodeImpl implements FieldValuePairQuerythis.field = fieldName;}- /**- * This method is used to get the value converted to {@link String} and escaped using the given- * {@link EscapeQuerySyntax}.- *- * @param escaper the {@link EscapeQuerySyntax} used to escape the value {@link String}- * @return the value converted to {@link String} and escaped- */- protected CharSequence getTermEscaped(EscapeQuerySyntax escaper) {+ @Override+ public CharSequence getTermEscaped(EscapeQuerySyntax escaper) {return escaper.escape(numberFormat.format(this.value), Locale.ROOT, Type.NORMAL);}diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/nodes/TestAbstractRangeQueryNode.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/nodes/TestAbstractRangeQueryNode.javanew file mode 100644index 00000000000..64aea9413f0--- /dev/null+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/nodes/TestAbstractRangeQueryNode.java@@ -0,0 +1,65 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.queryparser.flexible.standard.nodes;++import static org.hamcrest.Matchers.is;++import java.text.NumberFormat;+import java.util.Locale;+import org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode;+import org.apache.lucene.queryparser.flexible.core.parser.EscapeQuerySyntax;+import org.apache.lucene.queryparser.flexible.standard.StandardQueryParser;+import org.apache.lucene.queryparser.flexible.standard.config.PointsConfig;+import org.apache.lucene.queryparser.flexible.standard.parser.EscapeQuerySyntaxImpl;+import org.apache.lucene.search.Query;+import org.apache.lucene.tests.util.LuceneTestCase;++public class TestAbstractRangeQueryNode extends LuceneTestCase {+ private static final EscapeQuerySyntax escaper = new EscapeQuerySyntaxImpl();+ private static final StandardQueryParser parser = new StandardQueryParser();++ /* GITHUB#7865 bug in toQueryString() */+ public void testTermRangeQueryNode() throws Exception {+ FieldQueryNode lower = new FieldQueryNode("FIELD", "aaa", 0, 0);+ FieldQueryNode upper = new FieldQueryNode("FIELD", "zzz", 0, 0);+ TermRangeQueryNode origNode = new TermRangeQueryNode(lower, upper, true, true);+ CharSequence queryString = origNode.toQueryString(escaper);++ new FieldQueryNode("FIELD", "(literal parens)", 0, 0).getTermEscaped(escaper);++ // query string should have expected format and parse into a valid query+ assertThat(queryString, is("FIELD:[aaa TO zzz]"));+ Query parsedQuery = parser.parse(queryString.toString(), "");+ assertThat(parsedQuery.toString(), is(queryString));+ }++ /* GITHUB#7865 bug in toQueryString() */+ public void testPointRangeQueryNode() throws Exception {+ NumberFormat format = NumberFormat.getIntegerInstance(Locale.ROOT);+ PointQueryNode lower = new PointQueryNode("FIELD", 1, format);+ PointQueryNode upper = new PointQueryNode("FIELD", 999, format);++ PointRangeQueryNode origNode =+ new PointRangeQueryNode(lower, upper, true, true, new PointsConfig(format, Integer.class));+ CharSequence queryString = origNode.toQueryString(escaper);++ // query string should have expected format and parse into a valid query+ assertThat(queryString, is("FIELD:[1 TO 999]"));+ Query parsedQuery = parser.parse(queryString.toString(), "");+ assertThat(parsedQuery.toString(), is(queryString));+ }+}