diff --git a/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java b/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java index 229efc1fd3e..5357fd190d5 100644 --- a/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java +++ b/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java @@ -80,6 +80,37 @@ public class MultiFieldQueryParser extends QueryParser return bQuery; } + /** + *
+ * Parses a query which searches on the fields specified. + *
+ * If x fields are specified, this effectively constructs: + *
+ *
+ * (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
+ *
+ *
+ * @param queries Queries strings to parse
+ * @param fields Fields to search on
+ * @param analyzer Analyzer to use
+ * @throws ParseException if query parsing fails
+ * @throws TokenMgrError if query parsing fails
+ */
+ public static Query parse(String[] queries, String[] fields,
+ Analyzer analyzer) throws ParseException
+ {
+ if (queries.length != fields.length)
+ // TODO Exception handling
+ throw new ParseException("queries.length != fields.length");
+ BooleanQuery bQuery = new BooleanQuery();
+ for (int i = 0; i < fields.length; i++)
+ {
+ Query q = parse(queries[i], fields[i], analyzer);
+ bQuery.add(q, false, false);
+ }
+ return bQuery;
+ }
+
/**
* * Parses a query, searching on the fields specified. @@ -134,4 +165,61 @@ public class MultiFieldQueryParser extends QueryParser } 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. + *
+ * Usage:
+ *
+ * String[] fields = {"filename", "contents", "description"};
+ * int[] flags = {MultiFieldQueryParser.NORMAL FIELD,
+ * MultiFieldQueryParser.REQUIRED FIELD,
+ * MultiFieldQueryParser.PROHIBITED FIELD,};
+ * parse(query, fields, flags, analyzer);
+ *
+ *
+ *+ * The code above would construct a query: + *
+ *
+ * (filename:query1) +(contents:query2) -(description:query3)
+ *
+ *
+ *
+ * @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
+ */
+ public static Query parse(String[] queries, String[] fields, int[] flags,
+ Analyzer analyzer) throws ParseException
+ {
+ if (queries.length != fields.length)
+ // TODO Exception handling
+ throw new ParseException("queries.length != fields.length");
+ BooleanQuery bQuery = new BooleanQuery();
+ for (int i = 0; i < fields.length; i++)
+ {
+ Query q = parse(queries[i], fields[i], analyzer);
+ int flag = flags[i];
+ switch (flag)
+ {
+ case REQUIRED_FIELD:
+ bQuery.add(q, true, false);
+ break;
+ case PROHIBITED_FIELD:
+ bQuery.add(q, false, true);
+ break;
+ default:
+ bQuery.add(q, false, false);
+ break;
+ }
+ }
+ return bQuery;
+ }
}