pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / standard / processors / MultiFieldQueryNodeProcessor.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.LinkedList;
21 import java.util.List;
22
23 import org.apache.lucene.queryParser.core.QueryNodeException;
24 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
25 import org.apache.lucene.queryParser.core.nodes.BooleanQueryNode;
26 import org.apache.lucene.queryParser.core.nodes.FieldableNode;
27 import org.apache.lucene.queryParser.core.nodes.GroupQueryNode;
28 import org.apache.lucene.queryParser.core.nodes.QueryNode;
29 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessorImpl;
30 import org.apache.lucene.queryParser.standard.config.StandardQueryConfigHandler.ConfigurationKeys;
31
32 /**
33  * This processor is used to expand terms so the query looks for the same term
34  * in different fields. It also boosts a query based on its field. <br/>
35  * <br/>
36  * This processor looks for every {@link FieldableNode} contained in the query
37  * node tree. If a {@link FieldableNode} is found, it checks if there is a
38  * {@link ConfigurationKeys#MULTI_FIELDS} defined in the {@link QueryConfigHandler}. If
39  * there is, the {@link FieldableNode} is cloned N times and the clones are
40  * added to a {@link BooleanQueryNode} together with the original node. N is
41  * defined by the number of fields that it will be expanded to. The
42  * {@link BooleanQueryNode} is returned. <br/>
43  * 
44  * @see ConfigurationKeys#MULTI_FIELDS
45  */
46 public class MultiFieldQueryNodeProcessor extends QueryNodeProcessorImpl {
47
48   private boolean processChildren = true;
49
50   public MultiFieldQueryNodeProcessor() {
51     // empty constructor
52   }
53
54   @Override
55   protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
56
57     return node;
58
59   }
60
61   @Override
62   protected void processChildren(QueryNode queryTree) throws QueryNodeException {
63
64     if (this.processChildren) {
65       super.processChildren(queryTree);
66
67     } else {
68       this.processChildren = true;
69     }
70
71   }
72
73   @Override
74   protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
75
76     if (node instanceof FieldableNode) {
77       this.processChildren = false;
78       FieldableNode fieldNode = (FieldableNode) node;
79
80       if (fieldNode.getField() == null) {
81         CharSequence[] fields = getQueryConfigHandler().get(ConfigurationKeys.MULTI_FIELDS);
82
83         if (fields == null) {
84           throw new IllegalArgumentException(
85               "StandardQueryConfigHandler.ConfigurationKeys.MULTI_FIELDS should be set on the QueryConfigHandler");
86         }
87
88         if (fields != null && fields.length > 0) {
89           fieldNode.setField(fields[0]);
90
91           if (fields.length == 1) {
92             return fieldNode;
93
94           } else {
95             LinkedList<QueryNode> children = new LinkedList<QueryNode>();
96             children.add(fieldNode);
97
98             for (int i = 1; i < fields.length; i++) {
99               try {
100                 fieldNode = (FieldableNode) fieldNode.cloneTree();
101                 fieldNode.setField(fields[i]);
102
103                 children.add(fieldNode);
104
105               } catch (CloneNotSupportedException e) {
106                 // should never happen
107               }
108
109             }
110
111             return new GroupQueryNode(new BooleanQueryNode(children));
112
113           }
114
115         }
116
117       }
118
119     }
120
121     return node;
122
123   }
124
125   @Override
126   protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
127       throws QueryNodeException {
128
129     return children;
130
131   }
132
133 }