1 package org.apache.lucene.queryParser.surround.query;
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Iterator;
23 import org.apache.lucene.search.Query;
25 public class FieldsQuery extends SrndQuery { /* mostly untested */
27 private List<String> fieldNames;
28 private final char fieldOp;
29 private final String OrOperatorName = "OR"; /* for expanded queries, not normally visible */
31 public FieldsQuery(SrndQuery q, List<String> fieldNames, char fieldOp) {
33 this.fieldNames = fieldNames;
34 this.fieldOp = fieldOp;
37 public FieldsQuery(SrndQuery q, String fieldName, char fieldOp) {
39 fieldNames = new ArrayList<String>();
40 fieldNames.add(fieldName);
41 this.fieldOp = fieldOp;
45 public boolean isFieldsSubQueryAcceptable() {
49 public Query makeLuceneQueryNoBoost(BasicQueryFactory qf) {
50 if (fieldNames.size() == 1) { /* single field name: no new queries needed */
51 return q.makeLuceneQueryFieldNoBoost(fieldNames.get(0), qf);
52 } else { /* OR query over the fields */
53 List<SrndQuery> queries = new ArrayList<SrndQuery>();
54 Iterator<String> fni = getFieldNames().listIterator();
56 while (fni.hasNext()) {
57 qc = (SrndQuery) q.clone();
58 queries.add( new FieldsQuery( qc, fni.next(), fieldOp));
60 OrQuery oq = new OrQuery(queries,
61 true /* infix OR for field names */,
63 System.out.println(getClass().toString() + ", fields expanded: " + oq.toString()); /* needs testing */
64 return oq.makeLuceneQueryField(null, qf);
69 public Query makeLuceneQueryFieldNoBoost(String fieldName, BasicQueryFactory qf) {
70 return makeLuceneQueryNoBoost(qf); /* use this.fieldNames instead of fieldName */
74 public List<String> getFieldNames() {return fieldNames;}
76 public char getFieldOperator() { return fieldOp;}
79 public String toString() {
80 StringBuilder r = new StringBuilder();
82 fieldNamesToString(r);
83 r.append(q.toString());
88 protected void fieldNamesToString(StringBuilder r) {
89 Iterator<String> fni = getFieldNames().listIterator();
90 while (fni.hasNext()) {
92 r.append(getFieldOperator());