Class ComplexPhraseQueryParser
- All Implemented Interfaces:
QueryParserConstants,CommonQueryParserConfiguration
Performs potentially multiple passes over Query text to parse any nested logic in PhraseQueries. - First pass takes any PhraseQuery content between quotes and stores for subsequent pass. All other query content is parsed as normal - Second pass parses any stored PhraseQuery content, checking all embedded clauses are referring to the same field and therefore can be rewritten as Span queries. All PhraseQuery clauses are expressed as ComplexPhraseQuery objects
This could arguably be done in one pass using a new QueryParser but here I am working within the constraints of the existing parser as a base class. This currently simply feeds all phrase content through an analyzer to select phrase terms - any "special" syntax such as * ~ * etc are not given special status
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.lucene.queryparser.classic.QueryParser
QueryParser.OperatorNested classes/interfaces inherited from class org.apache.lucene.util.QueryBuilder
QueryBuilder.TermAndBoost -
Field Summary
Fields inherited from class org.apache.lucene.queryparser.classic.QueryParser
DEFAULT_SPLIT_ON_WHITESPACE, jj_nt, token, token_sourceFields inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
AND_OPERATOR, field, OR_OPERATORFields inherited from class org.apache.lucene.util.QueryBuilder
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrementsFields 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 -
Method Summary
Modifier and TypeMethodDescriptionprotected 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 QuerygetRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) protected QuerygetWildcardQuery(String field, String termStr) Factory method for generating a query.protected QuerynewRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) Builds a newTermRangeQueryinstanceprotected QuerynewTermQuery(Term term, float boost) Parses a query string, returning aQuery.voidsetInOrder(boolean inOrder) WheninOrderis true, the search terms must exists in the documents as the same order as in query.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, trace_enabledMethods inherited from class org.apache.lucene.queryparser.classic.QueryParserBase
addClause, addMultiTermClauses, escape, getAllowLeadingWildcard, getAutoGeneratePhraseQueries, getBooleanQuery, getDateResolution, getDefaultOperator, getDeterminizeWorkLimit, getField, getFieldQuery, getFuzzyDistance, getFuzzyMinSim, getFuzzyPrefixLength, getLocale, getMultiTermRewriteMethod, getPhraseSlop, getPrefixQuery, getRegexpQuery, getTimeZone, init, newBooleanClause, newFieldQuery, newFuzzyQuery, newMatchAllDocsQuery, newPrefixQuery, newRegexpQuery, newWildcardQuery, setAllowLeadingWildcard, setDateResolution, setDateResolution, setDefaultOperator, setDeterminizeWorkLimit, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setMultiTermRewriteMethod, setPhraseSlop, setTimeZoneMethods inherited from class org.apache.lucene.util.QueryBuilder
add, analyzeBoolean, analyzeGraphBoolean, analyzeGraphPhrase, analyzeMultiBoolean, analyzeMultiPhrase, analyzePhrase, analyzeTerm, createBooleanQuery, createBooleanQuery, createFieldQuery, createFieldQuery, createMinShouldMatchQuery, createPhraseQuery, createPhraseQuery, getAnalyzer, getAutoGenerateMultiTermSynonymsPhraseQuery, getEnableGraphQueries, getEnablePositionIncrements, newBooleanQuery, newGraphSynonymQuery, newMultiPhraseQueryBuilder, newSynonymQuery, setAnalyzer, setAutoGenerateMultiTermSynonymsPhraseQuery, setEnableGraphQueries, setEnablePositionIncrementsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.lucene.queryparser.flexible.standard.CommonQueryParserConfiguration
getAnalyzer, getEnablePositionIncrements, setEnablePositionIncrements
-
Constructor Details
-
ComplexPhraseQueryParser
-
-
Method Details
-
setInOrder
public void setInOrder(boolean inOrder) WheninOrderis true, the search terms must exists in the documents as the same order as in query.- Parameters:
inOrder- parameter to choose between ordered or un-ordered proximity search
-
getFieldQuery
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
-
parse
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
-
newTermQuery
- Overrides:
newTermQueryin classQueryBuilder
-
getWildcardQuery
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
-
getRangeQuery
protected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException - Overrides:
getRangeQueryin classQueryParserBase- Throws:
ParseException
-
newRangeQuery
protected Query newRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) Description copied from class:QueryParserBaseBuilds a newTermRangeQueryinstance- Overrides:
newRangeQueryin classQueryParserBase- Parameters:
field- Fieldpart1- minpart2- maxstartInclusive- true if the start of the range is inclusiveendInclusive- true if the end of the range is inclusive- Returns:
- new
TermRangeQueryinstance
-
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
-