From 7123444b75ffbff76afd3caab531ba0974d82121 Mon Sep 17 00:00:00 2001 From: Otis Gospodnetic Date: Mon, 23 Aug 2004 23:07:41 +0000 Subject: [PATCH] - Added 2 new parse methods that take arrays of query strings. Submited by Andraz Skoric (andraz.skoric@medius.si) back in April 2004 git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150447 13f79535-47bb-0310-9956-ffa450edef68 --- .../queryParser/MultiFieldQueryParser.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) 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; + } }