org.apache.lucene.queryParser
Class MultiFieldQueryParser

java.lang.Object
  extended by org.apache.lucene.queryParser.QueryParser
      extended by org.apache.lucene.queryParser.MultiFieldQueryParser
All Implemented Interfaces:
QueryParserConstants

public class MultiFieldQueryParser
extends QueryParser

A QueryParser which constructs queries to search multiple fields.

Version:
$Revision: 295117 $
Author:
Kelvin Tan, Daniel Naber

Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.lucene.queryParser.QueryParser
QueryParser.Operator
 
Field Summary
static int NORMAL_FIELD
          Deprecated.  
static int PROHIBITED_FIELD
          Deprecated.  
static int REQUIRED_FIELD
          Deprecated.  
 
Fields inherited from class org.apache.lucene.queryParser.QueryParser
AND_OPERATOR, DEFAULT_OPERATOR_AND, DEFAULT_OPERATOR_OR, jj_nt, lookingAhead, OR_OPERATOR, token, token_source
 
Fields inherited from interface org.apache.lucene.queryParser.QueryParserConstants
_ESCAPED_CHAR, _NUM_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, AND, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, RangeEx, RANGEEX_END, RANGEEX_GOOP, RANGEEX_QUOTED, RANGEEX_START, RANGEEX_TO, RangeIn, RANGEIN_END, RANGEIN_GOOP, RANGEIN_QUOTED, RANGEIN_START, RANGEIN_TO, RPAREN, TERM, tokenImage, WILDTERM
 
Constructor Summary
MultiFieldQueryParser(CharStream stream)
          Deprecated. use MultiFieldQueryParser(String[], Analyzer) instead
MultiFieldQueryParser(QueryParserTokenManager tm)
          Deprecated. use MultiFieldQueryParser(String[], Analyzer) instead
MultiFieldQueryParser(String[] fields, Analyzer analyzer)
          Creates a MultiFieldQueryParser.
MultiFieldQueryParser(String f, Analyzer a)
          Deprecated. use MultiFieldQueryParser(String[], Analyzer) instead
 
Method Summary
protected  Query getFieldQuery(String field, Analyzer analyzer, String queryText)
          Deprecated. use getFieldQuery(String, String)
protected  Query getFieldQuery(String field, String queryText)
           
protected  Query getFieldQuery(String field, String queryText, int slop)
          Base implementation delegates to QueryParser.getFieldQuery(String,String).
protected  Query getFuzzyQuery(String field, String termStr)
          Deprecated. use getFuzzyQuery(String, String, float)
protected  Query getFuzzyQuery(String field, String termStr, float minSimilarity)
          Factory method for generating a query (similar to QueryParser.getWildcardQuery(java.lang.String, java.lang.String)).
protected  Query getPrefixQuery(String field, String termStr)
          Factory method for generating a query (similar to QueryParser.getWildcardQuery(java.lang.String, java.lang.String)).
protected  Query getRangeQuery(String field, Analyzer analyzer, String part1, String part2, boolean inclusive)
          Deprecated. use getRangeQuery(String, String, String, boolean)
protected  Query getRangeQuery(String field, String part1, String part2, boolean inclusive)
           
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[] queries, String[] fields, int[] flags, Analyzer analyzer)
          Deprecated. use parse(String[], String[], BooleanClause.Occur[], Analyzer) instead
static Query parse(String query, String[] fields, Analyzer analyzer)
          Deprecated. use QueryParser.parse(String) instead but note that it returns a different query for queries where all terms are required: its query excepts all terms, no matter in what field they occur whereas the query built by this (deprecated) method expected all terms in all fields at the same time.
static Query parse(String query, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer)
          Parses a query, searching on the fields specified.
static Query parse(String query, String[] fields, int[] flags, Analyzer analyzer)
          Deprecated. use parse(String, String[], BooleanClause.Occur[], Analyzer) instead
 
Methods inherited from class org.apache.lucene.queryParser.QueryParser
addClause, Clause, Conjunction, disable_tracing, enable_tracing, escape, generateParseException, getAnalyzer, getBooleanQuery, getBooleanQuery, getDefaultOperator, getField, getFieldQuery, getFuzzyMinSim, getFuzzyPrefixLength, getLocale, getLowercaseExpandedTerms, getLowercaseWildcardTerms, getNextToken, getOperator, getPhraseSlop, getToken, main, Modifiers, parse, parse, Query, ReInit, ReInit, setDefaultOperator, setFuzzyMinSim, setFuzzyPrefixLength, setLocale, setLowercaseExpandedTerms, setLowercaseWildcardTerms, setOperator, setPhraseSlop, Term
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NORMAL_FIELD

public static final int NORMAL_FIELD
Deprecated. 
See Also:
Constant Field Values

REQUIRED_FIELD

public static final int REQUIRED_FIELD
Deprecated. 
See Also:
Constant Field Values

PROHIBITED_FIELD

public static final int PROHIBITED_FIELD
Deprecated. 
See Also:
Constant Field Values
Constructor Detail

MultiFieldQueryParser

public MultiFieldQueryParser(String[] fields,
                             Analyzer analyzer)
Creates a MultiFieldQueryParser.

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.


MultiFieldQueryParser

public MultiFieldQueryParser(QueryParserTokenManager tm)
Deprecated. use MultiFieldQueryParser(String[], Analyzer) instead


MultiFieldQueryParser

public MultiFieldQueryParser(CharStream stream)
Deprecated. use MultiFieldQueryParser(String[], Analyzer) instead


MultiFieldQueryParser

public MultiFieldQueryParser(String f,
                             Analyzer a)
Deprecated. use MultiFieldQueryParser(String[], Analyzer) instead

Method Detail

getFieldQuery

protected Query getFieldQuery(String field,
                              String queryText,
                              int slop)
                       throws ParseException
Description copied from class: QueryParser
Base implementation delegates to QueryParser.getFieldQuery(String,String). This method may be overridden, for example, to return a SpanNearQuery instead of a PhraseQuery.

Overrides:
getFieldQuery in class QueryParser
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field,
                              String queryText)
                       throws ParseException
Overrides:
getFieldQuery in class QueryParser
Throws:
ParseException - throw in overridden method to disallow

getFieldQuery

protected Query getFieldQuery(String field,
                              Analyzer analyzer,
                              String queryText)
                       throws ParseException
Deprecated. use getFieldQuery(String, String)

Description copied from class: QueryParser
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.

Overrides:
getFieldQuery in class QueryParser
Throws:
ParseException - throw in overridden method to disallow

getFuzzyQuery

protected Query getFuzzyQuery(String field,
                              String termStr)
                       throws ParseException
Deprecated. use getFuzzyQuery(String, String, float)

Overrides:
getFuzzyQuery in class QueryParser
Throws:
ParseException

getFuzzyQuery

protected Query getFuzzyQuery(String field,
                              String termStr,
                              float minSimilarity)
                       throws ParseException
Description copied from class: QueryParser
Factory method for generating a query (similar to QueryParser.getWildcardQuery(java.lang.String, java.lang.String)). Called when parser parses an input term token that has the fuzzy suffix (~) appended.

Overrides:
getFuzzyQuery in class QueryParser
Parameters:
field - Name of the field query will use.
termStr - Term token to use for building term for the query
Returns:
Resulting Query built for the term
Throws:
ParseException - throw in overridden method to disallow

getPrefixQuery

protected Query getPrefixQuery(String field,
                               String termStr)
                        throws ParseException
Description copied from class: QueryParser
Factory method for generating a query (similar to QueryParser.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:
getPrefixQuery in class QueryParser
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 Query built 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: QueryParser
Factory 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:
getWildcardQuery in class QueryParser
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 Query built for the term
Throws:
ParseException - throw in overridden method to disallow

getRangeQuery

protected Query getRangeQuery(String field,
                              Analyzer analyzer,
                              String part1,
                              String part2,
                              boolean inclusive)
                       throws ParseException
Deprecated. use getRangeQuery(String, String, String, boolean)

Description copied from class: QueryParser
Note that parameter analyzer is ignored. Calls inside the parser always use class member analyzer.

Overrides:
getRangeQuery in class QueryParser
Throws:
ParseException

getRangeQuery

protected Query getRangeQuery(String field,
                              String part1,
                              String part2,
                              boolean inclusive)
                       throws ParseException
Overrides:
getRangeQuery in class QueryParser
Throws:
ParseException - throw in overridden method to disallow

parse

public static Query parse(String query,
                          String[] fields,
                          Analyzer analyzer)
                   throws ParseException
Deprecated. use QueryParser.parse(String) instead but note that it returns a different query for queries where all terms are required: its query excepts all terms, no matter in what field they occur whereas the query built by this (deprecated) method expected all terms in all fields at the same time.

Parses a query which searches on the fields specified. If x fields are specified, this effectively constructs: (field1:query) (field2:query) (field3:query)...(fieldx:query)

Parameters:
query - Query string to parse
fields - Fields to search on
analyzer - Analyzer to use
Throws:
ParseException - if query parsing fails
TokenMgrError - if query parsing fails

parse

public static Query parse(String[] queries,
                          String[] fields,
                          Analyzer analyzer)
                   throws ParseException
Parses a query which searches on the fields specified.

If x fields are specified, this effectively constructs:

 
 (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
 
 

Parameters:
queries - Queries strings to parse
fields - Fields to search on
analyzer - Analyzer to use
Throws:
ParseException - if query parsing fails
TokenMgrError - if query parsing fails
IllegalArgumentException - if the length of the queries array differs from the length of the fields array

parse

public static Query parse(String query,
                          String[] fields,
                          int[] flags,
                          Analyzer analyzer)
                   throws ParseException
Deprecated. use parse(String, String[], BooleanClause.Occur[], Analyzer) instead

Parses a query, searching on the fields specified. Use this if you need to specify certain fields as required, and others as prohibited.

 Usage:
 
 String[] fields = {"filename", "contents", "description"};
 int[] flags = {MultiFieldQueryParser.NORMAL_FIELD,
                MultiFieldQueryParser.REQUIRED_FIELD,
                MultiFieldQueryParser.PROHIBITED_FIELD,};
 parse(query, fields, flags, analyzer);
 
 

The code above would construct a query:

 
 (filename:query) +(contents:query) -(description:query)
 
 

Parameters:
query - Query string to parse
fields - Fields to search on
flags - Flags describing the fields
analyzer - Analyzer to use
Throws:
ParseException - if query parsing fails
TokenMgrError - if query parsing fails
IllegalArgumentException - if the length of the fields array differs from the length of the flags array

parse

public static Query parse(String query,
                          String[] fields,
                          BooleanClause.Occur[] flags,
                          Analyzer analyzer)
                   throws ParseException
Parses a query, searching on the fields specified. Use this if you need to specify certain fields as required, and others as prohibited.

 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)
 
 

Parameters:
query - Query string to parse
fields - Fields to search on
flags - Flags describing the fields
analyzer - Analyzer to use
Throws:
ParseException - if query parsing fails
TokenMgrError - if query parsing fails
IllegalArgumentException - if the length of the fields array differs from the length of the flags array

parse

public static Query parse(String[] queries,
                          String[] fields,
                          int[] flags,
                          Analyzer analyzer)
                   throws ParseException
Deprecated. use parse(String[], String[], BooleanClause.Occur[], Analyzer) instead

Parses a query, searching on the fields specified. Use this if you need to specify certain fields as required, and others as prohibited.

  Usage:
 
 String[] fields = { "filename", "contents", "description" };
 int[] flags = { MultiFieldQueryParser.NORMAL_FIELD,
     MultiFieldQueryParser.REQUIRED_FIELD,
     MultiFieldQueryParser.PROHIBITED_FIELD, };
 parse(query, fields, flags, analyzer);
 
 

The code above would construct a query:

 
  (filename:query1) +(contents:query2) -(description:query3)
 
 

Parameters:
queries - Queries string to parse
fields - Fields to search on
flags - Flags describing the fields
analyzer - Analyzer to use
Throws:
ParseException - if query parsing fails
TokenMgrError - if query parsing fails
IllegalArgumentException - if the length of the queries, fields, and flags array differ

parse

public static Query parse(String[] queries,
                          String[] fields,
                          BooleanClause.Occur[] flags,
                          Analyzer analyzer)
                   throws ParseException
Parses a query, searching on the fields specified. Use this if you need to specify certain fields as required, and others as prohibited.

 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)
 
 

Parameters:
queries - Queries string to parse
fields - Fields to search on
flags - Flags describing the fields
analyzer - Analyzer to use
Throws:
ParseException - if query parsing fails
TokenMgrError - if query parsing fails
IllegalArgumentException - if the length of the queries, fields, and flags array differ


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.