Apache Lucene open-source search software
LUCENE-1819: MatchAllDocsQuery.toString(field) should produce output that is parsable by the QueryParser.
Mark Robert Miller7700fa362646
diff --git a/CHANGES.txt b/CHANGES.txtindex 4a704d4c2ce..9ae1b4d0808 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -490,6 +490,9 @@ Bug fixes23. LUCENE-1801: Changed all Tokenizers or TokenStreams in core/contrib)that are the source of Tokens to always callAttributeSource.clearAttributes() first. (Uwe Schindler)++24. LUCENE-1819: MatchAllDocsQuery.toString(field) should produce output+ that is parsable by the QueryParser. (John Wang, Mark Miller)New featuresdiff --git a/src/java/org/apache/lucene/search/MatchAllDocsQuery.java b/src/java/org/apache/lucene/search/MatchAllDocsQuery.javaindex a2f99c67e25..36bb49a992c 100644--- a/src/java/org/apache/lucene/search/MatchAllDocsQuery.java+++ b/src/java/org/apache/lucene/search/MatchAllDocsQuery.java@@ -151,7 +151,7 @@ public class MatchAllDocsQuery extends Query {public String toString(String field) {StringBuffer buffer = new StringBuffer();- buffer.append("MatchAllDocsQuery");+ buffer.append("*:*");buffer.append(ToStringUtils.boost(getBoost()));return buffer.toString();}diff --git a/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java b/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.javaindex 13380fad97d..09feb03163e 100644--- a/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java+++ b/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java@@ -18,26 +18,30 @@ package org.apache.lucene.search;import java.io.IOException;+import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.index.IndexReader;+import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.store.RAMDirectory;import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util.Version;/*** Tests MatchAllDocsQuery.**/public class TestMatchAllDocsQuery extends LuceneTestCase {-- public void testQuery() throws IOException {+ private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);++ public void testQuery() throws Exception {RAMDirectory dir = new RAMDirectory();- IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);+ IndexWriter iw = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);iw.setMaxBufferedDocs(2); // force multi-segmentaddDoc("one", iw, 1f);addDoc("two", iw, 20f);@@ -95,6 +99,18 @@ public class TestMatchAllDocsQuery extends LuceneTestCase {hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;assertEquals(2, hits.length);+ // test parsable toString()+ QueryParser qp = new QueryParser("key", analyzer);+ hits = is.search(qp.parse(new MatchAllDocsQuery().toString()), null, 1000).scoreDocs;+ assertEquals(2, hits.length);++ // test parsable toString() with non default boost+ Query maq = new MatchAllDocsQuery();+ maq.setBoost(2.3f);+ Query pq = qp.parse(maq.toString());+ hits = is.search(pq, null, 1000).scoreDocs;+ assertEquals(2, hits.length);+is.close();ir.close();dir.close();