public class MultiFieldQueryParser extends QueryParser
QueryParser.Operator
QueryBuilder.TermAndBoost
Modifier and Type | Field and Description |
---|---|
protected Map<String,Float> |
boosts |
protected String[] |
fields |
DEFAULT_SPLIT_ON_WHITESPACE, jj_nt, token, token_source
AND_OPERATOR, field, OR_OPERATOR
analyzer, autoGenerateMultiTermSynonymsPhraseQuery, enableGraphQueries, enablePositionIncrements
_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 and Description |
---|
MultiFieldQueryParser(String[] fields,
Analyzer analyzer)
Creates a MultiFieldQueryParser.
|
MultiFieldQueryParser(String[] fields,
Analyzer analyzer,
Map<String,Float> boosts)
Creates a MultiFieldQueryParser.
|
Modifier and Type | Method and Description |
---|---|
protected Query |
getFieldQuery(String field,
String queryText,
boolean quoted) |
protected Query |
getFieldQuery(String field,
String queryText,
int slop)
Base implementation delegates to
QueryParserBase.getFieldQuery(String,String,boolean) . |
protected Query |
getFuzzyQuery(String field,
String termStr,
float minSimilarity)
Factory method for generating a query (similar to
QueryParserBase.getWildcardQuery(java.lang.String, java.lang.String) ). |
protected Query |
getMultiFieldQuery(List<Query> queries)
Creates a multifield query
|
protected Query |
getPrefixQuery(String field,
String termStr)
Factory method for generating a query (similar to
QueryParserBase.getWildcardQuery(java.lang.String, java.lang.String) ). |
protected Query |
getRangeQuery(String field,
String part1,
String part2,
boolean startInclusive,
boolean endInclusive) |
protected Query |
getRegexpQuery(String field,
String termStr)
Factory method for generating a query.
|
protected Query |
getWildcardQuery(String field,
String termStr)
Factory method for generating a query.
|
static Query |
parse(String[] queries,
String[] fields,
Analyzer analyzer)
Parses a query which searches on the fields specified.
|
static Query |
parse(String[] queries,
String[] fields,
BooleanClause.Occur[] flags,
Analyzer analyzer)
Parses a query, searching on the fields specified.
|
static Query |
parse(String query,
String[] fields,
BooleanClause.Occur[] flags,
Analyzer analyzer)
Parses a query, searching on the fields specified.
|
Clause, Conjunction, disable_tracing, enable_tracing, generateParseException, getNextToken, getSplitOnWhitespace, getToken, Modifiers, MultiTerm, Query, ReInit, ReInit, setAutoGeneratePhraseQueries, setSplitOnWhitespace, Term, TopLevelQuery
addClause, addMultiTermClauses, escape, getAllowLeadingWildcard, getAutoGeneratePhraseQueries, getBooleanQuery, getDateResolution, getDefaultOperator, getField, getFuzzyMinSim, getFuzzyPrefixLength, getLocale, getMaxDeterminizedStates, getMultiTermRewriteMethod, getPhraseSlop, getTimeZone, init, newBooleanClause, newFieldQuery, newFuzzyQuery, newMatchAllDocsQuery, newPrefixQuery, newRangeQuery, newRegexpQuery, newWildcardQuery, parse, setAllowLeadingWildcard, setDateResolution, setDateResolution, setDefaultOperator, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setMaxDeterminizedStates, setMultiTermRewriteMethod, setPhraseSlop, setTimeZone
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, setAutoGenerateMultiTermSynonymsPhraseQuery, setEnableGraphQueries, setEnablePositionIncrements
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getAnalyzer, getEnablePositionIncrements, setEnablePositionIncrements
public MultiFieldQueryParser(String[] fields, Analyzer analyzer, Map<String,Float> boosts)
It will, when parse(String query)
is called, construct a query like this (assuming the query consists of
two terms and you specify the two fields title
and body
):
(title:term1 body:term1) (title:term2 body:term2)
When setDefaultOperator(AND_OPERATOR) is set, the result will be:
+(title:term1 body:term1) +(title:term2 body:term2)
When you pass a boost (title=>5 body=>10) you can get
+(title:term1^5.0 body:term1^10.0) +(title:term2^5.0 body:term2^10.0)
In other words, all the query's terms must appear, but it doesn't matter in what fields they appear.
public MultiFieldQueryParser(String[] fields, Analyzer analyzer)
It will, when parse(String query)
is called, construct a query like this (assuming the query consists of
two terms and you specify the two fields title
and body
):
(title:term1 body:term1) (title:term2 body:term2)
When setDefaultOperator(AND_OPERATOR) is set, the result will be:
+(title:term1 body:term1) +(title:term2 body:term2)
In other words, all the query's terms must appear, but it doesn't matter in what fields they appear.
protected Query getFieldQuery(String field, String queryText, int slop) throws ParseException
QueryParserBase
QueryParserBase.getFieldQuery(String,String,boolean)
.
This method may be overridden, for example, to return
a SpanNearQuery instead of a PhraseQuery.getFieldQuery
in class QueryParserBase
ParseException
- throw in overridden method to disallowprotected Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException
getFieldQuery
in class QueryParserBase
ParseException
- throw in overridden method to disallowprotected Query getFuzzyQuery(String field, String termStr, float minSimilarity) throws ParseException
QueryParserBase
QueryParserBase.getWildcardQuery(java.lang.String, java.lang.String)
). Called when parser parses
an input term token that has the fuzzy suffix (~) appended.getFuzzyQuery
in class QueryParserBase
field
- Name of the field query will use.termStr
- Term token to use for building term for the queryQuery
built for the termParseException
- throw in overridden method to disallowprotected Query getPrefixQuery(String field, String termStr) throws ParseException
QueryParserBase
QueryParserBase.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.
getPrefixQuery
in class QueryParserBase
field
- Name of the field query will use.termStr
- Term token to use for building term for the query
(without trailing '*' character!)Query
built for the termParseException
- throw in overridden method to disallowprotected Query getWildcardQuery(String field, String termStr) throws ParseException
QueryParserBase
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.
getWildcardQuery
in class QueryParserBase
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 termQuery
built for the termParseException
- throw in overridden method to disallowprotected Query getRangeQuery(String field, String part1, String part2, boolean startInclusive, boolean endInclusive) throws ParseException
getRangeQuery
in class QueryParserBase
ParseException
protected Query getRegexpQuery(String field, String termStr) throws ParseException
QueryParserBase
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.
getRegexpQuery
in class QueryParserBase
field
- Name of the field query will use.termStr
- Term token that contains a regular expressionQuery
built for the termParseException
- throw in overridden method to disallowprotected Query getMultiFieldQuery(List<Query> queries) throws ParseException
ParseException
public static Query parse(String[] queries, String[] fields, Analyzer analyzer) throws ParseException
If x fields are specified, this effectively constructs:
(field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
queries
- Queries strings to parsefields
- Fields to search onanalyzer
- Analyzer to useParseException
- if query parsing failsIllegalArgumentException
- if the length of the queries array differs
from the length of the fields arraypublic static Query parse(String query, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException
Usage:
String[] fields = {"filename", "contents", "description"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST_NOT};
MultiFieldQueryParser.parse("query", fields, flags, analyzer);
The code above would construct a query:
(filename:query) +(contents:query) -(description:query)
query
- Query string to parsefields
- Fields to search onflags
- Flags describing the fieldsanalyzer
- Analyzer to useParseException
- if query parsing failsIllegalArgumentException
- if the length of the fields array differs
from the length of the flags arraypublic static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException
Usage:
String[] query = {"query1", "query2", "query3"};
String[] fields = {"filename", "contents", "description"};
BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
BooleanClause.Occur.MUST,
BooleanClause.Occur.MUST_NOT};
MultiFieldQueryParser.parse(query, fields, flags, analyzer);
The code above would construct a query:
(filename:query1) +(contents:query2) -(description:query3)
queries
- Queries string to parsefields
- Fields to search onflags
- Flags describing the fieldsanalyzer
- Analyzer to useParseException
- if query parsing failsIllegalArgumentException
- if the length of the queries, fields,
and flags array differCopyright © 2000-2021 Apache Software Foundation. All Rights Reserved.