Class SimpleQueryParser
- java.lang.Object
-
- org.apache.lucene.util.QueryBuilder
-
- org.apache.lucene.queryparser.simple.SimpleQueryParser
-
public class SimpleQueryParser extends QueryBuilder
SimpleQueryParser is used to parse human readable query syntax.The main idea behind this parser is that a person should be able to type whatever they want to represent a query, and this parser will do its best to interpret what to search for no matter how poorly composed the request may be. Tokens are considered to be any of a term, phrase, or subquery for the operations described below. Whitespace including ' ' '\n' '\r' and '\t' and certain operators may be used to delimit tokens ( ) + | " .
Any errors in query syntax will be ignored and the parser will attempt to decipher what it can; however, this may mean odd or unexpected results.
Query Operators
- '
+
' specifiesAND
operation: token1+token2 - '
|
' specifiesOR
operation: token1|token2 - '
-
' negates a single token: -token0 - '
"
' creates phrases of terms: "term1 term2 ..." - '
*
' at the end of terms specifies prefix query: term* - '
~
N' at the end of terms specifies fuzzy query: term~1 - '
~
N' at the end of phrases specifies near query: "term1 term2"~5 - '
(
' and ')
' specifies precedence: token1 + (token2 | token3)
The
default operator
isOR
if no other operator is specified. For example, the following willOR
token1
andtoken2
together: token1 token2Normal operator precedence will be simple order from right to left. For example, the following will evaluate
token1 OR token2
first, thenAND
withtoken3
:token1 | token2 + token3
Escaping
An individual term may contain any possible character with certain characters requiring escaping using a '
\
'. The following characters will need to be escaped in terms and phrases:+ | " ( ) ' \
The '
-
' operator is a special case. On individual terms (not phrases) the first character of a term that is-
must be escaped; however, any '-
' characters beyond the first character do not need to be escaped. For example:-term1
-- SpecifiesNOT
operation againstterm1
\-term1
-- Searches for the term-term1
.term-1
-- Searches for the termterm-1
.term\-1
-- Searches for the termterm-1
.
The '
*
' operator is a special case. On individual terms (not phrases) the last character of a term that is '*
' must be escaped; however, any '*
' characters before the last character do not need to be escaped:term1*
-- Searches for the prefixterm1
term1\*
-- Searches for the termterm1*
term*1
-- Searches for the termterm*1
term\*1
-- Searches for the termterm*1
Note that above examples consider the terms before text processing.
- '
-
-
Field Summary
Fields Modifier and Type Field Description static int
AND_OPERATOR
EnablesAND
operator (+)static int
ESCAPE_OPERATOR
EnablesESCAPE
operator (\)static int
FUZZY_OPERATOR
EnablesFUZZY
operators: (~) on single termsstatic int
NEAR_OPERATOR
EnablesNEAR
operators: (~) on phrasesstatic int
NOT_OPERATOR
EnablesNOT
operator (-)static int
OR_OPERATOR
EnablesOR
operator (|)static int
PHRASE_OPERATOR
EnablesPHRASE
operator (")static int
PRECEDENCE_OPERATORS
EnablesPRECEDENCE
operators:(
and)
static int
PREFIX_OPERATOR
EnablesPREFIX
operator (*)static int
WHITESPACE_OPERATOR
EnablesWHITESPACE
operators: ' ' '\n' '\r' '\t'
-
Constructor Summary
Constructors Constructor Description SimpleQueryParser(Analyzer analyzer, java.lang.String field)
Creates a new parser searching over a single field.SimpleQueryParser(Analyzer analyzer, java.util.Map<java.lang.String,java.lang.Float> weights)
Creates a new parser searching over multiple fields with different weights.SimpleQueryParser(Analyzer analyzer, java.util.Map<java.lang.String,java.lang.Float> weights, int flags)
Creates a new parser with custom flags used to enable/disable certain features.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description BooleanClause.Occur
getDefaultOperator()
Returns the implicit operator setting, which will be eitherSHOULD
orMUST
.Query
parse(java.lang.String queryText)
Parses the query text and returns parsed query (or null if empty)void
setDefaultOperator(BooleanClause.Occur operator)
Sets the implicit operator setting, which must be eitherSHOULD
orMUST
.-
Methods inherited from class org.apache.lucene.util.QueryBuilder
createBooleanQuery, createBooleanQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, getAnalyzer, getEnablePositionIncrements, setAnalyzer, setEnablePositionIncrements
-
-
-
-
Field Detail
-
AND_OPERATOR
public static final int AND_OPERATOR
EnablesAND
operator (+)- See Also:
- Constant Field Values
-
NOT_OPERATOR
public static final int NOT_OPERATOR
EnablesNOT
operator (-)- See Also:
- Constant Field Values
-
OR_OPERATOR
public static final int OR_OPERATOR
EnablesOR
operator (|)- See Also:
- Constant Field Values
-
PREFIX_OPERATOR
public static final int PREFIX_OPERATOR
EnablesPREFIX
operator (*)- See Also:
- Constant Field Values
-
PHRASE_OPERATOR
public static final int PHRASE_OPERATOR
EnablesPHRASE
operator (")- See Also:
- Constant Field Values
-
PRECEDENCE_OPERATORS
public static final int PRECEDENCE_OPERATORS
EnablesPRECEDENCE
operators:(
and)
- See Also:
- Constant Field Values
-
ESCAPE_OPERATOR
public static final int ESCAPE_OPERATOR
EnablesESCAPE
operator (\)- See Also:
- Constant Field Values
-
WHITESPACE_OPERATOR
public static final int WHITESPACE_OPERATOR
EnablesWHITESPACE
operators: ' ' '\n' '\r' '\t'- See Also:
- Constant Field Values
-
FUZZY_OPERATOR
public static final int FUZZY_OPERATOR
EnablesFUZZY
operators: (~) on single terms- See Also:
- Constant Field Values
-
NEAR_OPERATOR
public static final int NEAR_OPERATOR
EnablesNEAR
operators: (~) on phrases- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SimpleQueryParser
public SimpleQueryParser(Analyzer analyzer, java.lang.String field)
Creates a new parser searching over a single field.
-
SimpleQueryParser
public SimpleQueryParser(Analyzer analyzer, java.util.Map<java.lang.String,java.lang.Float> weights)
Creates a new parser searching over multiple fields with different weights.
-
SimpleQueryParser
public SimpleQueryParser(Analyzer analyzer, java.util.Map<java.lang.String,java.lang.Float> weights, int flags)
Creates a new parser with custom flags used to enable/disable certain features.
-
-
Method Detail
-
parse
public Query parse(java.lang.String queryText)
Parses the query text and returns parsed query (or null if empty)
-
getDefaultOperator
public BooleanClause.Occur getDefaultOperator()
Returns the implicit operator setting, which will be eitherSHOULD
orMUST
.
-
setDefaultOperator
public void setDefaultOperator(BooleanClause.Occur operator)
Sets the implicit operator setting, which must be eitherSHOULD
orMUST
.
-
-