org.apache.lucene.queryParser

Class MultiFieldQueryParser

Implemented Interfaces:
QueryParserConstants

public class MultiFieldQueryParser
extends QueryParser

A QueryParser which constructs queries to search multiple fields.
Version:
$Revision: 1.4 $
Author:
Kelvin Tan

Field Summary

static int
NORMAL_FIELD
static int
PROHIBITED_FIELD
static int
REQUIRED_FIELD

Fields inherited from class org.apache.lucene.queryParser.QueryParser

DEFAULT_OPERATOR_AND, DEFAULT_OPERATOR_OR, jj_nt, lookingAhead, token, token_source

Fields inherited from interface org.apache.lucene.queryParser.QueryParserConstants

AND, Boost, CARAT, COLON, DEFAULT, EOF, FUZZY_SLOP, LPAREN, MINUS, NOT, NUMBER, OR, PLUS, PREFIXTERM, QUOTED, RANGEEX_END, RANGEEX_GOOP, RANGEEX_QUOTED, RANGEEX_START, RANGEEX_TO, RANGEIN_END, RANGEIN_GOOP, RANGEIN_QUOTED, RANGEIN_START, RANGEIN_TO, RPAREN, RangeEx, RangeIn, TERM, WILDTERM, _ESCAPED_CHAR, _NUM_CHAR, _TERM_CHAR, _TERM_START_CHAR, _WHITESPACE, tokenImage

Constructor Summary

MultiFieldQueryParser(String f, Analyzer a)
MultiFieldQueryParser(CharStream stream)
MultiFieldQueryParser(QueryParserTokenManager tm)

Method Summary

static Query
parse(String query, String[] fields, int[] flags, Analyzer analyzer)
Parses a query, searching on the fields specified.
static Query
parse(String query, String[] fields, Analyzer analyzer)
Parses a query which searches on the fields specified.

Methods inherited from class org.apache.lucene.queryParser.QueryParser

Clause, Conjunction, Modifiers, Query, ReInit, ReInit, Term, addClause, disable_tracing, enable_tracing, escape, generateParseException, getAnalyzer, getBooleanQuery, getField, getFieldQuery, getFieldQuery, getFieldQuery, getFieldQuery, getFuzzyMinSim, getFuzzyQuery, getFuzzyQuery, getLocale, getLowercaseWildcardTerms, getNextToken, getOperator, getPhraseSlop, getPrefixQuery, getRangeQuery, getRangeQuery, getToken, getWildcardQuery, main, parse, parse, setFuzzyMinSim, setLocale, setLowercaseWildcardTerms, setOperator, setPhraseSlop

Field Details

NORMAL_FIELD

public static final int NORMAL_FIELD
Field Value:
0

PROHIBITED_FIELD

public static final int PROHIBITED_FIELD
Field Value:
2

REQUIRED_FIELD

public static final int REQUIRED_FIELD
Field Value:
1

Constructor Details

MultiFieldQueryParser

public MultiFieldQueryParser(String f,
                             Analyzer a)

MultiFieldQueryParser

public MultiFieldQueryParser(CharStream stream)

MultiFieldQueryParser

public MultiFieldQueryParser(QueryParserTokenManager tm)

Method Details

parse

public static Query parse(String query,
                          String[] fields,
                          int[] 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"};
 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

parse

public static Query parse(String query,
                          String[] fields,
                          Analyzer analyzer)
            throws ParseException
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

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