add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / QueryParserUtil.java
1 package org.apache.lucene.queryParser.standard;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.Analyzer;
21 import org.apache.lucene.queryParser.core.QueryNodeException;
22 import org.apache.lucene.search.BooleanClause;
23 import org.apache.lucene.search.BooleanQuery;
24 import org.apache.lucene.search.Query;
25
26 /**
27  * This class defines utility methods to (help) parse query strings into
28  * {@link Query} objects.
29  */
30 final public class QueryParserUtil {
31
32   /**
33    * Parses a query which searches on the fields specified.
34    * <p>
35    * If x fields are specified, this effectively constructs:
36    * 
37    * <pre>
38    * &lt;code&gt;
39    * (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
40    * &lt;/code&gt;
41    * </pre>
42    * 
43    * @param queries
44    *          Queries strings to parse
45    * @param fields
46    *          Fields to search on
47    * @param analyzer
48    *          Analyzer to use
49    * @throws IllegalArgumentException
50    *           if the length of the queries array differs from the length of the
51    *           fields array
52    */
53   public static Query parse(String[] queries, String[] fields, Analyzer analyzer)
54       throws QueryNodeException {
55     if (queries.length != fields.length)
56       throw new IllegalArgumentException("queries.length != fields.length");
57     BooleanQuery bQuery = new BooleanQuery();
58
59     StandardQueryParser qp = new StandardQueryParser();
60     qp.setAnalyzer(analyzer);
61
62     for (int i = 0; i < fields.length; i++) {
63       Query q = qp.parse(queries[i], fields[i]);
64
65       if (q != null && // q never null, just being defensive
66           (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
67         bQuery.add(q, BooleanClause.Occur.SHOULD);
68       }
69     }
70     return bQuery;
71   }
72
73   /**
74    * Parses a query, searching on the fields specified. Use this if you need to
75    * specify certain fields as required, and others as prohibited.
76    * <p>
77    * 
78    * <pre>
79    * Usage:
80    * &lt;code&gt;
81    * String[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
82    * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
83    *                BooleanClause.Occur.MUST,
84    *                BooleanClause.Occur.MUST_NOT};
85    * MultiFieldQueryParser.parse(&quot;query&quot;, fields, flags, analyzer);
86    * &lt;/code&gt;
87    * </pre>
88    *<p>
89    * The code above would construct a query:
90    * 
91    * <pre>
92    * &lt;code&gt;
93    * (filename:query) +(contents:query) -(description:query)
94    * &lt;/code&gt;
95    * </pre>
96    * 
97    * @param query
98    *          Query string to parse
99    * @param fields
100    *          Fields to search on
101    * @param flags
102    *          Flags describing the fields
103    * @param analyzer
104    *          Analyzer to use
105    * @throws IllegalArgumentException
106    *           if the length of the fields array differs from the length of the
107    *           flags array
108    */
109   public static Query parse(String query, String[] fields,
110       BooleanClause.Occur[] flags, Analyzer analyzer) throws QueryNodeException {
111     if (fields.length != flags.length)
112       throw new IllegalArgumentException("fields.length != flags.length");
113     BooleanQuery bQuery = new BooleanQuery();
114
115     StandardQueryParser qp = new StandardQueryParser();
116     qp.setAnalyzer(analyzer);
117
118     for (int i = 0; i < fields.length; i++) {
119       Query q = qp.parse(query, fields[i]);
120
121       if (q != null && // q never null, just being defensive
122           (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
123         bQuery.add(q, flags[i]);
124       }
125     }
126     return bQuery;
127   }
128
129   /**
130    * Parses a query, searching on the fields specified. Use this if you need to
131    * specify certain fields as required, and others as prohibited.
132    * <p>
133    * 
134    * <pre>
135    * Usage:
136    * &lt;code&gt;
137    * String[] query = {&quot;query1&quot;, &quot;query2&quot;, &quot;query3&quot;};
138    * String[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
139    * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
140    *                BooleanClause.Occur.MUST,
141    *                BooleanClause.Occur.MUST_NOT};
142    * MultiFieldQueryParser.parse(query, fields, flags, analyzer);
143    * &lt;/code&gt;
144    * </pre>
145    *<p>
146    * The code above would construct a query:
147    * 
148    * <pre>
149    * &lt;code&gt;
150    * (filename:query1) +(contents:query2) -(description:query3)
151    * &lt;/code&gt;
152    * </pre>
153    * 
154    * @param queries
155    *          Queries string to parse
156    * @param fields
157    *          Fields to search on
158    * @param flags
159    *          Flags describing the fields
160    * @param analyzer
161    *          Analyzer to use
162    * @throws IllegalArgumentException
163    *           if the length of the queries, fields, and flags array differ
164    */
165   public static Query parse(String[] queries, String[] fields,
166       BooleanClause.Occur[] flags, Analyzer analyzer) throws QueryNodeException {
167     if (!(queries.length == fields.length && queries.length == flags.length))
168       throw new IllegalArgumentException(
169           "queries, fields, and flags array have have different length");
170     BooleanQuery bQuery = new BooleanQuery();
171
172     StandardQueryParser qp = new StandardQueryParser();
173     qp.setAnalyzer(analyzer);
174
175     for (int i = 0; i < fields.length; i++) {
176       Query q = qp.parse(queries[i], fields[i]);
177
178       if (q != null && // q never null, just being defensive
179           (!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
180         bQuery.add(q, flags[i]);
181       }
182     }
183     return bQuery;
184   }
185
186   /**
187    * Returns a String where those characters that TextParser expects to be
188    * escaped are escaped by a preceding <code>\</code>.
189    */
190   public static String escape(String s) {
191     StringBuilder sb = new StringBuilder();
192     for (int i = 0; i < s.length(); i++) {
193       char c = s.charAt(i);
194       // These characters are part of the query syntax and must be escaped
195       if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')'
196           || c == ':' || c == '^' || c == '[' || c == ']' || c == '\"'
197           || c == '{' || c == '}' || c == '~' || c == '*' || c == '?'
198           || c == '|' || c == '&') {
199         sb.append('\\');
200       }
201       sb.append(c);
202     }
203     return sb.toString();
204   }
205
206 }