lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dna...@apache.org
Subject svn commit: r295117 - in /lucene/java/trunk/src: java/org/apache/lucene/queryParser/MultiFieldQueryParser.java test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
Date Wed, 05 Oct 2005 21:01:35 GMT
Author: dnaber
Date: Wed Oct  5 14:01:28 2005
New Revision: 295117

URL: http://svn.apache.org/viewcvs?rev=295117&view=rev
Log:
deprecate methods that use int flags, adding alternative methods that
use enumeration type instead

Modified:
    lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java
    lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java

Modified: lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java?rev=295117&r1=295116&r2=295117&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java Wed
Oct  5 14:01:28 2005
@@ -162,8 +162,11 @@
   }
  
 
+  /** @deprecated */
   public static final int NORMAL_FIELD     = 0;
+  /** @deprecated */
   public static final int REQUIRED_FIELD   = 1;
+  /** @deprecated */
   public static final int PROHIBITED_FIELD = 2;
 
   /**
@@ -283,6 +286,7 @@
    * @throws TokenMgrError if query parsing fails
    * @throws IllegalArgumentException if the length of the fields array differs
    *  from the length of the flags array
+   * @deprecated use {@link #parse(String, String[], BooleanClause.Occur[], Analyzer)} instead
    */
   public static Query parse(String query, String[] fields, int[] flags,
 	Analyzer analyzer) throws ParseException
@@ -319,28 +323,73 @@
    * Usage:
    * <code>
    * String[] fields = {"filename", "contents", "description"};
-   * int[] flags = {MultiFieldQueryParser.NORMAL_FIELD,
-   *                MultiFieldQueryParser.REQUIRED_FIELD,
-   *                MultiFieldQueryParser.PROHIBITED_FIELD,};
-   * parse(query, fields, flags, analyzer);
+   * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
+   *                BooleanClause.Occur.MUST,
+   *                BooleanClause.Occur.MUST_NOT};
+   * MultiFieldQueryParser.parse("query", fields, flags, analyzer);
    * </code>
    * </pre>
    *<p>
    * The code above would construct a query:
    * <pre>
    * <code>
-   * (filename:query1) +(contents:query2) -(description:query3)
+   * (filename:query) +(contents:query) -(description:query)
    * </code>
    * </pre>
    *
+   * @param query Query string to parse
+   * @param fields Fields to search on
+   * @param flags Flags describing the fields
+   * @param analyzer Analyzer to use
+   * @throws ParseException if query parsing fails
+   * @throws TokenMgrError if query parsing fails
+   * @throws IllegalArgumentException if the length of the fields array differs
+   *  from the length of the flags array
+   */
+  public static Query parse(String query, String[] fields,
+      BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException {
+    if (fields.length != flags.length)
+      throw new IllegalArgumentException("fields.length != flags.length");
+    BooleanQuery bQuery = new BooleanQuery();
+    for (int i = 0; i < fields.length; i++) {
+      QueryParser qp = new QueryParser(fields[i], analyzer);
+      Query q = qp.parse(query);
+      bQuery.add(q, flags[i]);
+    }
+    return bQuery;
+  }
+
+  /**
+   * Parses a query, searching on the fields specified. Use this if you need to
+   * specify certain fields as required, and others as prohibited.
+   * <p>
+   * <pre>
+   *  Usage:
+   * <code>
+   * String[] fields = { &quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;
};
+   * int[] flags = { MultiFieldQueryParser.NORMAL_FIELD,
+   *     MultiFieldQueryParser.REQUIRED_FIELD,
+   *     MultiFieldQueryParser.PROHIBITED_FIELD, };
+   * parse(query, fields, flags, analyzer);
+   * </code>
+   * </pre>
+   * 
+   * <p>
+   * The code above would construct a query:
+   * <pre>
+   * <code>
+   *  (filename:query1) +(contents:query2) -(description:query3)
+   * </code>
+   * </pre>
+   * 
    * @param queries Queries string to parse
    * @param fields Fields to search on
    * @param flags Flags describing the fields
    * @param analyzer Analyzer to use
    * @throws ParseException if query parsing fails
    * @throws TokenMgrError if query parsing fails
-   * @throws IllegalArgumentException if the length of the queries, fields,
-   *  and flags array differ
+   * @throws IllegalArgumentException if the length of the queries, fields, and flags array
differ
+   * @deprecated use {@link #parse(String[], String[], BooleanClause.Occur[], Analyzer)}
instead
    */
   public static Query parse(String[] queries, String[] fields, int[] flags,
       Analyzer analyzer) throws ParseException
@@ -368,5 +417,52 @@
     }
     return bQuery;
   }
-  
+
+  /**
+   * Parses a query, searching on the fields specified.
+   * Use this if you need to specify certain fields as required,
+   * and others as prohibited.
+   * <p><pre>
+   * Usage:
+   * <code>
+   * String[] query = {"query1", "query2", "query3"};
+   * String[] fields = {"filename", "contents", "description"};
+   * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
+   *                BooleanClause.Occur.MUST,
+   *                BooleanClause.Occur.MUST_NOT};
+   * MultiFieldQueryParser.parse(query, fields, flags, analyzer);
+   * </code>
+   * </pre>
+   *<p>
+   * The code above would construct a query:
+   * <pre>
+   * <code>
+   * (filename:query1) +(contents:query2) -(description:query3)
+   * </code>
+   * </pre>
+   *
+   * @param queries Queries string to parse
+   * @param fields Fields to search on
+   * @param flags Flags describing the fields
+   * @param analyzer Analyzer to use
+   * @throws ParseException if query parsing fails
+   * @throws TokenMgrError if query parsing fails
+   * @throws IllegalArgumentException if the length of the queries, fields,
+   *  and flags array differ
+   */
+  public static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags,
+      Analyzer analyzer) throws ParseException
+  {
+    if (!(queries.length == fields.length && queries.length == flags.length))
+      throw new IllegalArgumentException("queries, fields, and flags array have have different
length");
+    BooleanQuery bQuery = new BooleanQuery();
+    for (int i = 0; i < fields.length; i++)
+    {
+      QueryParser qp = new QueryParser(fields[i], analyzer);
+      Query q = qp.parse(queries[i]);
+      bQuery.add(q, flags[i]);
+    }
+    return bQuery;
+  }
+
 }

Modified: lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java?rev=295117&r1=295116&r2=295117&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
(original)
+++ lucene/java/trunk/src/test/org/apache/lucene/queryParser/TestMultiFieldQueryParser.java
Wed Oct  5 14:01:28 2005
@@ -27,6 +27,7 @@
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
 import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.Hits;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
@@ -139,9 +140,27 @@
       // expected exception, array length differs
     }
   }
-  
+
   public void testStaticMethod2() throws ParseException {
     String[] fields = {"b", "t"};
+    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST, BooleanClause.Occur.MUST_NOT};
+    Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
+    assertEquals("+b:one -t:one", q.toString());
+
+    q = MultiFieldQueryParser.parse("one two", fields, flags, new StandardAnalyzer());
+    assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());
+
+    try {
+      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
+      q = MultiFieldQueryParser.parse("blah", fields, flags2, new StandardAnalyzer());
+      fail();
+    } catch(IllegalArgumentException e) {
+      // expected exception, array length differs
+    }
+  }
+
+  public void testStaticMethod2Old() throws ParseException {
+    String[] fields = {"b", "t"};
     int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};
     Query q = MultiFieldQueryParser.parse("one", fields, flags, new StandardAnalyzer());
     assertEquals("+b:one -t:one", q.toString());
@@ -159,6 +178,23 @@
   }
 
   public void testStaticMethod3() throws ParseException {
+    String[] queries = {"one", "two", "three"};
+    String[] fields = {"f1", "f2", "f3"};
+    BooleanClause.Occur[] flags = {BooleanClause.Occur.MUST,
+        BooleanClause.Occur.MUST_NOT, BooleanClause.Occur.SHOULD};
+    Query q = MultiFieldQueryParser.parse(queries, fields, flags, new StandardAnalyzer());
+    assertEquals("+f1:one -f2:two f3:three", q.toString());
+
+    try {
+      BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
+      q = MultiFieldQueryParser.parse(queries, fields, flags2, new StandardAnalyzer());
+      fail();
+    } catch(IllegalArgumentException e) {
+      // expected exception, array length differs
+    }
+  }
+
+  public void testStaticMethod3Old() throws ParseException {
     String[] queries = {"one", "two"};
     String[] fields = {"b", "t"};
     int[] flags = {MultiFieldQueryParser.REQUIRED_FIELD, MultiFieldQueryParser.PROHIBITED_FIELD};



Mime
View raw message