IMHO QueryParser.DEFAULT_OPERATOR_AND and QueryParser.DEFAULT_OPERATOR_OR should be used instead QueryParser.AND and QueryParser.OR

Peter Veentjer - Anchor Men wrote:

package com.jph.lucene.parsers;

/**
* 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.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;

/**
* A QueryParser which constructs queries to indexSearcher multiple
fields.
* <p/>
* Deze parser kan waarschijnlijk gekicked worden bij Lucene 2.0.
*
* @author Peter Veentjer
*/
public class MultiFieldQueryParserUtil {
        /**
         * <p/>
         * Parses a query which searches on the fields specified.
         * <p/>
         * If x fields are specified, this effectively constructs:
         * <pre>
         * <code>
         * (field1:query) (field2:query) (field3:query)...(fieldx:query)
         * </code>
         * </pre>
         *
         * @param query    Query string to parse
         * @param fields   Fields to indexSearcher on
         * @param analyzer Analyzer to use
         * @throws ParseException if query parsing fails
         * @throws org.apache.lucene.queryParser.TokenMgrError
         *                        if query parsing fails
         */
        public static Query parse(String query, String[] fields,
Analyzer analyzer, boolean and) throws ParseException {
                BooleanQuery bQuery = new BooleanQuery();
                for (int i = 0; i < fields.length; i++) {
                        QueryParser queryParser = new
QueryParser(fields[i], analyzer);
                        queryParser.setOperator(and ? QueryParser.AND :
QueryParser.OR);
                        Query q = queryParser.parse(query);
                        bQuery.add(q, false, false);
                }
                return bQuery;
        }

        /**
         * <p/>
         * Parses a query, searching on the fields specified.
         * Use this if you need to specify certain fields as required,
         * and others as prohibited.
         * <p><pre>
         * Usage:
         * <code>
         * String[] fields = {"filename", "contents", "description"};
         * int[] flags = {MultiFieldQueryParser.NORMAL FIELD,
         *                MultiFieldQueryParser.REQUIRED FIELD,
         *                MultiFieldQueryParser.PROHIBITED FIELD,};
         * parse(query, fields, flags, analyzer);
         * </code>
         * </pre>
         * <p/>
         * The code above would construct a query:
         * <pre>
         * <code>
         * (filename:query) +(contents:query) -(description:query)
         * </code>
         * </pre>
         *
         * @param query    Query string to parse
         * @param fields   Fields to indexSearcher on
         * @param flags    Flags describing the fields
         * @param analyzer Analyzer to use
         * @throws ParseException if query parsing fails
         * @throws org.apache.lucene.queryParser.TokenMgrError
         *                        if query parsing fails
         */
        public static Query parse(String query, String[] fields, int[]
flags, Analyzer analyzer, boolean and) throws ParseException {
                if (query == null)
                        throw new NullPointerException("query can`t be
null");
                if (fields == null)
                        throw new NullPointerException("fields can`t be
null");
                if (flags == null)
                        throw new NullPointerException("flags can`t be
null");
                if (fields.length != flags.length)
                        throw new IllegalArgumentException();
                if (analyzer == null)
                        throw new NullPointerException("analyzer can`t
be null");


BooleanQuery bQuery = new BooleanQuery(); for (int i = 0; i < fields.length; i++) { QueryParser queryParser = new QueryParser(fields[i], analyzer); queryParser.setOperator(and ? QueryParser.AND : QueryParser.OR); Query q = queryParser.parse(query);

                        int flag = flags[i];
                        switch (flag) {
                                case
MultiFieldQueryParser.REQUIRED_FIELD:
                                        bQuery.add(q, true, false);
                                        break;
                                case
MultiFieldQueryParser.PROHIBITED_FIELD:
                                        bQuery.add(q, false, true);
                                        break;
                                case MultiFieldQueryParser.NORMAL_FIELD:
                                        bQuery.add(q, false, false);
                                        break;
                                default:
                                        throw new
IllegalArgumentException("unrecognized fieldflag: " + flag);
                        }
                }
                return bQuery;
        }

        public static Query parseAnd(String query, String[] fields,
int[] flags, Analyzer analyzer) throws ParseException {
                return parse(query, fields, flags, analyzer, true);
        }

public static Query parseOr(String query, String[] fields, int[]
flags, Analyzer analyzer) throws ParseException {
return parse(query, fields, flags, analyzer, false);
}
}


-----Oorspronkelijk bericht-----
Van: Volodymyr Bychkoviak [mailto:[EMAIL PROTECTED] Verzonden: woensdag 20 april 2005 15:21
Aan: [email protected]
Onderwerp: Re: What is going on with subversion.


Sorry, I've already read about servers moving.

Can somebody mail me latest  MultiFieldQueryParser.java  and
highlighting source code. Because I can't get it from subversion and I
need it urgently.

Thanks in advance.


Regards, Volodymyr Bychkoviak

Volodymyr Bychkoviak wrote:



I can't connect svn.apache.org. It seems that apache.org is down.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to