1 package org.apache.lucene.queryParser.standard.processors;
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.LinkedList;
21 import java.util.List;
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;
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/>
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/>
44 * @see ConfigurationKeys#MULTI_FIELDS
46 public class MultiFieldQueryNodeProcessor extends QueryNodeProcessorImpl {
48 private boolean processChildren = true;
50 public MultiFieldQueryNodeProcessor() {
55 protected QueryNode postProcessNode(QueryNode node) throws QueryNodeException {
62 protected void processChildren(QueryNode queryTree) throws QueryNodeException {
64 if (this.processChildren) {
65 super.processChildren(queryTree);
68 this.processChildren = true;
74 protected QueryNode preProcessNode(QueryNode node) throws QueryNodeException {
76 if (node instanceof FieldableNode) {
77 this.processChildren = false;
78 FieldableNode fieldNode = (FieldableNode) node;
80 if (fieldNode.getField() == null) {
81 CharSequence[] fields = getQueryConfigHandler().get(ConfigurationKeys.MULTI_FIELDS);
84 throw new IllegalArgumentException(
85 "StandardQueryConfigHandler.ConfigurationKeys.MULTI_FIELDS should be set on the QueryConfigHandler");
88 if (fields != null && fields.length > 0) {
89 fieldNode.setField(fields[0]);
91 if (fields.length == 1) {
95 LinkedList<QueryNode> children = new LinkedList<QueryNode>();
96 children.add(fieldNode);
98 for (int i = 1; i < fields.length; i++) {
100 fieldNode = (FieldableNode) fieldNode.cloneTree();
101 fieldNode.setField(fields[i]);
103 children.add(fieldNode);
105 } catch (CloneNotSupportedException e) {
106 // should never happen
111 return new GroupQueryNode(new BooleanQueryNode(children));
126 protected List<QueryNode> setChildrenOrder(List<QueryNode> children)
127 throws QueryNodeException {