pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / SlopQueryNode.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.messages.MessageImpl;
21 import org.apache.lucene.queryParser.core.QueryNodeError;
22 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
23 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
24
25 /**
26  * A {@link SlopQueryNode} represents phrase query with a slop.
27  * 
28  * From Lucene FAQ: Is there a way to use a proximity operator (like near or
29  * within) with Lucene? There is a variable called slop that allows you to
30  * perform NEAR/WITHIN-like queries. By default, slop is set to 0 so that only
31  * exact phrases will match. When using TextParser you can use this syntax to
32  * specify the slop: "doug cutting"~2 will find documents that contain
33  * "doug cutting" as well as ones that contain "cutting doug".
34  */
35 public class SlopQueryNode extends QueryNodeImpl implements FieldableNode {
36
37   private static final long serialVersionUID = 0L;
38
39   private int value = 0;
40
41   /**
42    * @param query
43    *          - QueryNode Tree with the phrase
44    * @param value
45    *          - slop value
46    */
47   public SlopQueryNode(QueryNode query, int value) {
48     if (query == null) {
49       throw new QueryNodeError(new MessageImpl(
50           QueryParserMessages.NODE_ACTION_NOT_SUPPORTED, "query", "null"));
51     }
52
53     this.value = value;
54     setLeaf(false);
55     allocate();
56     add(query);
57   }
58
59   public QueryNode getChild() {
60     return getChildren().get(0);
61   }
62
63   public int getValue() {
64     return this.value;
65   }
66
67   private CharSequence getValueString() {
68     Float f = Float.valueOf(this.value);
69     if (f == f.longValue())
70       return "" + f.longValue();
71     else
72       return "" + f;
73
74   }
75
76   @Override
77   public String toString() {
78     return "<slop value='" + getValueString() + "'>" + "\n"
79         + getChild().toString() + "\n</slop>";
80   }
81
82   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
83     if (getChild() == null)
84       return "";
85     return getChild().toQueryString(escapeSyntaxParser) + "~"
86         + getValueString();
87   }
88
89   @Override
90   public QueryNode cloneTree() throws CloneNotSupportedException {
91     SlopQueryNode clone = (SlopQueryNode) super.cloneTree();
92
93     clone.value = this.value;
94
95     return clone;
96   }
97
98   public CharSequence getField() {
99     QueryNode child = getChild();
100
101     if (child instanceof FieldableNode) {
102       return ((FieldableNode) child).getField();
103     }
104
105     return null;
106
107   }
108
109   public void setField(CharSequence fieldName) {
110     QueryNode child = getChild();
111
112     if (child instanceof FieldableNode) {
113       ((FieldableNode) child).setField(fieldName);
114     }
115
116   }
117
118 }