pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / FuzzyQueryNodeProcessor.java
1 package org.apache.lucene.queryParser.standard.processors;
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 java.util.List;
21
22 import org.apache.lucene.queryParser.core.QueryNodeException;
23 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
24 import org.apache.lucene.queryParser.core.nodes.FuzzyQueryNode;
25 import org.apache.lucene.queryParser.core.nodes.QueryNode;
26 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
27 import org.apache.lucene.queryParser.standard.config.FuzzyConfig;
28 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
29 import org.apache.lucene.search.FuzzyQuery;
30
31 /**
32  * This processor iterates the query node tree looking for every
33  * {@link FuzzyQueryNode}, when this kind of node is found, it checks on the
34  * query configuration for
35  * {@link ConfigurationKeys#FUZZY_CONFIG}, gets the
36  * fuzzy prefix length and default similarity from it and set to the fuzzy node.
37  * For more information about fuzzy prefix length check: {@link FuzzyQuery}. <br/>
38  * 
39  * @see ConfigurationKeys#FUZZY_CONFIG
40  * @see FuzzyQuery
41  * @see FuzzyQueryNode
42  */
43 public class FuzzyQueryNodeProcessor extends QueryNodeProcessorImpl {
44
45   @Override
46   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
47
48     return node;
49
50   }
51
52   @Override
53   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
54
55     if (node instanceof FuzzyQueryNode) {
56       FuzzyQueryNode fuzzyNode = (FuzzyQueryNode) node;
57       QueryConfigHandler config = getQueryConfigHandler();
58
59       FuzzyConfig fuzzyConfig = null;
60       
61       if (config != null && (fuzzyConfig = config.get(ConfigurationKeys.FUZZY_CONFIG)) != null) {
62         fuzzyNode.setPrefixLength(fuzzyConfig.getPrefixLength());
63
64         if (fuzzyNode.getSimilarity() < 0) {
65           fuzzyNode.setSimilarity(fuzzyConfig.getMinSimilarity());
66         }
67         
68       } else if (fuzzyNode.getSimilarity() < 0) {
69         throw new IllegalArgumentException("No FUZZY_CONFIG set in the config");
70       }
71
72     }
73
74     return node;
75
76   }
77
78   @Override
79   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
80       throws QueryNodeException {
81
82     return children;
83
84   }
85
86 }