You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mu...@apache.org on 2020/09/29 14:12:22 UTC

[lucene-solr] 03/03: LUCENE-9401: include field in the complex pharse query's toString

This is an automated email from the ASF dual-hosted git repository.

munendrasn pushed a commit to branch branch_8x
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git

commit 03a5391e511d060955701bfd75fb713bad0113ec
Author: Munendra S N <mu...@apache.org>
AuthorDate: Fri Sep 25 13:17:16 2020 +0530

    LUCENE-9401: include field in the complex pharse query's toString
---
 lucene/CHANGES.txt                                          |  2 ++
 .../queryparser/complexPhrase/ComplexPhraseQueryParser.java | 13 +++++++++----
 .../queryparser/complexPhrase/TestComplexPhraseQuery.java   |  6 ++++--
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 52d84f7..f55d61a 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -106,6 +106,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 d552aef..3be0a54 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 5935da9..68b3e39 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);