LUCENE-9401: include field in the complex pharse query's toString

This commit is contained in:
Munendra S N 2020-09-25 13:17:16 +05:30
parent 63f0b6b706
commit b9c7f50b6e
3 changed files with 15 additions and 6 deletions

View File

@ -278,6 +278,8 @@ Bug Fixes
* LUCENE-9501: Fix a bug in IndexSortSortedNumericDocValuesRangeQuery where it could violate the * LUCENE-9501: Fix a bug in IndexSortSortedNumericDocValuesRangeQuery where it could violate the
DocIdSetIterator contract. (Julie Tibshirani) DocIdSetIterator contract. (Julie Tibshirani)
* LUCENE-9401: Include field in ComplexPhraseQuery's toString() (Thomas Hecker via Munendra S N)
Documentation Documentation
--------------------- ---------------------

View File

@ -434,10 +434,15 @@ public class ComplexPhraseQueryParser extends QueryParser {
@Override @Override
public String toString(String field) { public String toString(String field) {
if (slopFactor == 0) StringBuilder sb = new StringBuilder();
return "\"" + phrasedQueryStringContents + "\""; if (!this.field.equals(field)) {
else sb.append(this.field).append(":");
return "\"" + phrasedQueryStringContents + "\"" + "~" + slopFactor; }
sb.append("\"").append(phrasedQueryStringContents).append("\"");
if (slopFactor != 0) {
sb.append("~").append(slopFactor);
}
return sb.toString();
} }
@Override @Override

View File

@ -169,12 +169,14 @@ public class TestComplexPhraseQuery extends LuceneTestCase {
} }
public void testToStringContainsSlop() throws Exception { public void testToStringContainsSlop() throws Exception {
ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(defaultFieldName, analyzer); ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser("", analyzer);
int slop = random().nextInt(31) + 1; int slop = random().nextInt(31) + 1;
String qString = "name:\"j* smyth~\"~" + slop; String qString = "name:\"j* smyth~\"~" + slop;
Query query = qp.parse(qString); 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~\""; String string = "\"j* smyth~\"";
Query q = qp.parse(string); Query q = qp.parse(string);