add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / BoostQueryNodeProcessor.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.FieldConfig;
24 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
25 import org.apache.lucene.queryParser.core.nodes.BoostQueryNode;
26 import org.apache.lucene.queryParser.core.nodes.FieldableNode;
27 import org.apache.lucene.queryParser.core.nodes.QueryNode;
28 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
29 import org.apache.lucene.queryParser.core.util.StringUtils;
30 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
31
32 /**
33  * This processor iterates the query node tree looking for every
34  * {@link FieldableNode} that has {@link ConfigurationKeys#BOOST} in its
35  * config. If there is, the boost is applied to that {@link FieldableNode}. <br/>
36  * 
37  * @see ConfigurationKeys#BOOST
38  * @see QueryConfigHandler
39  * @see FieldableNode
40  */
41 public class BoostQueryNodeProcessor extends QueryNodeProcessorImpl {
42
43   @Override
44   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
45
46     if (node instanceof FieldableNode && 
47         (node.getParent() == null || !(node.getParent() instanceof FieldableNode))) {
48       
49       FieldableNode fieldNode = (FieldableNode) node;
50       QueryConfigHandler config = getQueryConfigHandler();
51
52       if (config != null) {
53         CharSequence field = fieldNode.getField();
54         FieldConfig fieldConfig = config.getFieldConfig(StringUtils.toString(field));
55
56         if (fieldConfig != null) {
57           Float boost = fieldConfig.get(ConfigurationKeys.BOOST);
58
59           if (boost != null) {
60             return new BoostQueryNode(node, boost);
61           }
62
63         }
64
65       }
66
67     }
68
69     return node;
70
71   }
72
73   @Override
74   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
75
76     return node;
77
78   }
79
80   @Override
81   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
82       throws QueryNodeException {
83
84     return children;
85
86   }
87
88 }