FileDocCategorySizeDatePackage
MultiFieldQueryParser.javaAPI DocApache Lucene 1.915782Mon Feb 20 09:19:42 GMT 2006org.apache.lucene.queryParser

MultiFieldQueryParser

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

Fields Summary
private String[]
fields
public static final int
NORMAL_FIELD
public static final int
REQUIRED_FIELD
public static final int
PROHIBITED_FIELD
Constructors Summary
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.

    super(null, analyzer);
    this.fields = fields;
  
public MultiFieldQueryParser(QueryParserTokenManager tm)

deprecated
use {@link #MultiFieldQueryParser(String[], Analyzer)} instead


           
    
  
    super(tm);
  
public MultiFieldQueryParser(CharStream stream)

deprecated
use {@link #MultiFieldQueryParser(String[], Analyzer)} instead

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

deprecated
use {@link #MultiFieldQueryParser(String[], Analyzer)} instead

    super(f, a);
  
Methods Summary
protected org.apache.lucene.search.QuerygetFieldQuery(java.lang.String field, java.lang.String queryText, int slop)

    if (field == null) {
      Vector clauses = new Vector();
      for (int i = 0; i < fields.length; i++) {
        Query q = super.getFieldQuery(fields[i], queryText);
        if (q != null) {
          if (q instanceof PhraseQuery) {
            ((PhraseQuery) q).setSlop(slop);
          }
          if (q instanceof MultiPhraseQuery) {
            ((MultiPhraseQuery) q).setSlop(slop);
          }
          clauses.add(new BooleanClause(q, BooleanClause.Occur.SHOULD));
        }
      }
      if (clauses.size() == 0)  // happens for stopwords
        return null;
      return getBooleanQuery(clauses, true);
    }
    return super.getFieldQuery(field, queryText);
  
protected org.apache.lucene.search.QuerygetFieldQuery(java.lang.String field, java.lang.String queryText)

    return getFieldQuery(field, queryText, 0);
  
protected org.apache.lucene.search.QuerygetFieldQuery(java.lang.String field, org.apache.lucene.analysis.Analyzer analyzer, java.lang.String queryText)

deprecated
use {@link #getFieldQuery(String, String)}

    return getFieldQuery(field, queryText);
  
protected org.apache.lucene.search.QuerygetFuzzyQuery(java.lang.String field, java.lang.String termStr)

deprecated
use {@link #getFuzzyQuery(String, String, float)}

    return getFuzzyQuery(field, termStr, fuzzyMinSim);
  
protected org.apache.lucene.search.QuerygetFuzzyQuery(java.lang.String field, java.lang.String termStr, float minSimilarity)

    if (field == null) {
      Vector clauses = new Vector();
      for (int i = 0; i < fields.length; i++) {
        clauses.add(new BooleanClause(super.getFuzzyQuery(fields[i], termStr, minSimilarity),
            BooleanClause.Occur.SHOULD));
      }
      return getBooleanQuery(clauses, true);
    }
    return super.getFuzzyQuery(field, termStr, minSimilarity);
  
protected org.apache.lucene.search.QuerygetPrefixQuery(java.lang.String field, java.lang.String termStr)

    if (field == null) {
      Vector clauses = new Vector();
      for (int i = 0; i < fields.length; i++) {
        clauses.add(new BooleanClause(super.getPrefixQuery(fields[i], termStr),
            BooleanClause.Occur.SHOULD));
      }
      return getBooleanQuery(clauses, true);
    }
    return super.getPrefixQuery(field, termStr);
  
protected org.apache.lucene.search.QuerygetRangeQuery(java.lang.String field, java.lang.String part1, java.lang.String part2, boolean inclusive)

    if (field == null) {
      Vector clauses = new Vector();
      for (int i = 0; i < fields.length; i++) {
        clauses.add(new BooleanClause(super.getRangeQuery(fields[i], part1, part2, inclusive),
            BooleanClause.Occur.SHOULD));
      }
      return getBooleanQuery(clauses, true);
    }
    return super.getRangeQuery(field, part1, part2, inclusive);
  
protected org.apache.lucene.search.QuerygetRangeQuery(java.lang.String field, org.apache.lucene.analysis.Analyzer analyzer, java.lang.String part1, java.lang.String part2, boolean inclusive)

throws
ParseException
deprecated
use {@link #getRangeQuery(String, String, String, boolean)}

   return getRangeQuery(field, part1, part2, inclusive);
  
protected org.apache.lucene.search.QuerygetWildcardQuery(java.lang.String field, java.lang.String termStr)

    if (field == null) {
      Vector clauses = new Vector();
      for (int i = 0; i < fields.length; i++) {
        clauses.add(new BooleanClause(super.getWildcardQuery(fields[i], termStr),
            BooleanClause.Occur.SHOULD));
      }
      return getBooleanQuery(clauses, true);
    }
    return super.getWildcardQuery(field, termStr);
  
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
deprecated
use {@link #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.

    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      Query q = parse(query, fields[i], analyzer);
      bQuery.add(q, BooleanClause.Occur.SHOULD);
    }
    return bQuery;
  
public static org.apache.lucene.search.Queryparse(java.lang.String[] queries, 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:query1) (field2:query2) (field3:query3)...(fieldx:queryx)

param
queries Queries strings 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
throws
IllegalArgumentException if the length of the queries array differs from the length of the fields array

    if (queries.length != fields.length)
      throw new IllegalArgumentException("queries.length != fields.length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      QueryParser qp = new QueryParser(fields[i], analyzer);
      Query q = qp.parse(queries[i]);
      bQuery.add(q, BooleanClause.Occur.SHOULD);
    }
    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
throws
IllegalArgumentException if the length of the fields array differs from the length of the flags array
deprecated
use {@link #parse(String, String[], BooleanClause.Occur[], Analyzer)} instead

    if (fields.length != flags.length)
      throw new IllegalArgumentException("fields.length != flags.length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      QueryParser qp = new QueryParser(fields[i], analyzer);
      Query q = qp.parse(query);
      int flag = flags[i];
      switch (flag)
      {
        case REQUIRED_FIELD:
          bQuery.add(q, BooleanClause.Occur.MUST);
          break;
        case PROHIBITED_FIELD:
          bQuery.add(q, BooleanClause.Occur.MUST_NOT);
          break;
        default:
          bQuery.add(q, BooleanClause.Occur.SHOULD);
        break;
      }
    }
    return bQuery;
  
public static org.apache.lucene.search.Queryparse(java.lang.String query, java.lang.String[] fields, org.apache.lucene.search.BooleanClause$Occur[] 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"};
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)

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
throws
IllegalArgumentException if the length of the fields array differs from the length of the flags array

    if (fields.length != flags.length)
      throw new IllegalArgumentException("fields.length != flags.length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++) {
      QueryParser qp = new QueryParser(fields[i], analyzer);
      Query q = qp.parse(query);
      bQuery.add(q, flags[i]);
    }
    return bQuery;
  
public static org.apache.lucene.search.Queryparse(java.lang.String[] queries, 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:query1) +(contents:query2) -(description:query3)

param
queries Queries 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
throws
IllegalArgumentException if the length of the queries, fields, and flags array differ
deprecated
use {@link #parse(String[], String[], BooleanClause.Occur[], Analyzer)} instead

    if (!(queries.length == fields.length && queries.length == flags.length))
      throw new IllegalArgumentException("queries, fields, and flags array have have different length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      QueryParser qp = new QueryParser(fields[i], analyzer);
      Query q = qp.parse(queries[i]);
      int flag = flags[i];
      switch (flag)
      {
        case REQUIRED_FIELD:
          bQuery.add(q, BooleanClause.Occur.MUST);
          break;
        case PROHIBITED_FIELD:
          bQuery.add(q, BooleanClause.Occur.MUST_NOT);
          break;
        default:
          bQuery.add(q, BooleanClause.Occur.SHOULD);
          break;
      }
    }
    return bQuery;
  
public static org.apache.lucene.search.Queryparse(java.lang.String[] queries, java.lang.String[] fields, org.apache.lucene.search.BooleanClause$Occur[] 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[] 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)

param
queries Queries 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
throws
IllegalArgumentException if the length of the queries, fields, and flags array differ

    if (!(queries.length == fields.length && queries.length == flags.length))
      throw new IllegalArgumentException("queries, fields, and flags array have have different length");
    BooleanQuery bQuery = new BooleanQuery();
    for (int i = 0; i < fields.length; i++)
    {
      QueryParser qp = new QueryParser(fields[i], analyzer);
      Query q = qp.parse(queries[i]);
      bQuery.add(q, flags[i]);
    }
    return bQuery;