1 package org.apache.lucene.queryParser.core.nodes;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
26 * A {@link SlopQueryNode} represents phrase query with a slop.
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".
35 public class SlopQueryNode extends QueryNodeImpl implements FieldableNode {
37 private static final long serialVersionUID = 0L;
39 private int value = 0;
43 * - QueryNode Tree with the phrase
47 public SlopQueryNode(QueryNode query, int value) {
49 throw new QueryNodeError(new MessageImpl(
50 QueryParserMessages.NODE_ACTION_NOT_SUPPORTED, "query", "null"));
59 public QueryNode getChild() {
60 return getChildren().get(0);
63 public int getValue() {
67 private CharSequence getValueString() {
68 Float f = Float.valueOf(this.value);
69 if (f == f.longValue())
70 return "" + f.longValue();
77 public String toString() {
78 return "<slop value='" + getValueString() + "'>" + "\n"
79 + getChild().toString() + "\n</slop>";
82 public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
83 if (getChild() == null)
85 return getChild().toQueryString(escapeSyntaxParser) + "~"
90 public QueryNode cloneTree() throws CloneNotSupportedException {
91 SlopQueryNode clone = (SlopQueryNode) super.cloneTree();
93 clone.value = this.value;
98 public CharSequence getField() {
99 QueryNode child = getChild();
101 if (child instanceof FieldableNode) {
102 return ((FieldableNode) child).getField();
109 public void setField(CharSequence fieldName) {
110 QueryNode child = getChild();
112 if (child instanceof FieldableNode) {
113 ((FieldableNode) child).setField(fieldName);