alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

package org.apache.lucene.queryParser;

/**
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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.4 $
 */
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 ParseException 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 ParseException 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; } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.