Return-Path: Delivered-To: apmail-jakarta-lucene-dev-archive@apache.org Received: (qmail 67281 invoked from network); 22 Feb 2002 20:59:53 -0000 Received: from unknown (HELO nagoya.betaversion.org) (192.18.49.131) by daedalus.apache.org with SMTP; 22 Feb 2002 20:59:53 -0000 Received: (qmail 18468 invoked by uid 97); 22 Feb 2002 20:59:33 -0000 Delivered-To: qmlist-jakarta-archive-lucene-dev@jakarta.apache.org Received: (qmail 18412 invoked by uid 97); 22 Feb 2002 20:59:32 -0000 Mailing-List: contact lucene-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Lucene Developers List" Reply-To: "Lucene Developers List" Delivered-To: mailing list lucene-dev@jakarta.apache.org Received: (qmail 18338 invoked by uid 97); 22 Feb 2002 20:59:32 -0000 Date: 22 Feb 2002 20:59:11 -0000 Message-ID: <20020222205911.73465.qmail@icarus.apache.org> From: otis@apache.org To: jakarta-lucene-cvs@apache.org Subject: cvs commit: jakarta-lucene/src/java/org/apache/lucene/queryParser MultiFieldQueryParser.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N otis 02/02/22 12:59:11 Added: src/java/org/apache/lucene/queryParser MultiFieldQueryParser.java Log: - A query parser that searches multiple fields at once. Initial checkin. Submitted by: Kelvin Tan Reviewed by: Otis Gospodnetic Revision Changes Path 1.1 jakarta-lucene/src/java/org/apache/lucene/queryParser/MultiFieldQueryParser.java Index: MultiFieldQueryParser.java =================================================================== package org.apache.lucene.queryParser; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" * must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.queryParser.CharStream; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.queryParser.QueryParserTokenManager; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.Query; /** * A QueryParser which constructs queries to search multiple fields. * * @author Kelvin Tan * @version $Revision: 1.1 $ */ public class MultiFieldQueryParser extends QueryParser { public static final int NORMAL_FIELD = 0; public static final int REQUIRED_FIELD = 1; public static final int PROHIBITED_FIELD = 2; public MultiFieldQueryParser(QueryParserTokenManager tm) { super(tm); } public MultiFieldQueryParser(CharStream stream) { super(stream); } public MultiFieldQueryParser(String f, Analyzer a) { super(f, a); } /** *

* 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 ParserException if query parsing fails * @throws TokenMgrError if query parsing fails */ public static Query parse(String query, String[] fields, Analyzer analyzer) throws ParseException { 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; } /** *

* 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 ParserException if query parsing fails * @throws TokenMgrError if query parsing fails */ public static Query parse(String query, String[] fields, int[] flags, Analyzer analyzer) throws ParseException { 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; } } -- To unsubscribe, e-mail: For additional commands, e-mail: