pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / surround / query / FieldsQuery.java
1 package org.apache.lucene.queryParser.surround.query;
2 /**
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Iterator;
22
23 import org.apache.lucene.search.Query;
24
25 public class FieldsQuery extends SrndQuery { /* mostly untested */
26   private SrndQuery q;
27   private List<String> fieldNames;
28   private final char fieldOp;
29   private final String OrOperatorName = "OR"; /* for expanded queries, not normally visible */
30   
31   public FieldsQuery(SrndQuery q, List<String> fieldNames, char fieldOp) {
32     this.q = q;
33     this.fieldNames = fieldNames;
34     this.fieldOp = fieldOp;
35   }
36   
37   public FieldsQuery(SrndQuery q, String fieldName, char fieldOp) {
38     this.q = q;
39     fieldNames = new ArrayList<String>();
40     fieldNames.add(fieldName);
41     this.fieldOp = fieldOp;
42   }
43   
44   @Override
45   public boolean isFieldsSubQueryAcceptable() {
46     return false;
47   }
48   
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();
55       SrndQuery qc;
56       while (fni.hasNext()) {
57         qc = (SrndQuery) q.clone();
58         queries.add( new FieldsQuery( qc, fni.next(), fieldOp));
59       }
60       OrQuery oq = new OrQuery(queries,
61                               true /* infix OR for field names */,
62                               OrOperatorName);
63       System.out.println(getClass().toString() + ", fields expanded: " + oq.toString()); /* needs testing */
64       return oq.makeLuceneQueryField(null, qf);
65     }
66   }
67
68   @Override
69   public Query makeLuceneQueryFieldNoBoost(String fieldName, BasicQueryFactory qf) {
70     return makeLuceneQueryNoBoost(qf); /* use this.fieldNames instead of fieldName */
71   }
72
73   
74   public List<String> getFieldNames() {return fieldNames;}
75
76   public char getFieldOperator() { return fieldOp;}
77   
78   @Override
79   public String toString() {
80     StringBuilder r = new StringBuilder();
81     r.append("(");
82     fieldNamesToString(r);
83     r.append(q.toString());
84     r.append(")");
85     return r.toString();
86   }
87   
88   protected void fieldNamesToString(StringBuilder r) {
89     Iterator<String> fni = getFieldNames().listIterator();
90     while (fni.hasNext()) {
91       r.append(fni.next());
92       r.append(getFieldOperator());
93     }
94   }
95 }
96