01: package org.apache.lucene.queryParser.surround.query;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
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.
18: */
19:
20: import org.apache.lucene.search.BooleanClause;
21: import org.apache.lucene.search.BooleanQuery;
22: import org.apache.lucene.search.Query;
23:
24: public abstract class SrndQuery implements Cloneable {
25: public SrndQuery() {
26: }
27:
28: private float weight = (float) 1.0;
29: private boolean weighted = false;
30:
31: public void setWeight(float w) {
32: weight = w; /* as parsed from the query text */
33: weighted = true;
34: }
35:
36: public boolean isWeighted() {
37: return weighted;
38: }
39:
40: public float getWeight() {
41: return weight;
42: }
43:
44: public String getWeightString() {
45: return Float.toString(getWeight());
46: }
47:
48: public String getWeightOperator() {
49: return "^";
50: }
51:
52: protected void weightToString(StringBuffer r) { /* append the weight part of a query */
53: if (isWeighted()) {
54: r.append(getWeightOperator());
55: r.append(getWeightString());
56: }
57: }
58:
59: public Query makeLuceneQueryField(String fieldName,
60: BasicQueryFactory qf) {
61: Query q = makeLuceneQueryFieldNoBoost(fieldName, qf);
62: if (isWeighted()) {
63: q.setBoost(getWeight() * q.getBoost()); /* weight may be at any level in a SrndQuery */
64: }
65: return q;
66: }
67:
68: public abstract Query makeLuceneQueryFieldNoBoost(String fieldName,
69: BasicQueryFactory qf);
70:
71: public abstract String toString();
72:
73: public boolean isFieldsSubQueryAcceptable() {
74: return true;
75: }
76:
77: public Object clone() {
78: try {
79: return super .clone();
80: } catch (CloneNotSupportedException cns) {
81: throw new Error(cns);
82: }
83: }
84:
85: /* An empty Lucene query */
86: public final static Query theEmptyLcnQuery = new BooleanQuery() { /* no changes allowed */
87: public void setBoost(float boost) {
88: throw new UnsupportedOperationException();
89: }
90:
91: public void add(BooleanClause clause) {
92: throw new UnsupportedOperationException();
93: }
94:
95: public void add(Query query, BooleanClause.Occur occur) {
96: throw new UnsupportedOperationException();
97: }
98: };
99: }
|