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 java.util.List;
22 import org.apache.lucene.messages.MessageImpl;
23 import org.apache.lucene.queryParser.core.QueryNodeError;
24 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
25 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
28 * A {@link ProximityQueryNode} represents a query where the terms should meet
29 * specific distance conditions. (a b c) WITHIN [SENTENCE|PARAGRAPH|NUMBER]
30 * [INORDER] ("a" "b" "c") WITHIN [SENTENCE|PARAGRAPH|NUMBER] [INORDER]
32 * TODO: Add this to the future standard Lucene parser/processor/builder
34 public class ProximityQueryNode extends BooleanQueryNode {
36 private static final long serialVersionUID = 9018220596680832916L;
41 CharSequence toQueryString() { return "WITHIN PARAGRAPH"; }
45 CharSequence toQueryString() { return "WITHIN SENTENCE"; }
49 CharSequence toQueryString() { return "WITHIN"; }
52 abstract CharSequence toQueryString();
56 static public class ProximityType {
61 public ProximityType(Type type) {
65 public ProximityType(Type type, int distance) {
67 this.pDistance = distance;
71 private Type proximityType = Type.SENTENCE;
72 private int distance = -1;
73 private boolean inorder = false;
74 private CharSequence field = null;
78 * - QueryNode children
82 * - type of proximity query
84 * - positive integer that specifies the distance
86 * - true, if the tokens should be matched in the order of the
89 public ProximityQueryNode(List<QueryNode> clauses, CharSequence field,
90 Type type, int distance, boolean inorder) {
93 this.proximityType = type;
94 this.inorder = inorder;
96 if (type == Type.NUMBER) {
98 throw new QueryNodeError(new MessageImpl(
99 QueryParserMessages.PARAMETER_VALUE_NOT_SUPPORTED, "distance",
103 this.distance = distance;
107 clearFields(clauses, field);
112 * - QueryNode children
116 * - type of proximity query
118 * - true, if the tokens should be matched in the order of the
121 public ProximityQueryNode(List<QueryNode> clauses, CharSequence field,
122 Type type, boolean inorder) {
123 this(clauses, field, type, -1, inorder);
126 static private void clearFields(List<QueryNode> nodes, CharSequence field) {
127 if (nodes == null || nodes.size() == 0)
130 for (QueryNode clause : nodes) {
132 if (clause instanceof FieldQueryNode) {
133 ((FieldQueryNode) clause).toQueryStringIgnoreFields = true;
134 ((FieldQueryNode) clause).setField(field);
139 public Type getProximityType() {
140 return this.proximityType;
144 public String toString() {
145 String distanceSTR = ((this.distance == -1) ? ("")
146 : (" distance='" + this.distance) + "'");
148 if (getChildren() == null || getChildren().size() == 0)
149 return "<proximity field='" + this.field + "' inorder='" + this.inorder
150 + "' type='" + this.proximityType.toString() + "'" + distanceSTR
152 StringBuilder sb = new StringBuilder();
153 sb.append("<proximity field='" + this.field + "' inorder='" + this.inorder
154 + "' type='" + this.proximityType.toString() + "'" + distanceSTR + ">");
155 for (QueryNode child : getChildren()) {
157 sb.append(child.toString());
159 sb.append("\n</proximity>");
160 return sb.toString();
164 public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
165 String withinSTR = this.proximityType.toQueryString()
166 + ((this.distance == -1) ? ("") : (" " + this.distance))
167 + ((this.inorder) ? (" INORDER") : (""));
169 StringBuilder sb = new StringBuilder();
170 if (getChildren() == null || getChildren().size() == 0) {
174 for (QueryNode child : getChildren()) {
175 sb.append(filler).append(child.toQueryString(escapeSyntaxParser));
180 if (isDefaultField(this.field)) {
181 return "( " + sb.toString() + " ) " + withinSTR;
183 return this.field + ":(( " + sb.toString() + " ) " + withinSTR + ")";
188 public QueryNode cloneTree() throws CloneNotSupportedException {
189 ProximityQueryNode clone = (ProximityQueryNode) super.cloneTree();
191 clone.proximityType = this.proximityType;
192 clone.distance = this.distance;
193 clone.field = this.field;
199 * @return the distance
201 public int getDistance() {
202 return this.distance;
206 * returns null if the field was not specified in the query string
210 public CharSequence getField() {
215 * returns null if the field was not specified in the query string
219 public String getFieldAsString() {
220 if (this.field == null)
223 return this.field.toString();
230 public void setField(CharSequence field) {
235 * @return terms must be matched in the specified order
237 public boolean isInOrder() {