FileDocCategorySizeDatePackage
MultiFieldQueryParser.javaAPI DocApache Lucene 1.4.34280Tue Mar 30 00:48:04 BST 2004org.apache.lucene.queryParser

MultiFieldQueryParser

public class MultiFieldQueryParser extends QueryParser
A QueryParser which constructs queries to search multiple fields.
author
Kelvin Tan
version
$Revision: 1.4 $

Fields Summary
public static final int
NORMAL_FIELD
public static final int
REQUIRED_FIELD
public static final int
PROHIBITED_FIELD
Constructors Summary
public MultiFieldQueryParser(QueryParserTokenManager tm)


      
    
        super(tm);
    
public MultiFieldQueryParser(CharStream stream)

        super(stream);
    
public MultiFieldQueryParser(String f, Analyzer a)

        super(f, a);
    
Methods Summary
public static org.apache.lucene.search.Queryparse(java.lang.String query, java.lang.String[] fields, org.apache.lucene.analysis.Analyzer analyzer)

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)

param
query Query string to parse
param
fields Fields to search on
param
analyzer Analyzer to use
throws
ParseException if query parsing fails
throws
TokenMgrError if query parsing fails

        BooleanQuery bQuery = new BooleanQuery();
        for (int i = 0; i < fields.length; i++)
        {
            Query q = parse(query, fields[i], analyzer);
            bQuery.add(q, false, false);
        }
        return bQuery;
    
public static org.apache.lucene.search.Queryparse(java.lang.String query, java.lang.String[] fields, int[] flags, org.apache.lucene.analysis.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"};
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)

param
query Query string to parse
param
fields Fields to search on
param
flags Flags describing the fields
param
analyzer Analyzer to use
throws
ParseException if query parsing fails
throws
TokenMgrError if query parsing fails

        BooleanQuery bQuery = new BooleanQuery();
        for (int i = 0; i < fields.length; i++)
        {
            Query q = parse(query, fields[i], analyzer);
            int flag = flags[i];
            switch (flag)
            {
                case REQUIRED_FIELD:
                    bQuery.add(q, true, false);
                    break;
                case PROHIBITED_FIELD:
                    bQuery.add(q, false, true);
                    break;
                default:
                    bQuery.add(q, false, false);
                    break;
            }
        }
        return bQuery;