add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / FuzzyQueryNode.java
1 package org.apache.lucene.queryParser.core.nodes;
2
3 /**
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
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.queryParser.core.parser.EscapeQuerySyntax;
21
22 /**
23  * A {@link FuzzyQueryNode} represents a element that contains
24  * field/text/similarity tuple
25  */
26 public class FuzzyQueryNode extends FieldQueryNode {
27
28   private static final long serialVersionUID = -1794537213032589441L;
29
30   private float similarity;
31
32   private int prefixLength;
33
34   /**
35    * @param field
36    *          Name of the field query will use.
37    * @param termStr
38    *          Term token to use for building term for the query
39    */
40   /**
41    * @param field
42    *          - Field name
43    * @param term
44    *          - Value
45    * @param minSimilarity
46    *          - similarity value
47    * @param begin
48    *          - position in the query string
49    * @param end
50    *          - position in the query string
51    */
52   public FuzzyQueryNode(CharSequence field, CharSequence term,
53       float minSimilarity, int begin, int end) {
54     super(field, term, begin, end);
55     this.similarity = minSimilarity;
56     setLeaf(true);
57   }
58
59   public void setPrefixLength(int prefixLength) {
60     this.prefixLength = prefixLength;
61   }
62
63   public int getPrefixLength() {
64     return this.prefixLength;
65   }
66
67   @Override
68   public CharSequence toQueryString(EscapeQuerySyntax escaper) {
69     if (isDefaultField(this.field)) {
70       return getTermEscaped(escaper) + "~" + this.similarity;
71     } else {
72       return this.field + ":" + getTermEscaped(escaper) + "~" + this.similarity;
73     }
74   }
75
76   @Override
77   public String toString() {
78     return "<fuzzy field='" + this.field + "' similarity='" + this.similarity
79         + "' term='" + this.text + "'/>";
80   }
81
82   public void setSimilarity(float similarity) {
83     this.similarity = similarity;
84   }
85
86   @Override
87   public FuzzyQueryNode cloneTree() throws CloneNotSupportedException {
88     FuzzyQueryNode clone = (FuzzyQueryNode) super.cloneTree();
89
90     clone.similarity = this.similarity;
91
92     return clone;
93   }
94
95   /**
96    * @return the similarity
97    */
98   public float getSimilarity() {
99     return this.similarity;
100   }
101 }