diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index e699862f18a..f0e42c84782 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -278,6 +278,8 @@ Bug Fixes * LUCENE-9501: Fix a bug in IndexSortSortedNumericDocValuesRangeQuery where it could violate the DocIdSetIterator contract. (Julie Tibshirani) +* LUCENE-9401: Include field in ComplexPhraseQuery's toString() (Thomas Hecker via Munendra S N) + Documentation --------------------- diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java index d552aef9fe7..3be0a5467fb 100644 --- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java +++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/complexPhrase/ComplexPhraseQueryParser.java @@ -434,10 +434,15 @@ public class ComplexPhraseQueryParser extends QueryParser { @Override public String toString(String field) { - if (slopFactor == 0) - return "\"" + phrasedQueryStringContents + "\""; - else - return "\"" + phrasedQueryStringContents + "\"" + "~" + slopFactor; + StringBuilder sb = new StringBuilder(); + if (!this.field.equals(field)) { + sb.append(this.field).append(":"); + } + sb.append("\"").append(phrasedQueryStringContents).append("\""); + if (slopFactor != 0) { + sb.append("~").append(slopFactor); + } + return sb.toString(); } @Override diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java index 5935da96048..68b3e395c68 100644 --- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java +++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java @@ -169,12 +169,14 @@ public class TestComplexPhraseQuery extends LuceneTestCase { } public void testToStringContainsSlop() throws Exception { - ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(defaultFieldName, analyzer); + ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser("", analyzer); int slop = random().nextInt(31) + 1; String qString = "name:\"j* smyth~\"~" + slop; Query query = qp.parse(qString); - assertTrue("Slop is not shown in toString()", query.toString().endsWith("~" + slop)); + String actualQStr = query.toString(); + assertTrue("Slop is not shown in toString()", actualQStr.endsWith("~" + slop)); + assertEquals(qString, actualQStr); String string = "\"j* smyth~\""; Query q = qp.parse(string);