| diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt |
| index ab88bcb53db..f08ceaf34b6 100644 |
| --- a/lucene/CHANGES.txt |
| +++ b/lucene/CHANGES.txt |
| @@ -58,6 +58,10 @@ API Changes |
| |
| * LUCENE-9340: SimpleBindings#add(SortField) has been removed. (Alan Woodward) |
| |
| +* LUCENE-6807: Added public CharSequence getValueAsText() to ValueQueryNode. |
| + Added implementations to all existing classes which implement |
| + ValueQueryNode. (Peter Barna, Adam Schwartz) |
| + |
| Improvements |
| |
| * LUCENE-9370: RegExp query is no longer lenient about inappropriate backslashes and |
| @@ -121,6 +125,10 @@ Bug fixes |
| * LUCENE-9372: Fix paths for cygwin/msys before gradle wrapper jar lookup. |
| (Peter Barna) |
| |
| +* LUCENE-6807: AbstractRangeQueryNode#toQueryString(EscapeQuerySyntax) now |
| + returns a string which can be parsed back into the original node. |
| + (Peter Barna, Adam Schwartz) |
| + |
| Other |
| |
| * LUCENE-8768: Fix Javadocs build in Java 11. (Namgyu Kim) |
| 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.java |
| index e9813c4cc13..9235e93e7a0 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 |
| @@ -193,4 +193,9 @@ public class FieldQueryNode extends QueryNodeImpl implements FieldValuePairQuery |
| setText(value); |
| } |
| |
| + @Override |
| + public CharSequence getValueAsText(){ |
| + return getText(); |
| + } |
| + |
| } |
| 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.java |
| index 38100300242..2aec7b3bc8d 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 |
| @@ -25,5 +25,7 @@ public interface ValueQueryNode<T extends Object> extends QueryNode { |
| public void setValue(T value); |
| |
| public T getValue(); |
| - |
| + |
| + public CharSequence getValueAsText(); |
| + |
| } |
| 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.java |
| index c2f0a804534..164d1644442 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 |
| @@ -17,6 +17,7 @@ |
| package org.apache.lucene.queryparser.flexible.standard.nodes; |
| |
| import java.util.ArrayList; |
| +import java.util.Locale; |
| |
| import org.apache.lucene.queryparser.flexible.core.nodes.FieldValuePairQueryNode; |
| import org.apache.lucene.queryparser.flexible.core.nodes.FieldableNode; |
| @@ -171,13 +172,20 @@ public class AbstractRangeQueryNode<T extends FieldValuePairQueryNode<?>> |
| } |
| |
| } |
| - |
| + |
| + private CharSequence escape(CharSequence text, EscapeQuerySyntax escapeSyntaxParser){ |
| + return escapeSyntaxParser.escape(text, Locale.getDefault(), EscapeQuerySyntax.Type.NORMAL); |
| + } |
| + |
| @Override |
| public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) { |
| StringBuilder sb = new StringBuilder(); |
| |
| T lower = getLowerBound(); |
| T upper = getUpperBound(); |
| + |
| + sb.append(getField()) |
| + .append(":"); |
| |
| if (lowerInclusive) { |
| sb.append('['); |
| @@ -187,7 +195,7 @@ public class AbstractRangeQueryNode<T extends FieldValuePairQueryNode<?>> |
| } |
| |
| if (lower != null) { |
| - sb.append(lower.toQueryString(escapeSyntaxParser)); |
| + sb.append(escape(lower.getValueAsText(), escapeSyntaxParser)); |
| |
| } else { |
| sb.append("..."); |
| @@ -196,7 +204,7 @@ public class AbstractRangeQueryNode<T extends FieldValuePairQueryNode<?>> |
| sb.append(' '); |
| |
| if (upper != null) { |
| - sb.append(upper.toQueryString(escapeSyntaxParser)); |
| + sb.append(escape(upper.getValueAsText(), 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.java |
| index 69a7d095ebb..d4895178c3c 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 |
| @@ -141,6 +141,17 @@ public class PointQueryNode extends QueryNodeImpl implements |
| public void setValue(Number value) { |
| this.value = value; |
| } |
| + |
| + /** |
| + * Returns a textual representation of the value, formatted as specified, |
| + * as a {@link CharSequence} |
| + * |
| + * @return the formatted value |
| + */ |
| + @Override |
| + public CharSequence getValueAsText(){ |
| + return numberFormat.format(this.value); |
| + } |
| |
| @Override |
| public String toString() { |
| 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.java |
| new file mode 100755 |
| index 00000000000..85fb4df8165 |
| --- /dev/null |
| +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/nodes/TestAbstractRangeQueryNode.java |
| @@ -0,0 +1,68 @@ |
| +/* |
| + * 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.CoreMatchers.*; |
| + |
| +import org.apache.lucene.queryparser.flexible.core.QueryNodeException; |
| +import org.apache.lucene.queryparser.flexible.core.nodes.FieldQueryNode; |
| +import org.apache.lucene.queryparser.flexible.standard.config.PointsConfig; |
| +import org.apache.lucene.queryparser.flexible.standard.parser.EscapeQuerySyntaxImpl; |
| +import org.apache.lucene.util.LuceneTestCase; |
| +import org.junit.Test; |
| + |
| +import java.text.NumberFormat; |
| +import java.util.Locale; |
| + |
| +public class TestAbstractRangeQueryNode extends LuceneTestCase { |
| + private EscapeQuerySyntaxImpl eqs = new EscapeQuerySyntaxImpl(); |
| + |
| + /* LUCENE-6807 toQueryString should be accurate representation of node */ |
| + @Test |
| + public void testTermRangeQueryNode() { |
| + FieldQueryNode lower = new FieldQueryNode("FIELD", "aaa", 0, 0); |
| + FieldQueryNode upper = new FieldQueryNode("FIELD", "zzz", 0, 0); |
| + TermRangeQueryNode trqNode = new TermRangeQueryNode(lower, upper, true, true); |
| + CharSequence queryString = trqNode.toQueryString(eqs); |
| + assertThat(queryString, is(not("[FIELD:aaa FIELD:zzz]"))); //the previous behavior |
| + assertThat(queryString, is("FIELD:[aaa zzz]")); //the desired behavior |
| + } |
| + |
| + /* LUCENE-6807 toQueryString should be accurate representation of node */ |
| + @Test |
| + public void testPointRangeQueryNode() { |
| + NumberFormat format = NumberFormat.getIntegerInstance(Locale.ROOT); |
| + PointQueryNode lower = new PointQueryNode("FIELD", 1, format); |
| + PointQueryNode upper = new PointQueryNode("FIELD", 999, format); |
| + PointRangeQueryNode prqNode; |
| + try { |
| + prqNode = new PointRangeQueryNode( |
| + lower, |
| + upper, |
| + true, |
| + true, |
| + new PointsConfig(format, Integer.class) |
| + ); |
| + CharSequence queryString = prqNode.toQueryString(eqs); |
| + assertThat(queryString, is(not("[FIELD:1 FIELD:999]"))); //the previous behavior |
| + assertThat(queryString, is("FIELD:[1 999]")); //the desired behavior |
| + } |
| + catch (QueryNodeException e) { |
| + fail("testAbstractRangeQueryNode: testNumericRangeQueryNode: \n" + e.getMessage()); |
| + } |
| + } |
| +} |