1 package org.apache.lucene.queryParser.core;
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 org.apache.lucene.queryParser.core.builders.QueryBuilder;
21 import org.apache.lucene.queryParser.core.config.QueryConfigHandler;
22 import org.apache.lucene.queryParser.core.nodes.QueryNode;
23 import org.apache.lucene.queryParser.core.parser.SyntaxParser;
24 import org.apache.lucene.queryParser.core.processors.QueryNodeProcessor;
28 * This class is a helper for the query parser framework, it does all the three
29 * query parser phrases at once: text parsing, query processing and query
33 * It contains methods that allows the user to change the implementation used on
37 * @see QueryNodeProcessor
40 * @see QueryConfigHandler
42 public class QueryParserHelper {
44 private QueryNodeProcessor processor;
46 private SyntaxParser syntaxParser;
48 private QueryBuilder builder;
50 private QueryConfigHandler config;
53 * Creates a query parser helper object using the specified configuration,
54 * text parser, processor and builder.
56 * @param queryConfigHandler
57 * the query configuration handler that will be initially set to this
60 * the text parser that will be initially set to this helper
62 * the query processor that will be initially set to this helper
64 * the query builder that will be initially set to this helper
66 * @see QueryNodeProcessor
69 * @see QueryConfigHandler
71 public QueryParserHelper(QueryConfigHandler queryConfigHandler, SyntaxParser syntaxParser, QueryNodeProcessor processor,
72 QueryBuilder builder) {
73 this.syntaxParser = syntaxParser;
74 this.config = queryConfigHandler;
75 this.processor = processor;
76 this.builder = builder;
78 if (processor != null) {
79 processor.setQueryConfigHandler(queryConfigHandler);
85 * Returns the processor object used to process the query node tree, it
86 * returns <code>null</code> if no processor is used.
88 * @return the actual processor used to process the query node tree,
89 * <code>null</code> if no processor is used
91 * @see QueryNodeProcessor
92 * @see #setQueryNodeProcessor(QueryNodeProcessor)
94 public QueryNodeProcessor getQueryNodeProcessor() {
99 * Sets the processor that will be used to process the query node tree. If
100 * there is any {@link QueryConfigHandler} returned by
101 * {@link #getQueryConfigHandler()}, it will be set on the processor. The
102 * argument can be <code>null</code>, which means that no processor will be
103 * used to process the query node tree.
106 * the processor that will be used to process the query node tree,
107 * this argument can be <code>null</code>
109 * @see #getQueryNodeProcessor()
110 * @see QueryNodeProcessor
112 public void setQueryNodeProcessor(QueryNodeProcessor processor) {
113 this.processor = processor;
114 this.processor.setQueryConfigHandler(getQueryConfigHandler());
119 * Sets the text parser that will be used to parse the query string, it cannot
120 * be <code>null</code>.
122 * @param syntaxParser
123 * the text parser that will be used to parse the query string
125 * @see #getSyntaxParser()
128 public void setSyntaxParser(SyntaxParser syntaxParser) {
130 if (syntaxParser == null) {
131 throw new IllegalArgumentException("textParser should not be null!");
134 this.syntaxParser = syntaxParser;
139 * The query builder that will be used to build an object from the query node
140 * tree. It cannot be <code>null</code>.
142 * @param queryBuilder
143 * the query builder used to build something from the query node tree
145 * @see #getQueryBuilder()
148 public void setQueryBuilder(QueryBuilder queryBuilder) {
150 if (queryBuilder == null) {
151 throw new IllegalArgumentException("queryBuilder should not be null!");
154 this.builder = queryBuilder;
159 * Returns the query configuration handler, which is used during the query
160 * node tree processing. It can be <code>null</code>.
162 * @return the query configuration handler used on the query processing,
163 * <code>null</code> if not query configuration handler is defined
165 * @see QueryConfigHandler
166 * @see #setQueryConfigHandler(QueryConfigHandler)
168 public QueryConfigHandler getQueryConfigHandler() {
173 * Returns the query builder used to build a object from the query node tree.
174 * The object produced by this builder is returned by
175 * {@link #parse(String, String)}.
177 * @return the query builder
179 * @see #setQueryBuilder(QueryBuilder)
182 public QueryBuilder getQueryBuilder() {
187 * Returns the text parser used to build a query node tree from a query
188 * string. The default text parser instance returned by this method is a
189 * {@link SyntaxParser}.
191 * @return the text parse used to build query node trees.
194 * @see #setSyntaxParser(SyntaxParser)
196 public SyntaxParser getSyntaxParser() {
197 return this.syntaxParser;
201 * Sets the query configuration handler that will be used during query
202 * processing. It can be <code>null</code>. It's also set to the processor
203 * returned by {@link #getQueryNodeProcessor()}.
206 * the query configuration handler used during query processing, it
207 * can be <code>null</code>
209 * @see #getQueryConfigHandler()
210 * @see QueryConfigHandler
212 public void setQueryConfigHandler(QueryConfigHandler config) {
213 this.config = config;
214 QueryNodeProcessor processor = getQueryNodeProcessor();
216 if (processor != null) {
217 processor.setQueryConfigHandler(config);
223 * Parses a query string to an object, usually some query object. <br/>
225 * In this method the three phases are executed: <br/>
227 * 1st - the query string is parsed using the
228 * text parser returned by {@link #getSyntaxParser()}, the result is a query
231 * 2nd - the query node tree is processed by the
232 * processor returned by {@link #getQueryNodeProcessor()} <br/>
234 * 3th - a object is built from the query node
235 * tree using the builder returned by {@link #getQueryBuilder()}
239 * @param defaultField
240 * the default field used by the text parser
242 * @return the object built from the query
244 * @throws QueryNodeException
245 * if something wrong happens along the three phases
247 public Object parse(String query, String defaultField)
248 throws QueryNodeException {
249 QueryNode queryTree = getSyntaxParser().parse(query, defaultField);
251 QueryNodeProcessor processor = getQueryNodeProcessor();
253 if (processor != null) {
254 queryTree = processor.process(queryTree);
257 return getQueryBuilder().build(queryTree);