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.
22
import org.apache.lucene.analysis.Analyzer;
23
import org.apache.lucene.queryParser.ParseException;
24
import org.apache.lucene.search.BooleanClause;
25
import org.apache.lucene.search.BooleanQuery;
26
import org.apache.lucene.search.Query;
29
* This class behaves as the as the lucene 2.4 MultiFieldQueryParser class, but uses the new
30
* query parser interface instead of the old one. <br/>
32
* This class should be used when the new query parser features are needed and
33
* also keep at the same time the old query parser interface. <br/>
35
* @deprecated this class will be removed soon, it's a temporary class to be
36
* used along the transition from the old query parser to the new
40
public class MultiFieldQueryParserWrapper extends QueryParserWrapper {
43
* Creates a MultiFieldQueryParser. Allows passing of a map with term to
44
* Boost, and the boost to apply to each term.
47
* It will, when parse(String query) is called, construct a query like this
48
* (assuming the query consists of two terms and you specify the two fields
49
* <code>title</code> and <code>body</code>):
53
* (title:term1 body:term1) (title:term2 body:term2)
57
* When setDefaultOperator(AND_OPERATOR) is set, the result will be:
61
* +(title:term1 body:term1) +(title:term2 body:term2)
65
* When you pass a boost (title=>5 body=>10) you can get
69
* +(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)
73
* In other words, all the query's terms must appear, but it doesn't matter in
74
* what fields they appear.
77
public MultiFieldQueryParserWrapper(String[] fields, Analyzer analyzer, Map<String, Float> boosts) {
78
this(fields, analyzer);
79
StandardQueryParser qpHelper = getQueryParserHelper();
81
qpHelper.setMultiFields(fields);
82
qpHelper.setFieldsBoost(boosts);
87
* Creates a MultiFieldQueryParser.
90
* It will, when parse(String query) is called, construct a query like this
91
* (assuming the query consists of two terms and you specify the two fields
92
* <code>title</code> and <code>body</code>):
96
* (title:term1 body:term1) (title:term2 body:term2)
100
* When setDefaultOperator(AND_OPERATOR) is set, the result will be:
104
* +(title:term1 body:term1) +(title:term2 body:term2)
108
* In other words, all the query's terms must appear, but it doesn't matter in
109
* what fields they appear.
112
public MultiFieldQueryParserWrapper(String[] fields, Analyzer analyzer) {
113
super(null, analyzer);
115
StandardQueryParser qpHelper = getQueryParserHelper();
116
qpHelper.setAnalyzer(analyzer);
118
qpHelper.setMultiFields(fields);
122
* Parses a query which searches on the fields specified.
124
* If x fields are specified, this effectively constructs:
128
* (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
133
* Queries strings to parse
135
* Fields to search on
138
* @throws ParseException
139
* if query parsing fails
140
* @throws IllegalArgumentException
141
* if the length of the queries array differs from the length of the
144
public static Query parse(String[] queries, String[] fields, Analyzer analyzer)
145
throws ParseException {
146
if (queries.length != fields.length)
147
throw new IllegalArgumentException("queries.length != fields.length");
148
BooleanQuery bQuery = new BooleanQuery();
149
for (int i = 0; i < fields.length; i++) {
150
QueryParserWrapper qp = new QueryParserWrapper(fields[i], analyzer);
151
Query q = qp.parse(queries[i]);
152
if (q != null && // q never null, just being defensive
153
(!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
154
bQuery.add(q, BooleanClause.Occur.SHOULD);
161
* Parses a query, searching on the fields specified. Use this if you need to
162
* specify certain fields as required, and others as prohibited.
168
* String[] fields = {"filename", "contents", "description"};
169
* BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
170
* BooleanClause.Occur.MUST,
171
* BooleanClause.Occur.MUST_NOT};
172
* MultiFieldQueryParser.parse("query", fields, flags, analyzer);
176
* The code above would construct a query:
180
* (filename:query) +(contents:query) -(description:query)
185
* Query string to parse
187
* Fields to search on
189
* Flags describing the fields
192
* @throws ParseException
193
* if query parsing fails
194
* @throws IllegalArgumentException
195
* if the length of the fields array differs from the length of the
198
public static Query parse(String query, String[] fields,
199
BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException {
200
if (fields.length != flags.length)
201
throw new IllegalArgumentException("fields.length != flags.length");
202
BooleanQuery bQuery = new BooleanQuery();
203
for (int i = 0; i < fields.length; i++) {
204
QueryParserWrapper qp = new QueryParserWrapper(fields[i], analyzer);
205
Query q = qp.parse(query);
206
if (q != null && // q never null, just being defensive
207
(!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
208
bQuery.add(q, flags[i]);
215
* Parses a query, searching on the fields specified. Use this if you need to
216
* specify certain fields as required, and others as prohibited.
222
* String[] query = {"query1", "query2", "query3"};
223
* String[] fields = {"filename", "contents", "description"};
224
* BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
225
* BooleanClause.Occur.MUST,
226
* BooleanClause.Occur.MUST_NOT};
227
* MultiFieldQueryParser.parse(query, fields, flags, analyzer);
231
* The code above would construct a query:
235
* (filename:query1) +(contents:query2) -(description:query3)
240
* Queries string to parse
242
* Fields to search on
244
* Flags describing the fields
247
* @throws ParseException
248
* if query parsing fails
249
* @throws IllegalArgumentException
250
* if the length of the queries, fields, and flags array differ
252
public static Query parse(String[] queries, String[] fields,
253
BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException {
254
if (!(queries.length == fields.length && queries.length == flags.length))
255
throw new IllegalArgumentException(
256
"queries, fields, and flags array have have different length");
257
BooleanQuery bQuery = new BooleanQuery();
258
for (int i = 0; i < fields.length; i++) {
259
QueryParserWrapper qp = new QueryParserWrapper(fields[i], analyzer);
260
Query q = qp.parse(queries[i]);
261
if (q != null && // q never null, just being defensive
262
(!(q instanceof BooleanQuery) || ((BooleanQuery) q).getClauses().length > 0)) {
263
bQuery.add(q, flags[i]);