Class StandardQueryParser

java.lang.Object
org.apache.lucene.queryparser.flexible.core.QueryParserHelper
org.apache.lucene.queryparser.flexible.standard.StandardQueryParser
All Implemented Interfaces:
CommonQueryParserConfiguration
Direct Known Subclasses:
PrecedenceQueryParser

public class StandardQueryParser extends QueryParserHelper implements CommonQueryParserConfiguration
This class is a helper that enables users to easily use the Lucene query parser.

To construct a Query object from a query string, use the parse(String, String) method:

 StandardQueryParser queryParserHelper = new StandardQueryParser();
 Query query = queryParserHelper.parse("a AND b", "defaultField");
 

To change any configuration before parsing the query string do, for example:

 // the query config handler returned by StandardQueryParser is a StandardQueryConfigHandler
 queryParserHelper.getQueryConfigHandler().setAnalyzer(new WhitespaceAnalyzer());
 

The syntax for query strings is as follows (copied from the old QueryParser javadoc): A Query is a series of clauses. A clause may be prefixed by:

  • a plus (+) or a minus (-) sign, indicating that the clause is required or prohibited respectively; or
  • a term followed by a colon, indicating the field to be searched. This enables one to construct queries which search multiple fields.
A clause may be either:
  • a term, indicating all the documents that contain this term; or
  • a nested query, enclosed in parentheses. Note that this may be used with a +/- prefix to require any of a set of terms.
Thus, in BNF, the query grammar is:
   Query  ::= ( Clause )*
   Clause ::= ["+", "-"] [<TERM> ":"] ( <TERM> | "(" Query ")" )
 

Examples of appropriately formatted queries can be found in the query syntax documentation.

The text parser used by this helper is a StandardSyntaxParser.

The query node processor used by this helper is a StandardQueryNodeProcessorPipeline.

The builder used by this helper is a StandardQueryTreeBuilder.

See Also: