All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.queryparsers.HebrewMultiFieldQueryParser Maven / Gradle / Ivy

There is a newer version: 6.6.1
Show newest version
/***************************************************************************
 *   Copyright (C) 2010-2015 by                                            *
 *      Itamar Syn-Hershko                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Affero General Public License           *
 *   version 3, as published by the Free Software Foundation.              *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU Affero General Public License for more details.                   *
 *                                                                         *
 *   You should have received a copy of the GNU Affero General Public      *
 *   License along with this program; if not, see                          *
 *   .                                       *
 **************************************************************************/
package org.apache.lucene.queryparsers;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;

import java.util.Map;

public class HebrewMultiFieldQueryParser extends MultiFieldQueryParser {
    HebrewMultiFieldQueryParser(String[] fields, Analyzer analyzer) {
        super(fields, analyzer);
    }

    HebrewMultiFieldQueryParser(String[] fields, Analyzer analyzer, Map boosts) {
        super(fields, analyzer, boosts);
    }

    ///  Parses a query, searching on the fields specified. Use this if you need
    /// to specify certain fields as required, and others as prohibited.
    /// 

/// ///

    /// Usage:
    /// <code>
    /// String[] fields = {"filename", "contents", "description"};
    /// BooleanClause.Occur[] flags = {BooleanClause.Occur.SHOULD,
    /// BooleanClause.Occur.MUST,
    /// BooleanClause.Occur.MUST_NOT};
    /// MultiFieldQueryParser.parse("query", fields, flags, analyzer);
    /// </code>
    /// 
///

/// The code above would construct a query: /// ///

    /// <code>
    /// (filename:query) +(contents:query) -(description:query)
    /// </code>
    /// 
/// ///
/// Lucene version to match; this is passed through to /// QueryParser. /// /// Query string to parse /// /// Fields to search on /// /// Flags describing the fields /// /// Analyzer to use /// /// ParseException /// if query parsing fails /// /// IllegalArgumentException /// if the length of the fields array differs from the length of /// the flags array /// public static Query parse(String query, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException { if (fields.length > flags.length) throw new IllegalArgumentException("fields.length != flags.length"); BooleanQuery.Builder bQueryBuilder = new BooleanQuery.Builder(); for (int i = 0; i < fields.length; i++) { QueryParser qp = new HebrewQueryParser(fields[i], analyzer); Query q = qp.parse(query); if (q != null && (!(q instanceof BooleanQuery) || ((BooleanQuery) q).clauses().size() > 0)) { bQueryBuilder.add(q,flags[i]); } } return bQueryBuilder.build(); } /// Parses a query which searches on the fields specified. ///

/// If x fields are specified, this effectively constructs: /// ///

    /// <code>
    /// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
    /// </code>
    /// 
/// ///
/// Lucene version to match; this is passed through to /// QueryParser. /// /// Queries strings to parse /// /// Fields to search on /// /// Analyzer to use /// /// ParseException /// if query parsing fails /// /// IllegalArgumentException /// if the length of the queries array differs from the length of /// the fields array /// public static Query parse(String[] queries, String[] fields, Analyzer analyzer) throws ParseException { if (queries.length != fields.length) throw new IllegalArgumentException("queries.length != fields.length"); BooleanQuery.Builder bQueryBuilder = new BooleanQuery.Builder(); for (int i = 0; i < fields.length; i++) { QueryParser qp = new HebrewQueryParser(fields[i], analyzer); Query q = qp.parse(queries[i]); if (q != null && (!(q instanceof BooleanQuery) || ((BooleanQuery) q).clauses().size() > 0)) { bQueryBuilder.add(q, BooleanClause.Occur.SHOULD); } } return bQueryBuilder.build(); } /// Parses a query, searching on the fields specified. Use this if you need /// to specify certain fields as required, and others as prohibited. ///

/// ///

    /// Usage:
    /// <code>
    /// 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);
    /// </code>
    /// 
///

/// The code above would construct a query: /// ///

    /// <code>
    /// (filename:query1) +(contents:query2) -(description:query3)
    /// </code>
    /// 
/// ///
/// Lucene version to match; this is passed through to /// QueryParser. /// /// Queries string to parse /// /// Fields to search on /// /// Flags describing the fields /// /// Analyzer to use /// /// ParseException /// if query parsing fails /// /// IllegalArgumentException /// if the length of the queries, fields, and flags array differ /// public static Query parse(String[] queries, String[] fields, BooleanClause.Occur[] flags, Analyzer analyzer) throws ParseException { if (!(queries.length == fields.length && queries.length == flags.length)) throw new IllegalArgumentException("queries, fields, and flags array have have different length"); BooleanQuery.Builder bQueryBuilder = new BooleanQuery.Builder(); for (int i = 0; i < fields.length; i++) { QueryParser qp = new HebrewQueryParser(fields[i], analyzer); Query q = qp.parse(queries[i]); if (q != null && (!(q instanceof BooleanQuery) || ((BooleanQuery) q).clauses().size() > 0)) { bQueryBuilder.add(q, flags[i]); } } return bQueryBuilder.build(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy