lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r697469 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/PhraseQuery.java src/test/org/apache/lucene/search/TestPhraseQuery.java
Date Sun, 21 Sep 2008 10:41:42 GMT
Author: mikemccand
Date: Sun Sep 21 03:41:41 2008
New Revision: 697469

URL: http://svn.apache.org/viewvc?rev=697469&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/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestPhraseQuery.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/java/trunk/CHANGES.txt?rev=697469&r1=697468&r2=697469&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Sun Sep 21 03:41:41 2008
@@ -57,6 +57,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/trunk/src/java/org/apache/lucene/search/PhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java?rev=697469&r1=697468&r2=697469&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/PhraseQuery.java Sun Sep 21 03:41:41
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/trunk/src/test/org/apache/lucene/search/TestPhraseQuery.java
URL: http://svn.apache.org/viewvc/lucene/java/trunk/src/test/org/apache/lucene/search/TestPhraseQuery.java?rev=697469&r1=697468&r2=697469&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestPhraseQuery.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestPhraseQuery.java Sun Sep 21 03:41:41
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"));



Mime
View raw message