lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dna...@apache.org
Subject cvs commit: jakarta-lucene/src/test/org/apache/lucene/queryParser TestQueryParser.java
Date Fri, 03 Sep 2004 21:12:22 GMT
dnaber      2004/09/03 14:12:22

  Modified:    src/java/org/apache/lucene/queryParser QueryParser.java
                        QueryParser.jj
               src/java/org/apache/lucene/search TermQuery.java
               src/test/org/apache/lucene/queryParser TestQueryParser.java
  Log:
  Fix toString() to print the escaped version of a query. Adapt some test cases, also comment
in some test cases again, but there's still a TODO which someone should have a look at.
  PR: 30985
  
  Revision  Changes    Path
  1.13      +19 -0     jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.java
  
  Index: QueryParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- QueryParser.java	27 Aug 2004 21:50:17 -0000	1.12
  +++ QueryParser.java	3 Sep 2004 21:12:22 -0000	1.13
  @@ -429,6 +429,25 @@
       return new String(caDest, 0, j);
     }
   
  +  /**
  +   * Returns a String where those characters that QueryParser
  +   * expects to be escaped are escaped, i.e. preceded by a <code>\</code>.
  +   */
  +  public static String escape(String s) {
  +    StringBuffer sb = new StringBuffer();
  +    for (int i = 0; i < s.length(); i++) {
  +      char c = s.charAt(i);
  +      // NOTE: keep this in sync with _ESCAPED_CHAR below!
  +      if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c ==
':'
  +        || c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c ==
'~'
  +        || c == '*' || c == '?') {
  +        sb.append('\\');
  +      }
  +      sb.append(c);
  +    }
  +    return sb.toString();
  +  }
  +
     public static void main(String[] args) throws Exception {
       QueryParser qp = new QueryParser("field",
                              new org.apache.lucene.analysis.SimpleAnalyzer());
  
  
  
  1.45      +20 -0     jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj
  
  Index: QueryParser.jj
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/queryParser/QueryParser.jj,v
  retrieving revision 1.44
  retrieving revision 1.45
  diff -u -r1.44 -r1.45
  --- QueryParser.jj	27 Aug 2004 21:50:17 -0000	1.44
  +++ QueryParser.jj	3 Sep 2004 21:12:22 -0000	1.45
  @@ -452,6 +452,25 @@
       return new String(caDest, 0, j);
     }
   
  +  /**
  +   * Returns a String where those characters that QueryParser
  +   * expects to be escaped are escaped, i.e. preceded by a <code>\</code>.
  +   */
  +  public static String escape(String s) {
  +    StringBuffer sb = new StringBuffer();
  +    for (int i = 0; i < s.length(); i++) {
  +      char c = s.charAt(i);
  +      // NOTE: keep this in sync with _ESCAPED_CHAR below!
  +      if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c ==
':'
  +      	|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c ==
'~'
  +      	|| c == '*' || c == '?') {
  +        sb.append('\\');
  +      }
  +      sb.append(c);
  +    }
  +    return sb.toString();
  +  }
  +
     public static void main(String[] args) throws Exception {
       QueryParser qp = new QueryParser("field",
                              new org.apache.lucene.analysis.SimpleAnalyzer());
  @@ -468,6 +487,7 @@
   
   <*> TOKEN : {
     <#_NUM_CHAR:   ["0"-"9"] >
  +// NOTE: keep this in sync with escape(String) above!
   | <#_ESCAPED_CHAR: "\\" [ "\\", "+", "-", "!", "(", ")", ":", "^",
                             "[", "]", "\"", "{", "}", "~", "*", "?" ] >
   | <#_TERM_START_CHAR: ( ~[ " ", "\t", "\n", "\r", "+", "-", "!", "(", ")", ":", "^",
  
  
  
  1.12      +3 -2      jakarta-lucene/src/java/org/apache/lucene/search/TermQuery.java
  
  Index: TermQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/java/org/apache/lucene/search/TermQuery.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- TermQuery.java	29 Mar 2004 22:48:04 -0000	1.11
  +++ TermQuery.java	3 Sep 2004 21:12:22 -0000	1.12
  @@ -20,6 +20,7 @@
   import org.apache.lucene.index.Term;
   import org.apache.lucene.index.TermDocs;
   import org.apache.lucene.index.IndexReader;
  +import org.apache.lucene.queryParser.QueryParser;
   
   /** A Query that matches documents containing a term.
     This may be combined with other terms with a {@link BooleanQuery}.
  @@ -142,10 +143,10 @@
     public String toString(String field) {
       StringBuffer buffer = new StringBuffer();
       if (!term.field().equals(field)) {
  -      buffer.append(term.field());
  +      buffer.append(QueryParser.escape(term.field()));
         buffer.append(":");
       }
  -    buffer.append(term.text());
  +    buffer.append(QueryParser.escape(term.text()));
       if (getBoost() != 1.0f) {
         buffer.append("^");
         buffer.append(Float.toString(getBoost()));
  
  
  
  1.27      +17 -13    jakarta-lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java
  
  Index: TestQueryParser.java
  ===================================================================
  RCS file: /home/cvs/jakarta-lucene/src/test/org/apache/lucene/queryParser/TestQueryParser.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- TestQueryParser.java	30 May 2004 20:24:20 -0000	1.26
  +++ TestQueryParser.java	3 Sep 2004 21:12:22 -0000	1.27
  @@ -317,7 +317,7 @@
   
     public void testEscaped() throws Exception {
       Analyzer a = new WhitespaceAnalyzer();
  - /*   assertQueryEquals("\\[brackets", a, "\\[brackets");
  +    assertQueryEquals("\\[brackets", a, "\\[brackets");
       assertQueryEquals("\\[brackets", null, "brackets");
       assertQueryEquals("\\\\", a, "\\\\");
       assertQueryEquals("\\+blah", a, "\\+blah");
  @@ -337,29 +337,33 @@
       assertQueryEquals("\\~blah", a, "\\~blah");
       assertQueryEquals("\\*blah", a, "\\*blah");
       assertQueryEquals("\\?blah", a, "\\?blah");
  -    assertQueryEquals("foo \\&& bar", a, "foo \\&& bar");
  -    assertQueryEquals("foo \\|| bar", a, "foo \\|| bar");
  -    assertQueryEquals("foo \\AND bar", a, "foo \\AND bar"); */
  +    
  +    // TODO: what about these?
  +    //assertQueryEquals("foo \\&\\& bar", a, "foo \\&\\& bar");
  +    //assertQueryEquals("foo \\|| bar", a, "foo \\|| bar");
  +    //assertQueryEquals("foo \\AND bar", a, "foo \\AND bar");
   
  -	assertQueryEquals("a\\-b:c",a,"a-b:c");
  -	assertQueryEquals("a\\+b:c",a,"a+b:c");
  -	assertQueryEquals("a\\:b:c",a,"a:b:c");
  -	assertQueryEquals("a\\\\b:c",a,"a\\b:c");
  +	assertQueryEquals("a\\-b:c",a,"a\\-b:c");
  +	assertQueryEquals("a\\+b:c",a,"a\\+b:c");
  +	assertQueryEquals("a\\:b:c",a,"a\\:b:c");
  +	assertQueryEquals("a\\\\b:c",a,"a\\\\b:c");
   
  -	assertQueryEquals("a:b\\-c",a,"a:b-c");
  -	assertQueryEquals("a:b\\+c",a,"a:b+c");
  -	assertQueryEquals("a:b\\:c",a,"a:b:c");
  -	assertQueryEquals("a:b\\\\c",a,"a:b\\c");
  +	assertQueryEquals("a:b\\-c",a,"a:b\\-c");
  +	assertQueryEquals("a:b\\+c",a,"a:b\\+c");
  +	assertQueryEquals("a:b\\:c",a,"a:b\\:c");
  +	assertQueryEquals("a:b\\\\c",a,"a:b\\\\c");
   
   	assertQueryEquals("a:b\\-c*",a,"a:b-c*");
   	assertQueryEquals("a:b\\+c*",a,"a:b+c*");
   	assertQueryEquals("a:b\\:c*",a,"a:b:c*");
  +  
   	assertQueryEquals("a:b\\\\c*",a,"a:b\\c*");
   
   	assertQueryEquals("a:b\\-?c",a,"a:b-?c");
   	assertQueryEquals("a:b\\+?c",a,"a:b+?c");
   	assertQueryEquals("a:b\\:?c",a,"a:b:?c");
  -	assertQueryEquals("a:b\\\\?c",a,"a:b\\?c");
  +  
  +	assertQueryEquals("a:b\\\\?c",a,"a:b\\\\\\?c");
   
   	assertQueryEquals("a:b\\-c~",a,"a:b-c~");
   	assertQueryEquals("a:b\\+c~",a,"a:b+c~");
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-dev-help@jakarta.apache.org


Mime
View raw message