1 package org.apache.lucene.xmlparser.builders;
3 import org.apache.lucene.analysis.Analyzer;
4 import org.apache.lucene.queryParser.ParseException;
5 import org.apache.lucene.queryParser.QueryParser;
6 import org.apache.lucene.search.Query;
7 import org.apache.lucene.xmlparser.DOMUtils;
8 import org.apache.lucene.xmlparser.ParserException;
9 import org.apache.lucene.xmlparser.QueryBuilder;
10 import org.w3c.dom.Element;
11 import org.apache.lucene.util.Version;
14 * Licensed to the Apache Software Foundation (ASF) under one or more
15 * contributor license agreements. See the NOTICE file distributed with
16 * this work for additional information regarding copyright ownership.
17 * The ASF licenses this file to You under the Apache License, Version 2.0
18 * (the "License"); you may not use this file except in compliance with
19 * the License. You may obtain a copy of the License at
21 * http://www.apache.org/licenses/LICENSE-2.0
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS,
25 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
31 * UserInputQueryBuilder uses 1 of 2 strategies for thread-safe parsing:
32 * 1) Synchronizing access to "parse" calls on a previously supplied QueryParser
34 * 2) creating a new QueryParser object for each parse request
36 public class UserInputQueryBuilder implements QueryBuilder {
38 QueryParser unSafeParser;
39 private Analyzer analyzer;
40 private String defaultField;
43 * This constructor has the disadvantage of not being able to change choice of default field name
44 * @param parser thread un-safe query parser
46 public UserInputQueryBuilder(QueryParser parser) {
47 this.unSafeParser = parser;
50 public UserInputQueryBuilder(String defaultField, Analyzer analyzer) {
51 this.analyzer = analyzer;
52 this.defaultField = defaultField;
56 * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
58 public Query getQuery(Element e) throws ParserException {
59 String text=DOMUtils.getText(e);
62 if(unSafeParser!=null)
64 //synchronize on unsafe parser
65 synchronized (unSafeParser)
67 q = unSafeParser.parse(text);
72 String fieldName=DOMUtils.getAttribute(e, "fieldName", defaultField);
74 QueryParser parser=createQueryParser(fieldName, analyzer);
75 q = parser.parse(text);
77 // use the boost of the original query here, too and multiply (which may be != 1.0f):
78 q.setBoost(q.getBoost()*DOMUtils.getAttribute(e,"boost",1.0f));
80 } catch (ParseException e1) {
81 throw new ParserException(e1.getMessage());
86 * Method to create a QueryParser - designed to be overridden
91 protected QueryParser createQueryParser(String fieldName, Analyzer analyzer)
93 return new QueryParser(Version.LUCENE_CURRENT, fieldName,analyzer);