public class

MultiFieldQueryParser

extends QueryParser
java.lang.Object
   ↳ org.apache.lucene.queryParser.QueryParser
     ↳ org.apache.lucene.queryParser.MultiFieldQueryParser

Class Overview

A QueryParser which constructs queries to search multiple fields.

Summary

[Expand]
Inherited Constants
From interface org.apache.lucene.queryParser.QueryParserConstants
Fields
protected Map<StringFloat> boosts
protected String[] fields
[Expand]
Inherited Fields
From class org.apache.lucene.queryParser.QueryParser
From interface org.apache.lucene.queryParser.QueryParserConstants
Public Constructors
MultiFieldQueryParser(Version matchVersion, String[] fields, Analyzer analyzer, Map<StringFloat> boosts)
Creates a MultiFieldQueryParser.
MultiFieldQueryParser(Version matchVersion, String[] fields, Analyzer analyzer)
Creates a MultiFieldQueryParser.
Public Methods
static Query parse(Version matchVersion, String[] queries, String[] fields, Analyzer analyzer)
Parses a query which searches on the fields specified.
static Query parse(Version matchVersion, String[] queries, String[] fields, Occur[] flags, Analyzer analyzer)
Parses a query, searching on the fields specified.
static Query parse(Version matchVersion, String query, String[] fields, Occur[] flags, Analyzer analyzer)
Parses a query, searching on the fields specified.
Protected Methods
Query getFieldQuery(String field, String queryText)
Query getFieldQuery(String field, String queryText, int slop)
Base implementation delegates to getFieldQuery(String, String).
Query getFuzzyQuery(String field, String termStr, float minSimilarity)
Factory method for generating a query (similar to getWildcardQuery(String, String)).
Query getPrefixQuery(String field, String termStr)
Factory method for generating a query (similar to getWildcardQuery(String, String)).
Query getRangeQuery(String field, String part1, String part2, boolean inclusive)
Query getWildcardQuery(String field, String termStr)
Factory method for generating a query.
[Expand]
Inherited Methods
From class org.apache.lucene.queryParser.QueryParser
From class java.lang.Object

Fields

protected Map<StringFloat> boosts

protected String[] fields

Public Constructors

public MultiFieldQueryParser (Version matchVersion, String[] fields, Analyzer analyzer, Map<StringFloat> boosts)

Creates a MultiFieldQueryParser. Allows passing of a map with term to Boost, and the boost to apply to each term.

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 (Version matchVersion, 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.

Public Methods

public static Query parse (Version matchVersion, String[] queries, String[] fields, Analyzer analyzer)

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
matchVersion Lucene version to match; this is passed through to QueryParser.
queries Queries strings to parse
fields Fields to search on
analyzer Analyzer to use
Throws
ParseException if query parsing fails
IllegalArgumentException if the length of the queries array differs from the length of the fields array

public static Query parse (Version matchVersion, String[] queries, String[] fields, Occur[] flags, Analyzer analyzer)

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
matchVersion Lucene version to match; this is passed through to QueryParser.
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
IllegalArgumentException if the length of the queries, fields, and flags array differ

public static Query parse (Version matchVersion, String query, String[] fields, Occur[] flags, Analyzer analyzer)

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
matchVersion Lucene version to match; this is passed through to QueryParser.
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
IllegalArgumentException if the length of the fields array differs from the length of the flags array

Protected Methods

protected Query getFieldQuery (String field, String queryText)

protected Query getFieldQuery (String field, String queryText, int slop)

Base implementation delegates to getFieldQuery(String, String). This method may be overridden, for example, to return a SpanNearQuery instead of a PhraseQuery.

protected Query getFuzzyQuery (String field, String termStr, float minSimilarity)

Factory method for generating a query (similar to getWildcardQuery(String, String)). Called when parser parses an input term token that has the fuzzy suffix (~) appended.

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

protected Query getPrefixQuery (String field, String termStr)

Factory method for generating a query (similar to getWildcardQuery(String, 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.

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

protected Query getRangeQuery (String field, String part1, String part2, boolean inclusive)

protected Query getWildcardQuery (String field, String termStr)

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.

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