Package org.apache.lucene.util
Class QueryBuilder
java.lang.Object
org.apache.lucene.util.QueryBuilder
- Direct Known Subclasses:
QueryParserBase
,SimpleQueryParser
Creates queries from the
Analyzer
chain.
Example usage:
QueryBuilder builder = new QueryBuilder(analyzer); Query a = builder.createBooleanQuery("body", "just a test"); Query b = builder.createPhraseQuery("body", "another test"); Query c = builder.createMinShouldMatchQuery("body", "another test", 0.5f);
This can also be used as a subclass for query parsers to make it easier
to interact with the analysis chain. Factory methods such as newTermQuery
are provided so that the generated queries can be customized.
-
Constructor Summary
ConstructorsConstructorDescriptionQueryBuilder
(Analyzer analyzer) Creates a new QueryBuilder using the given analyzer. -
Method Summary
Modifier and TypeMethodDescriptioncreateBooleanQuery
(String field, String queryText) Creates a boolean query from the query text.createBooleanQuery
(String field, String queryText, BooleanClause.Occur operator) Creates a boolean query from the query text.createMinShouldMatchQuery
(String field, String queryText, float fraction) Creates a minimum-should-match query from the query text.createPhraseQuery
(String field, String queryText) Creates a phrase query from the query text.createPhraseQuery
(String field, String queryText, int phraseSlop) Creates a phrase query from the query text.Returns the analyzer.boolean
Returns true if position increments are enabled.void
setAnalyzer
(Analyzer analyzer) Sets the analyzer used to tokenize text.void
setEnablePositionIncrements
(boolean enable) Set totrue
to enable position increments in result query.
-
Constructor Details
-
QueryBuilder
Creates a new QueryBuilder using the given analyzer.
-
-
Method Details
-
createBooleanQuery
Creates a boolean query from the query text.This is equivalent to
createBooleanQuery(field, queryText, Occur.SHOULD)
- Parameters:
field
- field namequeryText
- text to be passed to the analyzer- Returns:
TermQuery
orBooleanQuery
, based on the analysis ofqueryText
-
createBooleanQuery
Creates a boolean query from the query text.- Parameters:
field
- field namequeryText
- text to be passed to the analyzeroperator
- operator used for clauses between analyzer tokens.- Returns:
TermQuery
orBooleanQuery
, based on the analysis ofqueryText
-
createPhraseQuery
Creates a phrase query from the query text.This is equivalent to
createPhraseQuery(field, queryText, 0)
- Parameters:
field
- field namequeryText
- text to be passed to the analyzer- Returns:
TermQuery
,BooleanQuery
,PhraseQuery
, orMultiPhraseQuery
, based on the analysis ofqueryText
-
createPhraseQuery
Creates a phrase query from the query text.- Parameters:
field
- field namequeryText
- text to be passed to the analyzerphraseSlop
- number of other words permitted between words in query phrase- Returns:
TermQuery
,BooleanQuery
,PhraseQuery
, orMultiPhraseQuery
, based on the analysis ofqueryText
-
createMinShouldMatchQuery
Creates a minimum-should-match query from the query text.- Parameters:
field
- field namequeryText
- text to be passed to the analyzerfraction
- of query terms[0..1]
that should match- Returns:
TermQuery
orBooleanQuery
, based on the analysis ofqueryText
-
getAnalyzer
Returns the analyzer.- See Also:
-
setAnalyzer
Sets the analyzer used to tokenize text. -
getEnablePositionIncrements
public boolean getEnablePositionIncrements()Returns true if position increments are enabled.- See Also:
-
setEnablePositionIncrements
public void setEnablePositionIncrements(boolean enable) Set totrue
to enable position increments in result query.When set, result phrase and multi-phrase queries will be aware of position increments. Useful when e.g. a StopFilter increases the position increment of the token that follows an omitted token.
Default: true.
-