1 package org.apache.lucene.queryParser.standard;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
27 * This class defines utility methods to (help) parse query strings into
28 * {@link Query} objects.
30 final public class QueryParserUtil {
33 * Parses a query which searches on the fields specified.
35 * If x fields are specified, this effectively constructs:
39 * (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
44 * Queries strings to parse
49 * @throws IllegalArgumentException
50 * if the length of the queries array differs from the length of the
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();
59 StandardQueryParser qp = new StandardQueryParser();
60 qp.setAnalyzer(analyzer);
62 for (int i = 0; i < fields.length; i++) {
63 Query q = qp.parse(queries[i], fields[i]);
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);
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.
81 * String[] fields = {"filename", "contents", "description"};
82 * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
83 * BooleanClause.Occur.MUST,
84 * BooleanClause.Occur.MUST_NOT};
85 * MultiFieldQueryParser.parse("query", fields, flags, analyzer);
89 * The code above would construct a query:
93 * (filename:query) +(contents:query) -(description:query)
98 * Query string to parse
100 * Fields to search on
102 * Flags describing the fields
105 * @throws IllegalArgumentException
106 * if the length of the fields array differs from the length of the
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();
115 StandardQueryParser qp = new StandardQueryParser();
116 qp.setAnalyzer(analyzer);
118 for (int i = 0; i < fields.length; i++) {
119 Query q = qp.parse(query, fields[i]);
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]);
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.
137 * String[] query = {"query1", "query2", "query3"};
138 * String[] fields = {"filename", "contents", "description"};
139 * BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
140 * BooleanClause.Occur.MUST,
141 * BooleanClause.Occur.MUST_NOT};
142 * MultiFieldQueryParser.parse(query, fields, flags, analyzer);
146 * The code above would construct a query:
150 * (filename:query1) +(contents:query2) -(description:query3)
155 * Queries string to parse
157 * Fields to search on
159 * Flags describing the fields
162 * @throws IllegalArgumentException
163 * if the length of the queries, fields, and flags array differ
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();
172 StandardQueryParser qp = new StandardQueryParser();
173 qp.setAnalyzer(analyzer);
175 for (int i = 0; i < fields.length; i++) {
176 Query q = qp.parse(queries[i], fields[i]);
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]);
187 * Returns a String where those characters that TextParser expects to be
188 * escaped are escaped by a preceding <code>\</code>.
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 == '&') {
203 return sb.toString();