001: package org.apache.lucene.queryParser.surround.query;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.ArrayList;
021: import java.util.List;
022: import java.util.Iterator;
023:
024: import org.apache.lucene.search.Query;
025:
026: public class FieldsQuery extends SrndQuery { /* mostly untested */
027: private SrndQuery q;
028: private ArrayList fieldNames;
029: private final char fieldOp;
030: private final String OrOperatorName = "OR"; /* for expanded queries, not normally visible */
031:
032: public FieldsQuery(SrndQuery q, ArrayList fieldNames, char fieldOp) {
033: this .q = q;
034: this .fieldNames = fieldNames;
035: this .fieldOp = fieldOp;
036: }
037:
038: public FieldsQuery(SrndQuery q, String fieldName, char fieldOp) {
039: this .q = q;
040: fieldNames = new ArrayList();
041: fieldNames.add(fieldName);
042: this .fieldOp = fieldOp;
043: }
044:
045: public boolean isFieldsSubQueryAcceptable() {
046: return false;
047: }
048:
049: public Query makeLuceneQueryNoBoost(BasicQueryFactory qf) {
050: if (fieldNames.size() == 1) { /* single field name: no new queries needed */
051: return q.makeLuceneQueryFieldNoBoost((String) fieldNames
052: .get(0), qf);
053: } else { /* OR query over the fields */
054: ArrayList queries = new ArrayList();
055: Iterator fni = getFieldNames().listIterator();
056: SrndQuery qc;
057: while (fni.hasNext()) {
058: qc = (SrndQuery) q.clone();
059: queries.add(new FieldsQuery(qc, (String) fni.next(),
060: fieldOp));
061: }
062: boolean infix = true;
063: OrQuery oq = new OrQuery(queries,
064: true /* infix OR for field names */,
065: OrOperatorName);
066: System.out.println(getClass().toString()
067: + ", fields expanded: " + oq.toString()); /* needs testing */
068: return oq.makeLuceneQueryField(null, qf);
069: }
070: }
071:
072: public Query makeLuceneQueryFieldNoBoost(String fieldName,
073: BasicQueryFactory qf) {
074: return makeLuceneQueryNoBoost(qf); /* use this.fieldNames instead of fieldName */
075: }
076:
077: public List getFieldNames() {
078: return fieldNames;
079: }
080:
081: public char getFieldOperator() {
082: return fieldOp;
083: }
084:
085: public String toString() {
086: StringBuffer r = new StringBuffer();
087: r.append("(");
088: fieldNamesToString(r);
089: r.append(q.toString());
090: r.append(")");
091: return r.toString();
092: }
093:
094: protected void fieldNamesToString(StringBuffer r) {
095: Iterator fni = getFieldNames().listIterator();
096: while (fni.hasNext()) {
097: r.append((String) fni.next());
098: r.append(getFieldOperator());
099: }
100: }
101: }
|