You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@lucene.apache.org by mi...@apache.org on 2008/09/21 12:42:47 UTC

svn commit: r697470 - in /lucene/java/branches/lucene_2_4: CHANGES.txt src/java/org/apache/lucene/search/PhraseQuery.java src/test/org/apache/lucene/search/TestPhraseQuery.java

Author: mikemccand
Date: Sun Sep 21 03:42:47 2008
New Revision: 697470

URL: http://svn.apache.org/viewvc?rev=697470&view=rev
Log:
LUCENE-1396: improve PhraseQuery.toString: gaps are shown with a ? and multiple terms at the same position are joined with |

Modified:
    lucene/java/branches/lucene_2_4/CHANGES.txt
    lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/search/PhraseQuery.java
    lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/search/TestPhraseQuery.java

Modified: lucene/java/branches/lucene_2_4/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_4/CHANGES.txt?rev=697470&r1=697469&r2=697470&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_4/CHANGES.txt (original)
+++ lucene/java/branches/lucene_2_4/CHANGES.txt Sun Sep 21 03:42:47 2008
@@ -39,6 +39,11 @@
     adding the same Directory more than once was causing duplicates
     which 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 Changes
 
  1. LUCENE-1084: Changed all IndexWriter constructors to take an

Modified: lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/search/PhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/search/PhraseQuery.java?rev=697470&r1=697469&r2=697470&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/search/PhraseQuery.java (original)
+++ lucene/java/branches/lucene_2_4/src/java/org/apache/lucene/search/PhraseQuery.java Sun Sep 21 03:42:47 2008
@@ -35,6 +35,7 @@
   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 @@
 
       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 @@
     }
 
     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("\"");
 

Modified: lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/search/TestPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/search/TestPhraseQuery.java?rev=697470&r1=697469&r2=697470&view=diff
==============================================================================
--- lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/search/TestPhraseQuery.java (original)
+++ lucene/java/branches/lucene_2_4/src/test/org/apache/lucene/search/TestPhraseQuery.java Sun Sep 21 03:42:47 2008
@@ -22,6 +22,7 @@
 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 @@
     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"));