Class QueryParser

java.lang.Object
org.codelibs.fess.query.parser.QueryParser

public class QueryParser extends Object
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.

  • Field Details

    • defaultField

      protected String defaultField
      The default field to search in when no field is specified in the query
    • analyzer

      protected org.apache.lucene.analysis.Analyzer analyzer
      The analyzer used to analyze query terms
    • allowLeadingWildcard

      protected boolean allowLeadingWildcard
      Whether to allow leading wildcards in query terms
    • defaultOperator

      protected org.apache.lucene.queryparser.classic.QueryParser.Operator defaultOperator
      The default operator to use between query terms
    • filterList

      protected List<QueryParser.Filter> filterList
      List of filters to apply to queries
    • filterChain

      protected QueryParser.FilterChain 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

      public org.apache.lucene.search.Query parse(String query)
      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

      public void setDefaultField(String defaultField)
      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

      public void addFilter(QueryParser.Filter filter)
      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 append
      chain - the existing filter chain
      Returns:
      a new filter chain with the filter appended
    • createDefaultFilterChain

      protected QueryParser.FilterChain 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