Apache Solr open-source search software
LUCENE-1396: improve PhraseQuery.toString: gaps are shown with a ? and multiple terms at the same position are joined with |
Michael McCandless6927ee46a435
diff --git a/CHANGES.txt b/CHANGES.txtindex 6b6889e4286..c52da8e822b 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -57,6 +57,11 @@ Changes in runtime behavioradding the same Directory more than once was causing duplicateswhich led to problems (Mike McCandless)+ 4. LUCENE-1396: Improve PhraseQuery.toString() so that gaps in the+ positions are indicated with a ? and multiple terms at the same+ position are joined with a |. (Andrzej Bialecki via Mike+ McCandless)+API Changes1. LUCENE-1084: Changed all IndexWriter constructors to take andiff --git a/src/java/org/apache/lucene/search/PhraseQuery.java b/src/java/org/apache/lucene/search/PhraseQuery.javaindex 10d12ee9545..806243a4a95 100644--- a/src/java/org/apache/lucene/search/PhraseQuery.java+++ b/src/java/org/apache/lucene/search/PhraseQuery.java@@ -35,6 +35,7 @@ public class PhraseQuery extends Query {private String field;private ArrayList terms = new ArrayList(4);private ArrayList positions = new ArrayList(4);+ private int maxPosition = 0;private int slop = 0;/** Constructs an empty phrase query. */@@ -87,6 +88,7 @@ public class PhraseQuery extends Query {terms.add(term);positions.add(new Integer(position));+ if (position > maxPosition) maxPosition = position;}/** Returns the set of terms in this phrase. */@@ -261,10 +263,27 @@ public class PhraseQuery extends Query {}buffer.append("\"");+ String[] pieces = new String[maxPosition + 1];for (int i = 0; i < terms.size(); i++) {- buffer.append(((Term)terms.get(i)).text());- if (i != terms.size()-1)- buffer.append(" ");+ int pos = ((Integer)positions.get(i)).intValue();+ String s = pieces[pos];+ if (s == null) {+ s = ((Term)terms.get(i)).text();+ } else {+ s = s + "|" + ((Term)terms.get(i)).text();+ }+ pieces[pos] = s;+ }+ for (int i = 0; i < pieces.length; i++) {+ if (i > 0) {+ buffer.append(' ');+ }+ String s = pieces[i];+ if (s == null) {+ buffer.append('?');+ } else {+ buffer.append(s);+ }}buffer.append("\"");diff --git a/src/test/org/apache/lucene/search/TestPhraseQuery.java b/src/test/org/apache/lucene/search/TestPhraseQuery.javaindex 85061cd25fb..2b269fc0f3b 100644--- a/src/test/org/apache/lucene/search/TestPhraseQuery.java+++ b/src/test/org/apache/lucene/search/TestPhraseQuery.java@@ -22,6 +22,7 @@ import org.apache.lucene.analysis.*;import org.apache.lucene.document.*;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;+import org.apache.lucene.queryParser.QueryParser;import org.apache.lucene.store.Directory;import org.apache.lucene.store.RAMDirectory;@@ -354,6 +355,17 @@ public class TestPhraseQuery extends LuceneTestCase {assertEquals(2, hits[2].doc);QueryUtils.check(query,searcher);}++ public void testToString() throws Exception {+ StopAnalyzer analyzer = new StopAnalyzer();+ StopFilter.setEnablePositionIncrementsDefault(true);+ QueryParser qp = new QueryParser("field", analyzer);+ qp.setEnablePositionIncrements(true);+ PhraseQuery q = (PhraseQuery)qp.parse("\"this hi this is a test is\"");+ assertEquals("field:\"? hi ? ? ? test\"", q.toString());+ q.add(new Term("field", "hello"), 1);+ assertEquals("field:\"? hi|hello ? ? ? test\"", q.toString());+ }public void testWrappedPhrase() throws IOException {query.add(new Term("repeated", "first"));