home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (UserInputQueryBuilder.java)

This example Lucene source code file (UserInputQueryBuilder.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

analyzer, analyzer, dom, parseexception, parserexception, query, querybuilder, queryparser, queryparser, string, string, userinputquerybuilder, userinputquerybuilder

The Lucene UserInputQueryBuilder.java source code

package org.apache.lucene.xmlparser.builders;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Query;
import org.apache.lucene.xmlparser.DOMUtils;
import org.apache.lucene.xmlparser.ParserException;
import org.apache.lucene.xmlparser.QueryBuilder;
import org.w3c.dom.Element;
import org.apache.lucene.util.Version;

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

 * UserInputQueryBuilder uses 1 of 2 strategies for thread-safe parsing:
 * 1) Synchronizing access to "parse" calls on a previously supplied QueryParser
 * or..
 * 2) creating a new QueryParser object for each parse request
public class UserInputQueryBuilder implements QueryBuilder {

	QueryParser unSafeParser;
	private Analyzer analyzer;
	private String defaultField;
	 * This constructor has the disadvantage of not being able to change choice of default field name
	 * @param parser thread un-safe query parser
	public UserInputQueryBuilder(QueryParser parser) {
		this.unSafeParser = parser;

	public UserInputQueryBuilder(String defaultField, Analyzer analyzer) {
		this.analyzer = analyzer;
		this.defaultField = defaultField;
	/* (non-Javadoc)
	 * @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element)
	public Query getQuery(Element e) throws ParserException {
		String text=DOMUtils.getText(e);
		try {
			Query q = null;
				//synchronize on unsafe parser
				synchronized (unSafeParser)
					q = unSafeParser.parse(text);
				String fieldName=DOMUtils.getAttribute(e, "fieldName", defaultField);
				//Create new parser
				QueryParser parser=createQueryParser(fieldName, analyzer);
				q = parser.parse(text);				
			return q;
		} catch (ParseException e1) {
			throw new ParserException(e1.getMessage());
	 * Method to create a QueryParser - designed to be overridden
	 * @param fieldName
	 * @param analyzer
	 * @return QueryParser
	protected QueryParser createQueryParser(String fieldName, Analyzer analyzer)
		return new QueryParser(Version.LUCENE_CURRENT, fieldName,analyzer);


Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene UserInputQueryBuilder.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.