Class QueryBuilder
- java.lang.Object
-
- org.apache.lucene.util.QueryBuilder
-
public class QueryBuilder extends Object
Creates queries from theAnalyzerchain.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
newTermQueryare provided so that the generated queries can be customized.
-
-
Constructor Summary
Constructors Constructor Description QueryBuilder(Analyzer analyzer)Creates a new QueryBuilder using the given analyzer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description QuerycreateBooleanQuery(String field, String queryText)Creates a boolean query from the query text.QuerycreateBooleanQuery(String field, String queryText, BooleanClause.Occur operator)Creates a boolean query from the query text.protected QuerycreateFieldQuery(Analyzer analyzer, BooleanClause.Occur operator, String field, String queryText, boolean quoted, int phraseSlop)Creates a query from the analysis chain.QuerycreateMinShouldMatchQuery(String field, String queryText, float fraction)Creates a minimum-should-match query from the query text.QuerycreatePhraseQuery(String field, String queryText)Creates a phrase query from the query text.QuerycreatePhraseQuery(String field, String queryText, int phraseSlop)Creates a phrase query from the query text.AnalyzergetAnalyzer()Returns the analyzer.booleangetEnablePositionIncrements()Returns true if position increments are enabled.protected BooleanQuerynewBooleanQuery(boolean disableCoord)Builds a new BooleanQuery instance.protected MultiPhraseQuerynewMultiPhraseQuery()Builds a new MultiPhraseQuery instance.protected PhraseQuerynewPhraseQuery()Builds a new PhraseQuery instance.protected QuerynewTermQuery(Term term)Builds a new TermQuery instance.voidsetAnalyzer(Analyzer analyzer)Sets the analyzer used to tokenize text.voidsetEnablePositionIncrements(boolean enable)Set totrueto enable position increments in result query.
-
-
-
Constructor Detail
-
QueryBuilder
public QueryBuilder(Analyzer analyzer)
Creates a new QueryBuilder using the given analyzer.
-
-
Method Detail
-
createBooleanQuery
public Query createBooleanQuery(String field, String queryText)
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:
TermQueryorBooleanQuery, based on the analysis ofqueryText
-
createBooleanQuery
public Query createBooleanQuery(String field, String queryText, BooleanClause.Occur operator)
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:
TermQueryorBooleanQuery, based on the analysis ofqueryText
-
createPhraseQuery
public Query createPhraseQuery(String field, String queryText)
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
public Query createPhraseQuery(String field, String queryText, int phraseSlop)
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
public Query createMinShouldMatchQuery(String field, String queryText, float fraction)
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:
TermQueryorBooleanQuery, based on the analysis ofqueryText
-
getAnalyzer
public Analyzer getAnalyzer()
Returns the analyzer.- See Also:
setAnalyzer(Analyzer)
-
setAnalyzer
public void setAnalyzer(Analyzer analyzer)
Sets the analyzer used to tokenize text.
-
getEnablePositionIncrements
public boolean getEnablePositionIncrements()
Returns true if position increments are enabled.- See Also:
setEnablePositionIncrements(boolean)
-
setEnablePositionIncrements
public void setEnablePositionIncrements(boolean enable)
Set totrueto 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.
-
createFieldQuery
protected final Query createFieldQuery(Analyzer analyzer, BooleanClause.Occur operator, String field, String queryText, boolean quoted, int phraseSlop)
Creates a query from the analysis chain.Expert: this is more useful for subclasses such as queryparsers. If using this class directly, just use
createBooleanQuery(String, String)andcreatePhraseQuery(String, String)- Parameters:
analyzer- analyzer used for this queryoperator- default boolean operator used for this queryfield- field to create queries againstqueryText- text to be passed to the analysis chainquoted- true if phrases should be generated when terms occur at more than one positionphraseSlop- slop factor for phrase/multiphrase queries
-
newBooleanQuery
protected BooleanQuery newBooleanQuery(boolean disableCoord)
Builds a new BooleanQuery instance.This is intended for subclasses that wish to customize the generated queries.
- Parameters:
disableCoord- disable coord- Returns:
- new BooleanQuery instance
-
newTermQuery
protected Query newTermQuery(Term term)
Builds a new TermQuery instance.This is intended for subclasses that wish to customize the generated queries.
- Parameters:
term- term- Returns:
- new TermQuery instance
-
newPhraseQuery
protected PhraseQuery newPhraseQuery()
Builds a new PhraseQuery instance.This is intended for subclasses that wish to customize the generated queries.
- Returns:
- new PhraseQuery instance
-
newMultiPhraseQuery
protected MultiPhraseQuery newMultiPhraseQuery()
Builds a new MultiPhraseQuery instance.This is intended for subclasses that wish to customize the generated queries.
- Returns:
- new MultiPhraseQuery instance
-
-