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
DocIdSetIterator contract. (Julie Tibshirani)
* LUCENE-9401: Include field in ComplexPhraseQuery's toString() (Thomas Hecker via Munendra S N)
Documentation
---------------------

View File

@ -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

View File

@ -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);