Package org.codelibs.fess.query.parser
Class QueryParser
java.lang.Object
org.codelibs.fess.query.parser.QueryParser
A query parser that processes search queries and converts them to Lucene Query objects.
This class provides a flexible architecture using a chain of filters to process and transform
queries before they are parsed by the underlying Lucene query parser.
The parser supports configuration of default field, analyzer, wildcard settings, and default operator. It also allows adding custom filters to modify query behavior.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for query filters that can modify or transform queries.static interfaceInterface for the filter chain that processes queries.protected static classCustom Lucene query parser that extends the standard QueryParser to provide additional functionality for quoted queries. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanWhether to allow leading wildcards in query termsprotected org.apache.lucene.analysis.AnalyzerThe analyzer used to analyze query termsprotected StringThe default field to search in when no field is specified in the queryprotected org.apache.lucene.queryparser.classic.QueryParser.OperatorThe default operator to use between query termsprotected QueryParser.FilterChainThe filter chain used to process queriesprotected List<QueryParser.Filter> List of filters to apply to queries -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFilter(QueryParser.Filter filter) Adds a filter to the query processing chain.protected QueryParser.FilterChainappendFilterChain(QueryParser.Filter filter, QueryParser.FilterChain chain) Appends a filter to the existing filter chain.protected QueryParser.FilterChainCreates the default filter chain that performs the actual query parsing.protected voidCreates the filter chain by combining all registered filters.protected org.apache.lucene.queryparser.classic.QueryParserCreates a new Lucene query parser with the current configuration.voidinit()Initializes the query parser by creating the filter chain.org.apache.lucene.search.QueryParses the given query string and returns a Lucene Query object.voidsetAllowLeadingWildcard(boolean allowLeadingWildcard) Sets whether to allow leading wildcards in query terms.voidsetAnalyzer(org.apache.lucene.analysis.Analyzer analyzer) Sets the analyzer used to analyze query terms.voidsetDefaultField(String defaultField) Sets the default field to search in when no field is specified in the query.voidsetDefaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator defaultOperator) Sets the default operator to use between query terms.
-
Field Details
-
defaultField
The default field to search in when no field is specified in the query -
analyzer
protected org.apache.lucene.analysis.Analyzer analyzerThe analyzer used to analyze query terms -
allowLeadingWildcard
protected boolean allowLeadingWildcardWhether to allow leading wildcards in query terms -
defaultOperator
protected org.apache.lucene.queryparser.classic.QueryParser.Operator defaultOperatorThe default operator to use between query terms -
filterList
List of filters to apply to queries -
filterChain
The filter chain used to process queries
-
-
Constructor Details
-
QueryParser
public QueryParser()Default constructor.
-
-
Method Details
-
init
@PostConstruct public void init()Initializes the query parser by creating the filter chain. This method is called automatically after construction. -
parse
Parses the given query string and returns a Lucene Query object. The query is processed through the filter chain before being parsed.- Parameters:
query- the query string to parse- Returns:
- the parsed Query object
- Throws:
QueryParseException- if the query cannot be parsed
-
createQueryParser
protected org.apache.lucene.queryparser.classic.QueryParser createQueryParser()Creates a new Lucene query parser with the current configuration. The parser is configured with the default field, analyzer, wildcard settings, and default operator.- Returns:
- a configured Lucene query parser
-
setDefaultField
Sets the default field to search in when no field is specified in the query.- Parameters:
defaultField- the default field name
-
setAnalyzer
public void setAnalyzer(org.apache.lucene.analysis.Analyzer analyzer) Sets the analyzer used to analyze query terms.- Parameters:
analyzer- the analyzer to use
-
setAllowLeadingWildcard
public void setAllowLeadingWildcard(boolean allowLeadingWildcard) Sets whether to allow leading wildcards in query terms.- Parameters:
allowLeadingWildcard- true to allow leading wildcards, false otherwise
-
setDefaultOperator
public void setDefaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator defaultOperator) Sets the default operator to use between query terms.- Parameters:
defaultOperator- the default operator (AND or OR)
-
addFilter
Adds a filter to the query processing chain. The filter chain is recreated after adding the filter.- Parameters:
filter- the filter to add
-
createFilterChain
protected void createFilterChain()Creates the filter chain by combining all registered filters. The chain starts with the default filter chain and appends each registered filter. -
appendFilterChain
protected QueryParser.FilterChain appendFilterChain(QueryParser.Filter filter, QueryParser.FilterChain chain) Appends a filter to the existing filter chain.- Parameters:
filter- the filter to appendchain- the existing filter chain- Returns:
- a new filter chain with the filter appended
-
createDefaultFilterChain
Creates the default filter chain that performs the actual query parsing. This chain uses the Lucene query parser to parse the query string.- Returns:
- the default filter chain
-