Class QueryStringQueryParser
- java.lang.Object
-
- org.apache.lucene.util.QueryBuilder
-
- org.apache.lucene.queryparser.classic.QueryParserBase
-
- org.apache.lucene.queryparser.classic.QueryParser
-
- org.apache.lucene.queryparser.classic.XQueryParser
-
- org.elasticsearch.index.search.QueryStringQueryParser
-
- All Implemented Interfaces:
QueryParserConstants,CommonQueryParserConfiguration
public class QueryStringQueryParser extends XQueryParser
AXQueryParserthat uses theMapperServicein order to build smarter queries based on the mapping information. This class usesMultiMatchQueryto build the text query around operators andXQueryParserto assemble the result logically.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.lucene.queryparser.classic.QueryParser
QueryParser.Operator
-
-
Field Summary
-
Fields inherited from class org.apache.lucene.queryparser.classic.QueryParser
DEFAULT_SPLIT_ON_WHITESPACE, jj_nt, token, token_source
-
Fields inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
AND_OPERATOR, field, OR_OPERATOR
-
Fields inherited from class org.apache.lucene.util.QueryBuilder
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements
-
Fields inherited from interface org.apache.lucene.queryparser.classic.QueryParserConstants
_ESCAPED_CHAR, _NUM_CHAR, _QUOTED_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, AND, BAREOPER, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, Range, RANGE_GOOP, RANGE_QUOTED, RANGE_TO, RANGEEX_END, RANGEEX_START, RANGEIN_END, RANGEIN_START, REGEXPTERM, RPAREN, STAR, TERM, tokenImage, WILDTERM
-
-
Constructor Summary
Constructors Constructor Description QueryStringQueryParser(QueryShardContext context, boolean lenient)Defaults to all queryiable fields extracted from the mapping for query termsQueryStringQueryParser(QueryShardContext context, String defaultField)QueryStringQueryParser(QueryShardContext context, String defaultField, boolean lenient)QueryStringQueryParser(QueryShardContext context, Map<String,Float> fieldsAndWeights)QueryStringQueryParser(QueryShardContext context, Map<String,Float> fieldsAndWeights, boolean lenient)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected QuerygetBooleanQuery(List<BooleanClause> clauses)Factory method for generating query, given a set of clauses.QuerygetFieldQuery(String field, String queryText, boolean quoted)protected QuerygetFieldQuery(String field, String queryText, int slop)Base implementation delegates toQueryParserBase.getFieldQuery(String,String,boolean).protected QuerygetFuzzyQuery(String field, String termStr, float minSimilarity)Factory method for generating a query (similar toQueryParserBase.getWildcardQuery(java.lang.String, java.lang.String)).protected QuerygetPrefixQuery(String field, String termStr)Factory method for generating a query (similar toQueryParserBase.getWildcardQuery(java.lang.String, java.lang.String)).protected QuerygetRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive)protected QuerygetRegexpQuery(String field, String termStr)Factory method for generating a query.protected QuerygetWildcardQuery(String field, String termStr)Factory method for generating a query.protected QueryhandleBareFuzzy(String field, Token fuzzySlop, String termImage)protected QuerynewFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)Builds a new FuzzyQuery instanceprotected QuerynewMatchAllDocsQuery()Builds a new MatchAllDocsQuery instanceQueryparse(String query)Parses a query string, returning aQuery.voidsetAnalyzeWildcard(boolean analyzeWildcard)voidsetAutoGenerateMultiTermSynonymsPhraseQuery(boolean enable)Set totrueif phrase queries should be automatically generated for multi terms synonyms.voidsetDefaultOperator(QueryParser.Operator op)Sets the boolean operator of the QueryParser.voidsetEnablePositionIncrements(boolean enable)Set totrueto enable position increments in result query.voidsetForceAnalyzer(Analyzer analyzer)voidsetForceQuoteAnalyzer(Analyzer analyzer)voidsetFuzziness(Fuzziness fuzziness)voidsetFuzzyMaxExpansions(int fuzzyMaxExpansions)voidsetFuzzyRewriteMethod(MultiTermQuery.RewriteMethod fuzzyRewriteMethod)voidsetFuzzyTranspositions(boolean fuzzyTranspositions)voidsetGroupTieBreaker(float groupTieBreaker)voidsetPhraseSlop(int phraseSlop)Sets the default slop for phrases.voidsetQuoteFieldSuffix(String quoteFieldSuffix)voidsetTimeZone(ZoneId timeZone)voidsetType(MultiMatchQueryBuilder.Type type)-
Methods inherited from class org.apache.lucene.queryparser.classic.QueryParser
Clause, Conjunction, disable_tracing, enable_tracing, generateParseException, getNextToken, getSplitOnWhitespace, getToken, Modifiers, MultiTerm, Query, ReInit, ReInit, setAutoGeneratePhraseQueries, setSplitOnWhitespace, Term, TopLevelQuery
-
Methods inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
addClause, addMultiTermClauses, escape, getAllowLeadingWildcard, getAutoGeneratePhraseQueries, getDateResolution, getDefaultOperator, getField, getFuzzyMinSim, getFuzzyPrefixLength, getLocale, getMaxDeterminizedStates, getMultiTermRewriteMethod, getPhraseSlop, getTimeZone, init, newBooleanClause, newFieldQuery, newPrefixQuery, newRangeQuery, newRegexpQuery, newWildcardQuery, setAllowLeadingWildcard, setDateResolution, setDateResolution, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setMaxDeterminizedStates, setMultiTermRewriteMethod, setTimeZone
-
Methods inherited from class org.apache.lucene.util.QueryBuilder
add, analyzeBoolean, analyzeGraphBoolean, analyzeGraphPhrase, analyzeMultiBoolean, analyzeMultiPhrase, analyzePhrase, analyzeTerm, createBooleanQuery, createBooleanQuery, createFieldQuery, createFieldQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, createSpanQuery, getAnalyzer, getAutoGenerateMultiTermSynonymsPhraseQuery, getEnableGraphQueries, getEnablePositionIncrements, newBooleanQuery, newGraphSynonymQuery, newMultiPhraseQueryBuilder, newSynonymQuery, newTermQuery, setAnalyzer, setEnableGraphQueries
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration
getAnalyzer, getEnablePositionIncrements
-
-
-
-
Constructor Detail
-
QueryStringQueryParser
public QueryStringQueryParser(QueryShardContext context, String defaultField)
- Parameters:
context- The query shard context.defaultField- The default field for query terms.
-
QueryStringQueryParser
public QueryStringQueryParser(QueryShardContext context, String defaultField, boolean lenient)
- Parameters:
context- The query shard context.defaultField- The default field for query terms.lenient- If set to `true` will cause format based failures (like providing text to a numeric field) to be ignored.
-
QueryStringQueryParser
public QueryStringQueryParser(QueryShardContext context, Map<String,Float> fieldsAndWeights)
- Parameters:
context- The query shard contextfieldsAndWeights- The default fields and weights expansion for query terms
-
QueryStringQueryParser
public QueryStringQueryParser(QueryShardContext context, Map<String,Float> fieldsAndWeights, boolean lenient)
- Parameters:
context- The query shard context.fieldsAndWeights- The default fields and weights expansion for query terms.lenient- If set to `true` will cause format based failures (like providing text to a numeric field) to be ignored.
-
QueryStringQueryParser
public QueryStringQueryParser(QueryShardContext context, boolean lenient)
Defaults to all queryiable fields extracted from the mapping for query terms- Parameters:
context- The query shard contextlenient- If set to `true` will cause format based failures (like providing text to a numeric field) to be ignored.
-
-
Method Detail
-
setEnablePositionIncrements
public void setEnablePositionIncrements(boolean enable)
Description copied from class:QueryBuilderSet 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.
- Specified by:
setEnablePositionIncrementsin interfaceCommonQueryParserConfiguration- Overrides:
setEnablePositionIncrementsin classQueryBuilder
-
setDefaultOperator
public void setDefaultOperator(QueryParser.Operator op)
Description copied from class:QueryParserBaseSets the boolean operator of the QueryParser. In default mode (OR_OPERATOR) terms without any modifiers are considered optional: for examplecapital of Hungaryis equal tocapital OR of OR Hungary.
InAND_OPERATORmode terms are considered to be in conjunction: the above mentioned query is parsed ascapital AND of AND Hungary- Overrides:
setDefaultOperatorin classQueryParserBase
-
setPhraseSlop
public void setPhraseSlop(int phraseSlop)
Description copied from class:QueryParserBaseSets the default slop for phrases. If zero, then exact phrase matches are required. Default value is zero.- Specified by:
setPhraseSlopin interfaceCommonQueryParserConfiguration- Overrides:
setPhraseSlopin classQueryParserBase
-
setType
public void setType(MultiMatchQueryBuilder.Type type)
- Parameters:
type- Sets how multiple fields should be combined to build textual part queries.
-
setFuzziness
public void setFuzziness(Fuzziness fuzziness)
- Parameters:
fuzziness- Sets the defaultFuzzinessfor fuzzy query. Defaults toFuzziness.AUTO.
-
setFuzzyRewriteMethod
public void setFuzzyRewriteMethod(MultiTermQuery.RewriteMethod fuzzyRewriteMethod)
- Parameters:
fuzzyRewriteMethod- Sets the default rewrite method for fuzzy query.
-
setFuzzyMaxExpansions
public void setFuzzyMaxExpansions(int fuzzyMaxExpansions)
- Parameters:
fuzzyMaxExpansions- Sets the maximum number of expansions allowed in a fuzzy query. Defaults toFuzzyQuery.defaultMaxExpansions.
-
setForceAnalyzer
public void setForceAnalyzer(Analyzer analyzer)
- Parameters:
analyzer- Force the provided analyzer to be used for all query analysis regardless of the field.
-
setForceQuoteAnalyzer
public void setForceQuoteAnalyzer(Analyzer analyzer)
- Parameters:
analyzer- Force the provided analyzer to be used for all phrase query analysis regardless of the field.
-
setQuoteFieldSuffix
public void setQuoteFieldSuffix(String quoteFieldSuffix)
- Parameters:
quoteFieldSuffix- The suffix to append to fields for quoted parts of the query string.
-
setAnalyzeWildcard
public void setAnalyzeWildcard(boolean analyzeWildcard)
- Parameters:
analyzeWildcard- If true, the wildcard operator analyzes the term to build a wildcard query. Otherwise the query terms are only normalized.
-
setTimeZone
public void setTimeZone(ZoneId timeZone)
- Parameters:
timeZone- Time Zone to be applied to any range query related to dates.
-
setGroupTieBreaker
public void setGroupTieBreaker(float groupTieBreaker)
- Parameters:
groupTieBreaker- The tie breaker to apply when multiple fields are used.
-
setAutoGenerateMultiTermSynonymsPhraseQuery
public void setAutoGenerateMultiTermSynonymsPhraseQuery(boolean enable)
Description copied from class:QueryBuilderSet totrueif phrase queries should be automatically generated for multi terms synonyms. Default: false.- Overrides:
setAutoGenerateMultiTermSynonymsPhraseQueryin classQueryBuilder
-
setFuzzyTranspositions
public void setFuzzyTranspositions(boolean fuzzyTranspositions)
- Parameters:
fuzzyTranspositions- Sets whether transpositions are supported in fuzzy queries. Defaults toFuzzyQuery.defaultTranspositions.
-
newMatchAllDocsQuery
protected Query newMatchAllDocsQuery()
Description copied from class:QueryParserBaseBuilds a new MatchAllDocsQuery instance- Overrides:
newMatchAllDocsQueryin classQueryParserBase- Returns:
- new MatchAllDocsQuery instance
-
getFieldQuery
public Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException
- Overrides:
getFieldQueryin classQueryParserBase- Throws:
ParseException- throw in overridden method to disallow
-
getFieldQuery
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException
Description copied from class:QueryParserBaseBase implementation delegates toQueryParserBase.getFieldQuery(String,String,boolean). This method may be overridden, for example, to return a SpanNearQuery instead of a PhraseQuery.- Overrides:
getFieldQueryin classQueryParserBase- Throws:
ParseException- throw in overridden method to disallow
-
getRangeQuery
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException
- Overrides:
getRangeQueryin classQueryParserBase- Throws:
ParseException
-
handleBareFuzzy
protected Query handleBareFuzzy(String field, Token fuzzySlop, String termImage) throws ParseException
- Overrides:
handleBareFuzzyin classXQueryParser- Throws:
ParseException
-
getFuzzyQuery
protected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException
Description copied from class:QueryParserBaseFactory method for generating a query (similar toQueryParserBase.getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term token that has the fuzzy suffix (~) appended.- Overrides:
getFuzzyQueryin classQueryParserBase- Parameters:
field- Name of the field query will use.termStr- Term token to use for building term for the query- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
newFuzzyQuery
protected Query newFuzzyQuery(Term term, float minimumSimilarity, int prefixLength)
Description copied from class:QueryParserBaseBuilds a new FuzzyQuery instance- Overrides:
newFuzzyQueryin classQueryParserBase- Parameters:
term- TermminimumSimilarity- minimum similarityprefixLength- prefix length- Returns:
- new FuzzyQuery Instance
-
getPrefixQuery
protected Query getPrefixQuery(String field, String termStr) throws ParseException
Description copied from class:QueryParserBaseFactory method for generating a query (similar toQueryParserBase.getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term token that uses prefix notation; that is, contains a single '*' wildcard character as its last character. Since this is a special case of generic wildcard term, and such a query can be optimized easily, this usually results in a different query object.Depending on settings, a prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.
Can be overridden by extending classes, to provide custom handling for wild card queries, which may be necessary due to missing analyzer calls.
- Overrides:
getPrefixQueryin classQueryParserBase- Parameters:
field- Name of the field query will use.termStr- Term token to use for building term for the query (without trailing '*' character!)- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getWildcardQuery
protected Query getWildcardQuery(String field, String termStr) throws ParseException
Description copied from class:QueryParserBaseFactory method for generating a query. Called when parser parses an input term token that contains one or more wildcard characters (? and *), but is not a prefix term token (one that has just a single * character at the end)Depending on settings, prefix term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with wildcard templates.
Can be overridden by extending classes, to provide custom handling for wildcard queries, which may be necessary due to missing analyzer calls.
- Overrides:
getWildcardQueryin classQueryParserBase- Parameters:
field- Name of the field query will use.termStr- Term token that contains one or more wild card characters (? or *), but is not simple prefix term- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getRegexpQuery
protected Query getRegexpQuery(String field, String termStr) throws ParseException
Description copied from class:QueryParserBaseFactory method for generating a query. Called when parser parses an input term token that contains a regular expression query.Depending on settings, pattern term may be lower-cased automatically. It will not go through the default Analyzer, however, since normal Analyzers are unlikely to work properly with regular expression templates.
Can be overridden by extending classes, to provide custom handling for regular expression queries, which may be necessary due to missing analyzer calls.
- Overrides:
getRegexpQueryin classQueryParserBase- Parameters:
field- Name of the field query will use.termStr- Term token that contains a regular expression- Returns:
- Resulting
Querybuilt for the term - Throws:
ParseException- throw in overridden method to disallow
-
getBooleanQuery
protected Query getBooleanQuery(List<BooleanClause> clauses) throws ParseException
Description copied from class:QueryParserBaseFactory method for generating query, given a set of clauses. By default creates a boolean query composed of clauses passed in. Can be overridden by extending classes, to modify query being returned.- Overrides:
getBooleanQueryin classQueryParserBase- Parameters:
clauses- List that containsBooleanClauseinstances to join.- Returns:
- Resulting
Queryobject. - Throws:
ParseException- throw in overridden method to disallow
-
parse
public Query parse(String query) throws ParseException
Description copied from class:QueryParserBaseParses a query string, returning aQuery.- Overrides:
parsein classQueryParserBase- Parameters:
query- the query string to be parsed.- Throws:
ParseException- if the parsing fails
-
-